From patchwork Sun Mar 11 12:38:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 131343 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp3055504lja; Sun, 11 Mar 2018 05:39:12 -0700 (PDT) X-Google-Smtp-Source: AG47ELsyy/Cue4bCK+n4tjhjqr5IDsTysmqoP/b5nJahHZlrJiWEGz0BfS1VI+qSt9FD1JAE1R7s X-Received: by 2002:a17:902:467:: with SMTP id 94-v6mr4744903ple.57.1520771952058; Sun, 11 Mar 2018 05:39:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520771952; cv=none; d=google.com; s=arc-20160816; b=uloD54DLysXGeGyjDksfhJy3PGYz/FRXQtJA/51lJO4Btx3ccRMHTXkXEVn1o6+mrn fRZ9O4CDgMafZH6v4lslptOrDm9uqA/zWttoL5u6ICo6Rm6g/mfAcy+Ya+LzRXQaJ1jw 47WulVZV0sZthVKF4gjwKaVbzb77ObUsL1l+cdK+OgG51kcdH5vC55cGCpm2PwrWNhE8 gLlq2rEfnticgaXOhAMGekMzta8A57lFbkF1EXt1b0ep1YmakP38seyioHfyEFwmet73 dEMpSsNHlJSr6Yz9Qw9PNWXeD9NNgRgbGirfTzR1ZktUo/qfcy/FMkVNVGMeKQkTMfdy 956g== 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=GRMoN92mam/mVPAjYjGRyuEsi/CGFDhSglGru3Ea2sA=; b=j9jm5uzotpPy6Nk2fRNQS7tIvgkdIIJsr0njCcbShWCOHzYlB/471/psTlDj/Kcfgv +2dXTnJPmdW/bE9uYKjU2WYvt5DE5a/XeWBDZRb+aVmSM2q7WnIILlirAniYiTGOCfBd I984L8bLilyjOAn4vzXsjuZuETmzYaKMa9YfCGO40WsxoT/nCPa/gKNwg0Sy83lGWSsW LTm4186uusR4eCmpHEA/TRqUh9jK7MRkgQRpYyllJhByE825bz4Mdq6JeSVDIxcifd8r XyVvJqMF/PZefhmu5vTVTZj1taT0TTLJKNDuXN/+iqR6JcfE6AIZvvRXvRA1uVZL1s9k gm4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=K8yGCz2Y; 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 x4si3595555pgo.278.2018.03.11.05.39.11; Sun, 11 Mar 2018 05:39:12 -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=K8yGCz2Y; 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 S932240AbeCKMjF (ORCPT + 28 others); Sun, 11 Mar 2018 08:39:05 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:52808 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932126AbeCKMi7 (ORCPT ); Sun, 11 Mar 2018 08:38:59 -0400 Received: by mail-wm0-f66.google.com with SMTP id t3so11440564wmc.2 for ; Sun, 11 Mar 2018 05:38:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=GRMoN92mam/mVPAjYjGRyuEsi/CGFDhSglGru3Ea2sA=; b=K8yGCz2Y2VZYolB8JIt7DSN9hhN8tnUtTlDYlw/21Vwh6F0bwOWPTrbazxtmu1/CjV Mg92M4fhS4NWoqTSNXHqtl1mnh15fbz45amAK4ymmWtfWbB53yhO1M/zjlhHWh0mPYVM m34uvElvZpvKeUt32LzmlPKBKvNgFVcd4X3Oc= 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=GRMoN92mam/mVPAjYjGRyuEsi/CGFDhSglGru3Ea2sA=; b=fFHeWaXIiHOzft2z5PLrzvtDWX7iY+2+CePnAqhvUVgVDGy+gy0u04UNUJ079McaJh +PjkWovRrzg8PMRx4OVcAGSXfBq7QPzzMU4eQfdzpvy1azW3pe8aEKtkekjZiRlxYGsS VFyefTa0QxVWqpRVexroaXvczuAoLXtcPAzMOv/Xi7+D5oqFW22lSebOoQppz/jIMFrJ z0KG9tYyD7tW2aLD56MAKGb9r+MfpXH20kV63y6aQSZ+Pn8fQB8CUMk5oyPaMaVjf4Qs H21qh7+buwZTWyO2axiXvTEW2DFdyogi9tii+6PWsdwHPl8smMq7NPG7GpR4XSVMLPzC k4JQ== X-Gm-Message-State: AElRT7Eqa8N+fR2AX3K+lwHixjCXyhc8GBx8IPldEj//CK1tkWmR4DD3 YVc4JuWKbt7Id7smmhZq6ooXOQ== X-Received: by 10.28.26.202 with SMTP id a193mr3281810wma.138.1520771938386; Sun, 11 Mar 2018 05:38:58 -0700 (PDT) Received: from localhost.localdomain ([105.148.128.186]) by smtp.gmail.com with ESMTPSA id h188sm2569103wmf.37.2018.03.11.05.38.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Mar 2018 05:38:57 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.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-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: [PATCH v8 1/6] arch: enable relative relocations for arm64, power and x86 Date: Sun, 11 Mar 2018 12:38:10 +0000 Message-Id: <20180311123815.17916-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180311123815.17916-1-ard.biesheuvel@linaro.org> References: <20180311123815.17916-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.15.1 diff --git a/arch/Kconfig b/arch/Kconfig index 76c0b54443b1..4e624f75823a 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -975,4 +975,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 7381eeb7ef8e..7c543667703e 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -91,6 +91,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 73ce5dd07642..21d5ad1608d8 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -181,6 +181,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 c1236b187824..1c6e2ddd2fdc 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -117,6 +117,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 Sun Mar 11 12:38:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 131348 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp3056218lja; Sun, 11 Mar 2018 05:40:16 -0700 (PDT) X-Google-Smtp-Source: AG47ELt3t4Kl812KUqOavmvAqm8pnZJgeRwUj7VDlg/wxErKv0gLc1EbUbPii2LLDxpkYTbDgqUq X-Received: by 2002:a17:902:6ecd:: with SMTP id l13-v6mr4775336pln.374.1520772015860; Sun, 11 Mar 2018 05:40:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520772015; cv=none; d=google.com; s=arc-20160816; b=k7yS30deBuYRBuM8A0m+U2aYOHdSC9niKRhm8MIMecszJOO8c2wot+TBFFshjIdNaK nd3PaO55Fb14SaJbUUrFfzQ1AIyYZwZ5pv6M4xbRSAKY98BjZ0VWFZGql9wZZiuoVIre iBLquMjN8oACjw6Wmhplgm6gVxCOQ41BRedxuvR2kaEJ63IBTp5EuoGmB/SgNQuY3LLy yzkayIa5eR7gQdWPAe4x6Fbl0m+A7Dxim2WKpP7uQpqnMKxAgvwFVSsaAJLi5vrHpqwR 5XTxzmZxzd67rZpSAgK41Q+eOIWExe1axnF+6k4lM91jrKERHSH5JVlzH5vm04xU+ql3 Vu0g== 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=J6NwNeKEJnTS9CbN6SiQmSD17aI7GednBlkCHTWkumc=; b=tG0FWhYshKyal3HelrSGJTYVCjdmllKH6CK7DZLGkh24SMLKvAmE9E9MNCcwfFUkWj aAXwtQ1C1wf8nKKOdfrSfZaIcKX9pMLyjQ6dqFK4rvjUyJGkdgRbl6Cnh09YQHOSHYpp SQT3IDN8QnUy4bsGLtulocBSRXjx6s1/0Ev9d+hjx9ZwsPyzRSrAvyicxK+FRk6ilJcO LZQXmTL60AYiNoVSJ7fpIjbHO3BHMD+bM6ykznlt4a94GIx7oqQhjloDKZPj6vwccCre EaRx2U2JNzqVyOqttZdiYZmcfv6kp9heIqUZtL0lJDJpDLXp/MgvvKjdNQ8XfRzUDLAH Qzaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KFxDY1xU; 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 q11si3643507pgv.144.2018.03.11.05.40.15; Sun, 11 Mar 2018 05:40:15 -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=KFxDY1xU; 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 S932361AbeCKMkL (ORCPT + 28 others); Sun, 11 Mar 2018 08:40:11 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:34189 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932180AbeCKMjE (ORCPT ); Sun, 11 Mar 2018 08:39:04 -0400 Received: by mail-wr0-f193.google.com with SMTP id o8so13038974wra.1 for ; Sun, 11 Mar 2018 05:39:02 -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=J6NwNeKEJnTS9CbN6SiQmSD17aI7GednBlkCHTWkumc=; b=KFxDY1xUwCHPfI/HYAwbLhwkm4HxWX4FmjwJot7iIWaF6N/Pen6NsBwEcF7Oh0vU6m UQt3FJgyzqWs6lU/rH6LxqsDQLJh6Ip5x3kSOvIi4DXohBSKxP26ileZsXg0ToAsOS9v YEHAiDbEHMVu8SPuARLuSm/HGsrN/klNmkrFo= 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=J6NwNeKEJnTS9CbN6SiQmSD17aI7GednBlkCHTWkumc=; b=tXLDkVf3cdtWyej1EVlLdAwb5rGH/VnHc8nkxs6emSp/5XCnQHWyLS5A1NSC+wsaKR 85IHOtyxG8c5cx594r8XQDshxDLhN9OTbBVt2r+YAW4Nq2zdjGbd2hjl4VoeHMvFEO4M BjK2w00j8526ku3+Nl5Anu/7+M57xcTSz7Hzr0i7tCYFM0pT+BXxOaX7bO2+noGx0m9b ju+KWS6cEcoYlfn3TIqQ2fkqNAcrqjbdj2qmGvqTzz/8tVrQ/MJajmEPagELT28tUgt6 8MiWmBlVdqR6iVm3hjeJ5I0UUmgdah3VNjCyfLDXOOUHxYlTxd4tyvYr01+jvbs87VNf xpYA== X-Gm-Message-State: AElRT7ERk74rHEyDHxM8xq74PZoxrZmv0UPaT12hMirGTvokF8SEfc/p lmpJ3i9I/vUNmZflkY8XMoR6Pw== X-Received: by 10.223.161.195 with SMTP id v3mr3573692wrv.184.1520771942134; Sun, 11 Mar 2018 05:39:02 -0700 (PDT) Received: from localhost.localdomain ([105.148.128.186]) by smtp.gmail.com with ESMTPSA id h188sm2569103wmf.37.2018.03.11.05.38.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Mar 2018 05:39:01 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.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-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: [PATCH v8 2/6] module: allow symbol exports to be disabled Date: Sun, 11 Mar 2018 12:38:11 +0000 Message-Id: <20180311123815.17916-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180311123815.17916-1-ard.biesheuvel@linaro.org> References: <20180311123815.17916-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. 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.15.1 Acked-by: Nicolas Pitre diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 8199a6187251..3a2a6d7049e4 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 7b3ba40f0745..896a882c89f4 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 1a1dfdb2a5c6..25005b55b079 100644 --- a/include/linux/export.h +++ b/include/linux/export.h @@ -72,7 +72,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 Sun Mar 11 12:38:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 131344 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp3055595lja; Sun, 11 Mar 2018 05:39:20 -0700 (PDT) X-Google-Smtp-Source: AG47ELvVAF+8hCJ62m9R3Os8kzRBQoExDkPynlzdZQ2t1FLLaKLPaLTGL8g0Dh10Y0qhawMoD9L6 X-Received: by 2002:a17:902:40a:: with SMTP id 10-v6mr4853026ple.245.1520771959837; Sun, 11 Mar 2018 05:39:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520771959; cv=none; d=google.com; s=arc-20160816; b=jsHnpI3bqC4AGQgWmfZIpaFoCAF6UOcugpB9IuWh5iVrHO/AauIJ0RnN3XOgmCY+TO l7Zd90WFptsGW/FnMMi0c0z7zIti78kHnhWDzYymU5Hwm9m3uw/G6gwxeUR2WLVH2T+Q xEDWLE+ScYPGZ/NUKMuNRubtHBn1RTXVdaUdC6xLK8eKxOIpiBA2Y2cIXCYFEArm8UuX VCLHqCFW43mURC8mhih97PdMjKQC+B2nojy/BmiSq4EK/+Ns8+ob2bQC1tL6KQp9DAs7 67ofo6kt3D9b6R0f85f0InrxeyllyP5RcyIGeEmXpQ7fDrtEes2gdDdM2X5wN3VRL+40 tgOw== 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=cIBQXEJp/WJqYgkyL2azlB0OGK67eNhC55dbvil4JQQ=; b=NN2t6lE7Smt4Lj3uX+un9iTnFbw86REWAC3lco/nU5EKZYO8R1wDgY5m0TD9fLtl6o l8GLl9/9A5nsZc/YYH0UaN0L+mP7LI3ODpDBJqFfxI6KWieSIOhrbwv3ehy1qpToZRTV ePfgtSs8PtKuvKlUSAg8Cl3xAZDMwgUaS933qIG8vqvoeMLwwwjs8KVY1OrzBDJex3WK sh9U8HdTeKcSc9H96sxak+3ABsvPz0DGilMPf1zdT895pPyRJF/ib9pDLzaaEV0iaMOx yXdvBHgNCoCJd/BpMSBpPz/E9Cdn/VjJ1F+8J1RUeZS64ay277xkK6kCG4Pf7QLsjBBS ULJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EuJsZEJ0; 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 d5si4238997pfl.224.2018.03.11.05.39.19; Sun, 11 Mar 2018 05:39:19 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EuJsZEJ0; 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 S932266AbeCKMjN (ORCPT + 28 others); Sun, 11 Mar 2018 08:39:13 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:34290 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932126AbeCKMjH (ORCPT ); Sun, 11 Mar 2018 08:39:07 -0400 Received: by mail-wm0-f66.google.com with SMTP id a20so9510889wmd.1 for ; Sun, 11 Mar 2018 05:39:07 -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=cIBQXEJp/WJqYgkyL2azlB0OGK67eNhC55dbvil4JQQ=; b=EuJsZEJ0OWBxCIzipBL3jwFr+sdpqkmXwyJU9csqFhrK/CKa4Sqmy0lB0LJVS5dmQX Usl8lfNNjbt4u9g6z0bHa1NlnPIO5cHF0Cy9DQDvXPyRKdvt+XQsXiD/kb4O7BDtykEf aaQTsALS19hjJdFXT1K//lbiHCDaNNXfv1vQ4= 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=cIBQXEJp/WJqYgkyL2azlB0OGK67eNhC55dbvil4JQQ=; b=Dg7J21uQJK4dyi/0ABNOcU0ILDR8e/5XHTyQimkO+P6Ixn7JBDqlJxRUB7or7hWsKg Pwrc/gilEBJjOJD28gEzEADYZFC3aqDbB84xNsdXgWDd3k3xHrt4YzvsNho8QncC7++l r5buMfJzflIiXHSBcj3tmprgE2sjKg4aWgFT4hzgjDvYZRzgnBhSmRHyS+BsXYVkjOR0 /3WlMrYEmnIpfZFtm3ikqPreGvm9UVapdUm576oB+3IqMwWoD2Mp1e+1MzA164IQaYGA iXkxcGNiah6Fc5pB3bxLwEEPK14EzTeH65sasSzU208ciOR0+W1ZE8LTO7kz9NY3NJeu 0WyQ== X-Gm-Message-State: AElRT7Gmseh+ryBwalwd16W9KHIeFTDx+TN60qt6i80K6L1AANgCEVz8 wrKS0GcVvvwKPz3OBkVrgUT5ZA== X-Received: by 10.28.108.6 with SMTP id h6mr3057680wmc.6.1520771946296; Sun, 11 Mar 2018 05:39:06 -0700 (PDT) Received: from localhost.localdomain ([105.148.128.186]) by smtp.gmail.com with ESMTPSA id h188sm2569103wmf.37.2018.03.11.05.39.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Mar 2018 05:39:05 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.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-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, Ingo Molnar Subject: [PATCH v8 3/6] module: use relative references for __ksymtab entries Date: Sun, 11 Mar 2018 12:38:12 +0000 Message-Id: <20180311123815.17916-4-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180311123815.17916-1-ard.biesheuvel@linaro.org> References: <20180311123815.17916-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.15.1 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 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 ab4711c63601..0a9328ea9dbd 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 * const __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 25005b55b079..04c78e6bfec9 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 { + 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))) \ = 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(__DISABLE_EXPORTS) diff --git a/kernel/module.c b/kernel/module.c index ad2d420024f6..b4782cfbb79b 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -549,12 +549,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, @@ -2198,7 +2216,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 +2246,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 +2300,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 +2560,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 Sun Mar 11 12:38:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 131347 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp3055986lja; Sun, 11 Mar 2018 05:39:56 -0700 (PDT) X-Google-Smtp-Source: AG47ELu/xuCBXH2LYPrQgreSJpOynh6KraFJeBXSJPdcV9EQY6+NVEj68VnP5mgukfFiKY0baHF2 X-Received: by 10.99.126.24 with SMTP id z24mr1511442pgc.110.1520771996408; Sun, 11 Mar 2018 05:39:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520771996; cv=none; d=google.com; s=arc-20160816; b=MA7rSQeEntTLytQSj7yr+LoBEyp3cqQzShBiclzxvKVqipscGW9kzSnCgDPLeyHv+h HhRO+tWRyiErBvqmfim2vo4TZwgOoWC2A3C4HqK/b5oeDZasi9zGf3CTM8wSDbvvW0Cc MkmCAQv8O3BVsJ9b79rxq3AhyjOIH53TOjXXcu2OxwsuyYZyj3okdDiKWFo+yPhCuRQE fXiTH3Z9br2L/D+V9xyJtwub4/7QMhpfanQqueCRZ2yl4r8VxJcIMLkRoTS2hcXomu1m DPUfpMrO5jrJ1wJPLqkEJCOyI0LCLQ+qc58xEN2NN/orUhXINHyOqMz2moB9nS12x/FA 4aUw== 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=A6CdKGtcFxvkcJyprZ73SqXJmQeUMwPknEJEYW8ClQg=; b=iPW00e8k6kW1ltw7SHl+SiWE3b2iO2OO00lr3dRY3Z8t/7uMJp9tJZSNpVZn+dCs74 MhCJKoiIRWZG0YMPINqE7DEyVbvzU1QO08lv41yEfyr/cL51sFDJ/Z9lYK6Pd8RAjZ5w B+UNXCKGlFJkdTK+NGRXDYXAkHnNaxCat524ACw54eEBb35MJvAQQBFFnvDiyU8b36lw 2bYX9leoO7nmkRe9iEy0xKTD6VS170lAofKs6C6lkhqyR2azGZsknP9IjxG7bXW41nAn EG92VFq4Q8WaZt9p5VllrwKUYf1i2uKes4p/qsp2hp9yd3vaWdcNL5JbFEzJrkwoUq3y VcRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HDlfrQqh; 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 q10si4171017pfc.183.2018.03.11.05.39.56; Sun, 11 Mar 2018 05:39: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=HDlfrQqh; 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 S932344AbeCKMjw (ORCPT + 28 others); Sun, 11 Mar 2018 08:39:52 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:37230 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932242AbeCKMjL (ORCPT ); Sun, 11 Mar 2018 08:39:11 -0400 Received: by mail-wm0-f65.google.com with SMTP id 139so11288630wmn.2 for ; Sun, 11 Mar 2018 05:39:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=A6CdKGtcFxvkcJyprZ73SqXJmQeUMwPknEJEYW8ClQg=; b=HDlfrQqhCKNaq81bcQzsRKbMg/kFKS4+WUFlfWP3mLAJ0D7fn0+BT5VP8f0NnXiG0y RjHQxTnnDpFm9AHfDdaCSxP1NdV5nP2tR+JkXE1RnBk5lt8sqndIQZCaxjElFMxdSv0H Yt4YnZyueus1wuWYfSgP2/JilnSM9JC+n0J5s= 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=A6CdKGtcFxvkcJyprZ73SqXJmQeUMwPknEJEYW8ClQg=; b=gl3TkHG23IwFtWIMek8ShOzKu1DD1qpupechsq3hQX4yhdB3Pl6L/lnoDeaevwBabV Hd5g7iUBeAf1VzM5qhqAhmf8fZqUkgNnsCCiOphFVpmxK7cMPvGG+dKmVrMUxZcuouR9 g7evQV9430moCCuoiBp0hbD6PLZX1El6ELD4XXxB8GPP7VU9VQp7t9vZ4Zf9UAJI52Dh oTjp0CUCOSIU9Js5/n177KWaqYVd/J5Q2KJ6HoUgCaL2//6iake9lXhXSgLMmRth5euR hD2hQ/zdjQOw8zKxRFSLVoyskCbTXdmV6BtbctM/RsnTVBOGFlAAO5HXQ7l90pD+PO+7 6l5Q== X-Gm-Message-State: AElRT7EK6oJW5p//lBMvTzD71Ll//vxlvn35WXc47A1CsxlYqS8mFrQ5 +DDP6jx/tFDalb8IU9LGw0+hSw== X-Received: by 10.28.217.205 with SMTP id q196mr3300150wmg.149.1520771950287; Sun, 11 Mar 2018 05:39:10 -0700 (PDT) Received: from localhost.localdomain ([105.148.128.186]) by smtp.gmail.com with ESMTPSA id h188sm2569103wmf.37.2018.03.11.05.39.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Mar 2018 05:39:09 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.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-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: [PATCH v8 4/6] init: allow initcall tables to be emitted using relative references Date: Sun, 11 Mar 2018 12:38:13 +0000 Message-Id: <20180311123815.17916-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180311123815.17916-1-ard.biesheuvel@linaro.org> References: <20180311123815.17916-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.15.1 diff --git a/include/linux/init.h b/include/linux/init.h index 506a98151131..cca0f76bf4a6 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 " 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. @@ -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 a8100b954839..d81487cc126d 100644 --- a/init/main.c +++ b/init/main.c @@ -848,18 +848,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, @@ -885,7 +885,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], @@ -895,7 +895,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) @@ -926,10 +926,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 fc1123583fa6..0be1e8524873 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2780,7 +2780,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(); @@ -2791,7 +2791,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 Sun Mar 11 12:38:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 131345 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp3055630lja; Sun, 11 Mar 2018 05:39:23 -0700 (PDT) X-Google-Smtp-Source: AG47ELv08+5ObQsSsBkYOoSUDMDgETwb4XBrfcRAuGaxV3XWkscj4DjIODEiWdZc17bBUA0zO9V7 X-Received: by 2002:a17:902:7088:: with SMTP id z8-v6mr4835200plk.174.1520771963742; Sun, 11 Mar 2018 05:39:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520771963; cv=none; d=google.com; s=arc-20160816; b=t2OyWDnneZc51bfh4w/ABQ8D0e0316xgSe4a/LP6u1Bm5X2riNxwTOC6dCoGx/30j6 ZeM+J/ZA85k0wSuCOxxVGI+fg68I2mD41Gt5fL5kvWgPSrADfasAEZ6m9n1cyQZP7Jj1 SAizMiRNQWXAvOLfZ58F7jZMV0DkLmHLvvrURJ3JXrisr1ejp2Pm1dspfukfVrZ2DWYA UjZPgpUixc6FJBf0llqQe2ywMIQbxUx/zgWpZC2OP0Rggh2uOBb0h08BswCcF7RLfyhz eZZhHaDNDyFNmCjLb2QMuBT7vqdbvbtXUQDgeu4yfbm6y0gMxtgeIsOpAiirZfCMF16a LDZw== 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=72OAPI9Kvt1iwKA3RSKe8xgQu1XKCUkEMrG/Hji532U=; b=xi4n4YLsp2SxG9HBtEI92Sxzzr63AloMpj234MzmHX27qPZP/3MjQXjvJusl24LS6O RWFj0sqXPWcD/5Qous6MjrZix9ZePO6teZp7zbelv0sxqbQ5xtHy5ndxOO073ypqgI/Q XS70y/8YT6UhObvrMVZB5CuHAJ2B4G8l6RPBw+QTLavtkJbFXbEP3HvwWssx5l/fZolq 6PBzNPNYSWaSmhgFumS6vQB5v59CWOYXQQGacNHx9KAdlUgTKnrafyiLRZkjxTWX301D ycrHffjj8TMfMsZhwcDWHKBQBDV5l0z8lPheAidOASWt65iQAhXEJY2/hkeYuKIgkPov kZLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=DXVoHVls; 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 x3-v6si1385175plw.269.2018.03.11.05.39.23; Sun, 11 Mar 2018 05:39:23 -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=DXVoHVls; 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 S932295AbeCKMjS (ORCPT + 28 others); Sun, 11 Mar 2018 08:39:18 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:33120 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932126AbeCKMjP (ORCPT ); Sun, 11 Mar 2018 08:39:15 -0400 Received: by mail-wm0-f65.google.com with SMTP id s206so9520413wme.0 for ; Sun, 11 Mar 2018 05:39:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=72OAPI9Kvt1iwKA3RSKe8xgQu1XKCUkEMrG/Hji532U=; b=DXVoHVlsex+jQ6eacasWEx6IPON3vb7ibDZgPBkHSmmblHnmS/kylgN5Qm7zERsRDc o2iVDcwix2ZWvHSj7RAZyl/ctiT0Vztuoc2bCNh5oAkTOqrmLNoGeM5uZMTvwJwPbjmy cid+ShSZ3Zmx0mFW+HI6jgPNNdB4WD1A6+/o8= 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=72OAPI9Kvt1iwKA3RSKe8xgQu1XKCUkEMrG/Hji532U=; b=maaJoAK+nEoHAZh7XnCkt/rSgyLFCvC8/a4vOn1Wqomb3Fx8jh+YRKc8h0efVQJtOw fjOySfovZdLVd7UOYcNNRDoCfvYefoVkZEjJrrjgfnhCZ5RFjDocMZmAar2sFYo/lnZF n3pZlewPc79XQMQIsZSybRgD6Zw97Ixvw+fHL83B5OJQ4Zj2YGe9dz8iUcaa9UgOaI5O PMfnsWIw9X2pV21pykfFlajvCm1wU7XqrRZrJ0/6jAIvRSpHqLJrCAfJpKmL6eh7+NYw Ctxv+T2lIl3nRiHJ3sZHM23T/el0jeqoWzvfFc4IbE65vs/zByIb/JxSTZnb+ZMvM4bH IwHA== X-Gm-Message-State: AElRT7Hm7CMwutzBh0EzdmUy0rcgfT+a9dtfTKyLSTM2bR8+MIvKuBs/ rGMFUi+naWQWMju/HlkWDkvEdQ== X-Received: by 10.28.161.4 with SMTP id k4mr2856331wme.103.1520771953915; Sun, 11 Mar 2018 05:39:13 -0700 (PDT) Received: from localhost.localdomain ([105.148.128.186]) by smtp.gmail.com with ESMTPSA id h188sm2569103wmf.37.2018.03.11.05.39.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Mar 2018 05:39:13 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.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-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: [PATCH v8 5/6] PCI: Add support for relative addressing in quirk tables Date: Sun, 11 Mar 2018 12:38:14 +0000 Message-Id: <20180311123815.17916-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180311123815.17916-1-ard.biesheuvel@linaro.org> References: <20180311123815.17916-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.15.1 diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c index 8b14bd326d4a..24abfaccd2a0 100644 --- a/drivers/pci/quirks.c +++ b/drivers/pci/quirks.c @@ -3563,9 +3563,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 024a1beda008..765044bb9e8e 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1790,7 +1790,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 { @@ -1804,12 +1808,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 Sun Mar 11 12:38:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 131346 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp3055680lja; Sun, 11 Mar 2018 05:39:28 -0700 (PDT) X-Google-Smtp-Source: AG47ELsEnX/aFEuynwtY/mKTYx/jaRDpGiirqPqz/OeQd8GgoWAVjFujQoAlRgx7vxbg29rsIvI0 X-Received: by 2002:a17:902:6b04:: with SMTP id o4-v6mr4821367plk.201.1520771968332; Sun, 11 Mar 2018 05:39:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520771968; cv=none; d=google.com; s=arc-20160816; b=Liyc63FIbGiX/h2r0BBfIkKfRgtVL7ki673VMLCxzADZNiGwXb52PTzR9BA9mAi3L7 Kee4b4xaMg+hk2hghIcFjzzRaQS9ouh0/BUPTDeh3jTxlPZNnok3eAgI34iPwwGKQTrS T3ARu+CCoT0f2dJjs/qkqupMGd8+LD5qVZGSRO66Du34VO9nEWiL2tMmsSZW/6hNhybm wIotjQdtkH23r9o/yfay1pD99EbkvffhAZRuAzL6ZaYIsry2Mq++MCG7FB4mx8aXuqFt Bm84e6RUNBTcdUZHx9DDfKuikJ08x25O0CbYb7L/2c9lT7uITK5AoYO5QOZpY/J7YT91 Kw8Q== 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=bbTSC6rTARnom9ckByI6VfHxqghKSu+vbbJy6E3JvOU=; b=glyoxQgi9IKFMXJXbD0ii/Od4My4zBoI3y2/LXTOroLrDWVm77d2v+YL8iiSVlNhb+ MHP0q8Ac05YGwTb9fIcX1jaDxdxFNkpd1EZf/yI6i8Ik1+PzvGkhM2r1f/R1pGcUoyDw 9V01FoVw3dJdwoH9ePBBxSMKKRDws1SwhB7FlL7USmAcqJBWt4+J0UK78+P+N6QqZxd+ fjv28/nh38j7IVQM5hZax8DsWUcJCzxQlPsZ5Qkndd4b2TxaCC52J1ZRtps58we1GfnZ AaUkjoTDDVHvylECg/Bl92GYhFt7lAPh4U6AOpZgQjBi4tIPajzoiYFeq5JjynGgL1CT pivA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=P/fjsi0+; 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 q10si4171017pfc.183.2018.03.11.05.39.28; Sun, 11 Mar 2018 05:39:28 -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=P/fjsi0+; 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 S932331AbeCKMjZ (ORCPT + 28 others); Sun, 11 Mar 2018 08:39:25 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:36161 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932126AbeCKMjV (ORCPT ); Sun, 11 Mar 2018 08:39:21 -0400 Received: by mail-wm0-f68.google.com with SMTP id 188so11242812wme.1 for ; Sun, 11 Mar 2018 05:39:20 -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=bbTSC6rTARnom9ckByI6VfHxqghKSu+vbbJy6E3JvOU=; b=P/fjsi0+pbLreU2S7U7w9l6MFak2naltGvAx9BZhwUVRh4mWBVqbAzyBYFDXd+b7nz E0QmVuMdTLMYDRG9M7X4+VCW0dm+dRx+fT+epfX+dc+kjyNqp3Ch3JXjkRCR/RgkHllU Ue20Y2byk0fEaT5T96/bqVq1344i14dMZo+wA= 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=bbTSC6rTARnom9ckByI6VfHxqghKSu+vbbJy6E3JvOU=; b=dJCM84oy3Twfz0DAsR1qmnrhFuqzZh565NcXdgoPSRLe+E3uX6Y/FiLkmLXE0bK12W c4OgTDbJwNppEdRlwPFNyJaChWP9nbg9X6Zh1vTvVH4sWyJ4VbGy5yiZRZHQnEnomNh3 502dzlcxmFSnI54Paqln6gBJaB8IKBZmoMMrtUy3n0YG730fL898qnOx4cy2Ltl+4cLh min6m7RxTd9J+HCSangK0BFAOmPCuK1dYtdg04u8y1JjJwsk3CrIpOQH7fMsHKbJP+bk 9yYR+xuRDTZ1heqoexCbokijm6PwkBZWZjbWSqHpTR4sGJPzux2rEv4yn0ZJHEjprVga We+A== X-Gm-Message-State: AElRT7EV5aCFHFJN5c+3WJg/JksZJrEoL2LKtd0tWlrQcvzmEZHUMHt0 ICZD1WNfbIh43gmcxw7HSKfwiQ== X-Received: by 10.28.158.205 with SMTP id h196mr2838678wme.25.1520771959268; Sun, 11 Mar 2018 05:39:19 -0700 (PDT) Received: from localhost.localdomain ([105.148.128.186]) by smtp.gmail.com with ESMTPSA id h188sm2569103wmf.37.2018.03.11.05.39.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Mar 2018 05:39:18 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.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-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: [PATCH v8 6/6] kernel: tracepoints: add support for relative references Date: Sun, 11 Mar 2018 12:38:15 +0000 Message-Id: <20180311123815.17916-7-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180311123815.17916-1-ard.biesheuvel@linaro.org> References: <20180311123815.17916-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.15.1 diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index c94f466d57ef..cdb0a9461d71 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 " 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 @@ -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 671b13457387..4ce8cc4bf2c3 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -326,6 +326,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) { @@ -390,15 +411,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) @@ -449,8 +464,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; } } @@ -502,19 +518,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