From patchwork Wed Jul 4 08:36:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 141026 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp543418ljj; Wed, 4 Jul 2018 01:41:29 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeMEWsN3iPNvVMYyK/mwX5aGlfekc+M8rDrXcbOgOHJGt1Gt1dO+PZF0WNZ1nPJA65g4gsr X-Received: by 2002:a65:61af:: with SMTP id i15-v6mr1064488pgv.349.1530693688996; Wed, 04 Jul 2018 01:41:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530693688; cv=none; d=google.com; s=arc-20160816; b=J/b73ZUeXT/CkE4CamOlZFJUGEYSlbBJjepYEowWS/Hlrl9p6vTeUJDjoVd9Q2EItj Hd17gHJyW2/JzfRgJ9TyUVAxiRgfgdNvgnk9urbW4J60zcN5YjBTwK2rbugvBogzJw1B 4isiFkiyRzxuGAG3d7BNM0Tqor56BulzkOv9jzLdJFklPd3uTkRVv8Egu0KE+igWsfhK e6cO7qCop+1VdMyBn4YNX/dj/dKq4J1djdyFvKq3z+HobVI+6wH0FMxhlzTn+bQuqZFF nEyuYnMfLrwfDKyq9U9i6cArFsS/R4lSQAwhK2WDPbNgm/d0isFRhihf+zGjnG944ss+ Dwcw== 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=FvlJovxgJJ+QheLrbv7UTmXIJsBE8E1kyVYaaE1/yZw=; b=KwJHprjYUiDSuRA4YFUWSDdPc6uUkqv8bHHg7cpTTMVs4HTzls/Oyd1ieoXlSE6e/K fJ6KtoEvVlV4U0pFOVGg2/2lX8OXVwtmFCdQWjKRjHwlo+0aRq7OPEz0C7m1aDhojcUy AGG2ik2aNrISrYaLAB2CiuvavbiABACgFOngmkhevj64d/+6JMr7zj+RkfhyTnO/IhOP jEpKmsN1BY+3hN/gjyWHn1JCF48Qug1IcuSopaMygMK+tTV1F71I0VL079ybK+1zfeuW a4vS08P/gn/aNOURClIdIi9SBbIbilpwrxjVyVKW6+DgkuJ5eU/BnzR3OATdxBtjUz0l 5zsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SRshl8rh; 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 u6-v6si3149899plr.437.2018.07.04.01.41.28; Wed, 04 Jul 2018 01:41: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=SRshl8rh; 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 S934388AbeGDIlW (ORCPT + 31 others); Wed, 4 Jul 2018 04:41:22 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:34104 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933050AbeGDIit (ORCPT ); Wed, 4 Jul 2018 04:38:49 -0400 Received: by mail-ed1-f67.google.com with SMTP id d3-v6so3477385edi.1 for ; Wed, 04 Jul 2018 01:38: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=FvlJovxgJJ+QheLrbv7UTmXIJsBE8E1kyVYaaE1/yZw=; b=SRshl8rhluInJKzqhBxvquTse209GvHLTUrF9aCGlZ6nEHaH1VTiPTbYPXUcbCb+a+ CanuIV+YSPI9fQGaPxCA6y/sZ6A2bNAuuBf8kf+E/rTCjsu7IFInTnKFhmb5fWDdU8AV CMe2EYDfFpFvPQAAN1eCohJQFn7LthzXTVMpY= 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=FvlJovxgJJ+QheLrbv7UTmXIJsBE8E1kyVYaaE1/yZw=; b=QbnYNddRhGNcOCBe86PbY6V6UOc7L0U4bO5wSkqyIpICOrOMtGu+9ycZK7ELeeyzhl xqSrrmcfXNUYdaeAK2hG2j0DyPQqO72BTQ/wax931RqGeMBPupdsUHUoAU2V4JUE+QJc atqno9bcH+rlBIB6M5QGOgagOVorX58WCWUC24gvhrtmsfAI5fVsHIM80FdLXvljBeX0 gQJjmOPvVKeoWEcr1M9trk7sXL2If+KX80bQTEuHbD2OJDwUyyKudNlJErlbK5ujO1Rv Iv20jsSb0cZtwM/jF/k3+bQHAoK0qpEDQ26tZbsTm7wWJKaB1NbbVeMa3ETMxrgVBsGA eq8A== X-Gm-Message-State: APt69E3dpU/+v3JNa7OTbelsI0XfWYoFa9L9FJ5D701OfZB+XhTTAIOK BiIbM+fHWNRA/W4yhPlNRkXd0lJ6QZ4= X-Received: by 2002:a50:d9c7:: with SMTP id x7-v6mr1722213edj.95.1530693527904; Wed, 04 Jul 2018 01:38:47 -0700 (PDT) Received: from ards-mac-mini.arnhem.chello.nl (dhcp-077-251-017-237.chello.nl. [77.251.17.237]) by smtp.gmail.com with ESMTPSA id k13-v6sm1821740edj.55.2018.07.04.01.38.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 01:38: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 v10 1/6] arch: enable relative relocations for arm64, power and x86 Date: Wed, 4 Jul 2018 10:36:46 +0200 Message-Id: <20180704083651.24360-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180704083651.24360-1-ard.biesheuvel@linaro.org> References: <20180704083651.24360-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. Acked-by: Michael Ellerman Reviewed-by: Will Deacon Acked-by: Ingo Molnar 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.17.1 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 Wed Jul 4 08:36:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 141027 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp543604ljj; Wed, 4 Jul 2018 01:41:42 -0700 (PDT) X-Google-Smtp-Source: AAOMgpd2j96qr4U6dO+pRB1fVD9LiuiWMNQrjgIDt3TfJ5CENy+Ll89YRBHHMuxbMipRo+K4Wifs X-Received: by 2002:a17:902:e00a:: with SMTP id ca10-v6mr1203497plb.224.1530693702637; Wed, 04 Jul 2018 01:41:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530693702; cv=none; d=google.com; s=arc-20160816; b=DLsj8OGq1qfaRQ3LBuQ2wnTxX8ypUZbh+ISI6Nj1L9pGn7wFL+yxpCXfASvhHfiKjf IHwbrbe7r9gnvi9DUquQIN7LjNtH4OxvAcpsvA4LnfbkSmzmUnRSvhX+9vPxUL5T6XEp CoQMgDmdsw1p0tivrAmxPRVTNn2hdbdKSJu8AgfImOXTWehQ2Nf/pUmEqxuJW2LB8OWk 6TbeKh1uhOYAoE1/N8Gkznv9tVDQ37WfxSV5l+n98vpIED0bAFiwKMu1fZlF76ogjGRp DgeubNN35RsjTz8FECllr5mx/E7doOCcklfgcu/1jCvvCauyq3RIXYRRWcxvuQrxh3nV sLiw== 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=WwDzIwe447IcZCAIGu+8yb3n66DKqmjIylDR9Nx0qlM=; b=MGFkbqYZf0b6IpDt8mCOFLJKh5NuQF3taCnrdN0KS5XZjYshEX0thPNsD8yD40Ao6f C1OWMfOBPGYtM/BUCK29Jp1Y1xiyn5uaLgX3gyEgvYkQsJ+hKJWKkaomuEELaBcaM8Jl w6BKxs+sPG59MGfgg7oS3Boqbcrx8ww35KwBOjwRAZUaBcih0NqGZZVFPEvaq2SDumyM xc80POhVV9swIFB4L0uSJLzbpnLcRoyqna7xCmhJGXo1Ia0TwMQagrn7GB4FL2LvQ101 eWUawxt+aIEX6mHVkL8m2NSTBs5bTxrXCP4ucA77tuu3Vcim4ojA9REuhIqW52kWgCi7 9CRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iETq0ghz; 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 u6-v6si3149899plr.437.2018.07.04.01.41.42; Wed, 04 Jul 2018 01:41:42 -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=iETq0ghz; 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 S934346AbeGDIlU (ORCPT + 31 others); Wed, 4 Jul 2018 04:41:20 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:36936 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933324AbeGDIiv (ORCPT ); Wed, 4 Jul 2018 04:38:51 -0400 Received: by mail-ed1-f67.google.com with SMTP id v22-v6so3464603edq.4 for ; Wed, 04 Jul 2018 01:38: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=WwDzIwe447IcZCAIGu+8yb3n66DKqmjIylDR9Nx0qlM=; b=iETq0ghz2rcIjYDEH/K2UNHIhKkUiwML5a0df6hgI4a7Nx4F7NBPxcz3ngH7Y9oa1L L44DDfe5KlmSEncVAizmGhIjM0rtsKzkUH/13i8+M8NTQdiXYhV+oHUV00DqiUUisZYU aNvd6P4F8rohT+ixA21/ID5MCu88JTnWThcsA= 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=WwDzIwe447IcZCAIGu+8yb3n66DKqmjIylDR9Nx0qlM=; b=rd4Ecg56QfvMBdUsvsTBF+zkfrNExz8AGJRNSuQJ4uJxm6bTdUsu+LnFMs4jTsn1Yc Dp38GHYN6QS7gLKimYW3Y1UqM/wUfRN6L9jqtIPO8c52Qv1HYA94+NHP18FoU4WyR9bp 8Lp5b1NsByzFPZBDT7P4jUW+YBFN/B6hRtyHwSYugwycjKdiR/ROTylX2RJ/N6YSAka/ LlQOfRJ9OBKEAMLYwkIs+dwpIUqiXtdyOUjwRH7THyIsIhH2l3RiNdXxA9suZQT1CH2e /3oBFyEMUXoUf4H+bOUNxK2R/yyC0UGGCikGWMNyqvwkjwS8qMnBE8jG8upCtf6JJ6JW rQNw== X-Gm-Message-State: APt69E2ZiUpsxTENvpgSyT/ZcM35GEA/S1uI+Qr25jppFnR/qo3PCEBC F76R3OnJlqPBlrTekS/ixhpTKtqcNg0= X-Received: by 2002:a50:81a4:: with SMTP id 33-v6mr1701948ede.200.1530693529463; Wed, 04 Jul 2018 01:38:49 -0700 (PDT) Received: from ards-mac-mini.arnhem.chello.nl (dhcp-077-251-017-237.chello.nl. [77.251.17.237]) by smtp.gmail.com with ESMTPSA id k13-v6sm1821740edj.55.2018.07.04.01.38.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 01:38: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 v10 2/6] module: allow symbol exports to be disabled Date: Wed, 4 Jul 2018 10:36:47 +0200 Message-Id: <20180704083651.24360-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180704083651.24360-1-ard.biesheuvel@linaro.org> References: <20180704083651.24360-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 Acked-by: Michael Ellerman Reviewed-by: Will Deacon Acked-by: Ingo Molnar 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.17.1 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 Wed Jul 4 08:36:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 141025 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp543302ljj; Wed, 4 Jul 2018 01:41:19 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfgXvp1J98bmOXBFrxiFNyKbfqRQOczo1ED1LLmBhup0ii+V49vfeAy/sblMSHL3cmMJQs1 X-Received: by 2002:a17:902:8685:: with SMTP id g5-v6mr1206295plo.180.1530693679588; Wed, 04 Jul 2018 01:41:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530693679; cv=none; d=google.com; s=arc-20160816; b=JZrrjTfgxb35XW1ZecBPoiwJMxb80T6ZLzcLS8+xmvzjUqUT39++n5+GbOoX6yenLo tC78vTZigyrwBb5buItkQNRNygzK9S0RpESLnGn1XEofFAbq5qdqZhT32rWrbxcGMMY2 oNI4iVE9G8AWGxlkTrBANM5mB4r9Jck0dBbNuzeid0OUnk2FrgiY+E/vb63qrq/c0+Hk eKsVQEBpW83yK5vgdsTVFjeEAE5j4TAMxXWxPXiWU+YwUSZalBuMOZXsIz039cNtEQvp jQECThVTQzS+h6/0HsiCxCnZ9PHk0RGqt8cGqUuW/spmzXBUDQg5ODWwQMe6BiPH5oln 1RHA== 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=pUE9H3GCn9iSfvk1RDpcZwwUduNhQwB5+rtnuqBcPGk=; b=GG7L1sZl20g43xlaSBIaIJKtaehSYDYwMqZkrvovmMYAgY+pYnGP6rl4Hl4BgCQB+1 Xs26Yo9KmWpKoMGUZFt8kZsquhno2PP6aPbfUf9I4H/sXTdJsMBEA5bw03dK9ytSjyj0 2Rlq/zVUM7LFldOpOKwJX0OTv0sBc1tZZXyN4MIGhpnDODqiufWcLIDIcAOrFAOJfnZp Q5jzvv2Nm2lxQjs5EutljwZmeziFNO+eDCCJXxVBOF9+N7vpg2C1/ZZ6bfQfgW1j1UGt uYMksk8vhCZfc7Duea7QvwFpALT0u6V6UpilUjz85uvwPbVM2ugTlZNcQwzx6z1nj0mQ WXlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=X3uDfZW8; 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 u21-v6si2896156pgn.86.2018.07.04.01.41.19; Wed, 04 Jul 2018 01:41: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=X3uDfZW8; 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 S934312AbeGDIlR (ORCPT + 31 others); Wed, 4 Jul 2018 04:41:17 -0400 Received: from mail-ed1-f66.google.com ([209.85.208.66]:45284 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933581AbeGDIix (ORCPT ); Wed, 4 Jul 2018 04:38:53 -0400 Received: by mail-ed1-f66.google.com with SMTP id g15-v6so3454801edr.12 for ; Wed, 04 Jul 2018 01:38: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=pUE9H3GCn9iSfvk1RDpcZwwUduNhQwB5+rtnuqBcPGk=; b=X3uDfZW8JGzwT0gcPaTCVAzyWnaBbfdA+tyfmAhPHrbvjMWLjtqZSUPM0juQjNAy+Y 9DEdHqj3Ac5V1aqcUdRsmXv5llfPxyIstL1ztgQ9L9tLrln8Uy6LziX1fH3+HqsYQjqH k7lz49W6GubQuB2C1Ry0u8xcybFWFgavNFGQg= 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=pUE9H3GCn9iSfvk1RDpcZwwUduNhQwB5+rtnuqBcPGk=; b=W9BujuggZ5Y0ByHX8lP1gLnkLW7WjyXZzqYdp1NASutkBpLwNSGfYxWgg1h/IR+tKK gCYdhX6VXmi6X6ZBNHR8ellz2xKwHd0pvE4z0XAs8zFI5YGJEmFy1gNIibTPLgy3ug2Y DPNRq+AHAuN9is1uzQV3Q+db8MotWv+hF7K8qbV1g3qfAKasdjJVZ11cQ9zS99lY3UR4 whAr+jYU4L1MBaSGQ+6TYEsxLYBHNQExCiY6t5IJeM1q3QQ3fzpFEYjCPanOmv9zJwjF nDOMeHsGWcVQYtoMGZg4s3/MKVuV6Gbat+BYE4ZaXZT7GcegDHlByiOMIfeXyavB3wiy +2Cg== X-Gm-Message-State: APt69E04XKmq3sRG2nMpP/hg24bdl1laT0IyXUh8pp9/Ey/6cIAa6SXZ /9ATBBRBAgh2tv6/T1GYL2ySyWxYRcU= X-Received: by 2002:a50:9226:: with SMTP id i35-v6mr1679190eda.27.1530693531282; Wed, 04 Jul 2018 01:38:51 -0700 (PDT) Received: from ards-mac-mini.arnhem.chello.nl (dhcp-077-251-017-237.chello.nl. [77.251.17.237]) by smtp.gmail.com with ESMTPSA id k13-v6sm1821740edj.55.2018.07.04.01.38.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 01:38: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 v10 3/6] module: use relative references for __ksymtab entries Date: Wed, 4 Jul 2018 10:36:48 +0200 Message-Id: <20180704083651.24360-4-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180704083651.24360-1-ard.biesheuvel@linaro.org> References: <20180704083651.24360-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. Acked-by: Jessica Yu Acked-by: Michael Ellerman Reviewed-by: Will Deacon Acked-by: Ingo Molnar 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.17.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 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..61c844d4ab48 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.addressable"), 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 Wed Jul 4 08:36:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 141024 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp543283ljj; Wed, 4 Jul 2018 01:41:17 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcSZo8N4pwh3LoKq+PQsx6KFGhcZEsKM9PEzFixISbFGdeeL12/MWWMl3/9hpsQ/EL8UnqX X-Received: by 2002:a63:4857:: with SMTP id x23-v6mr1114415pgk.30.1530693677729; Wed, 04 Jul 2018 01:41:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530693677; cv=none; d=google.com; s=arc-20160816; b=WJGNYiX9Dj7viKw0453AA6DdURnoJQWgjdMc+CiJvSRNwozw0Ac8+M1FCWxjwuQ3CK lx5AWNqPdaazWDDNHlAZmhCt5LZnc2+bEvjM7tPHxwY/X0dV+abnh4bIUzZazRwFwZts WdXPPxO2MRURwpa2v89ikD5WP7q5Ovd9952BOU3dxD4uNWGZRTWVozyKDM8r045IJLIY 5DjrD/2QrMsn7RGuVHj3SJPOqCvbgZHcZdxSa4+kH0yZceKRx9ruxkRbtftNyFaUeNS8 WnzttfJ40nFfQ0o2YjmYYvCaDAR1ha/9ERC+3OnU+tUni7Qg308w9dHQHndjjdsSUi1z vpqg== 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=p91lF12qdUvsqqUYIEbuayY71r/VOTp9g79ZDtfXMw4=; b=dfjQR5v1RmP0wq00ctZLVKjuIawGRd3Dws/Cs9drIMiTu1hvresJxitxyEb7Hcc5LI 84txOKb+E8MDsxFpzTJufHLzM8KTszGwZ9t6comhgv/DslgKvnUpRwhWENSUh1MpifR0 OWQ6mkl2GF8ajHqG89Otd2zruk9CX7WwM9LGPTber6W8ETruFXn3jvKWqSHm1KugxWTU 5Af70BmAcEyxDv3N6LHS5H3Bone3TYiLsH5Alt+5hsovpjpnOQe6KmbdDs3jD3WRqyw5 xfv//2KnMqx/oMKtUZoYwA4HT8hqO64sOVtxSJwiMzUPL7lbCAUwVrfsvkD3luJ7GzOr 9SIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kgpSJneX; 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 u21-v6si2896156pgn.86.2018.07.04.01.41.17; Wed, 04 Jul 2018 01:41:17 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kgpSJneX; 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 S934235AbeGDIlO (ORCPT + 31 others); Wed, 4 Jul 2018 04:41:14 -0400 Received: from mail-ed1-f66.google.com ([209.85.208.66]:34112 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933594AbeGDIiy (ORCPT ); Wed, 4 Jul 2018 04:38:54 -0400 Received: by mail-ed1-f66.google.com with SMTP id d3-v6so3477576edi.1 for ; Wed, 04 Jul 2018 01:38:53 -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=p91lF12qdUvsqqUYIEbuayY71r/VOTp9g79ZDtfXMw4=; b=kgpSJneXTy3GRlY2BU7xjQ+bRlydSilw80tXvVnEVaWwXQI+TFsnpzI0JyI9LBD98Q rEe3fOpCQYrHoDDfnTTwLOsvJB+d5nPG7lPNq1tpVD10Ef1cKgEVU2W9i5hCEpwSqoVB 64fmJQGBVdEfTVY8Fnpgs1HzzqFjctVDmUD3U= 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=p91lF12qdUvsqqUYIEbuayY71r/VOTp9g79ZDtfXMw4=; b=V5pBjITg5x3CH0l4XGej9azb3lm0IYi9ssLYPbN5z90gVacEypdY2hwt2bO84wNCSC zVfG3kfmpC/15heqznExPw4Tv0PIFydarq4sivMAluOqRYA5hvW0iMoH4sbaS9lTUWW9 Uuu5MdAyYuyCORDY1SxY0PP01GtqPyB8SDZndDsOL0kMsapL7Sbng03UOj/uK3VOjtwu dfWLWj9QmhoWpGOnJhayeNjJJD0rMeSNONDA+5xSbV6Z5PsjpaTvjPjHuzYh2XAD/t6a tfNp0RTQvBkwIbO8czg9Ll282RB+VTUMf18jap6QFX35gq+9taRh6GpvOETn+ft02QQz jTqg== X-Gm-Message-State: APt69E1dS+YrgX09dHd1NG2BBSHCgwpnhVxo+DU84pOnhH7ZUAq9HIjB qIvUgohZJ1ToIoApjydxmRJmQFrYcSY= X-Received: by 2002:a50:f4d2:: with SMTP id v18-v6mr1758005edm.162.1530693532907; Wed, 04 Jul 2018 01:38:52 -0700 (PDT) Received: from ards-mac-mini.arnhem.chello.nl (dhcp-077-251-017-237.chello.nl. [77.251.17.237]) by smtp.gmail.com with ESMTPSA id k13-v6sm1821740edj.55.2018.07.04.01.38.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 01:38:52 -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 v10 4/6] init: allow initcall tables to be emitted using relative references Date: Wed, 4 Jul 2018 10:36:49 +0200 Message-Id: <20180704083651.24360-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180704083651.24360-1-ard.biesheuvel@linaro.org> References: <20180704083651.24360-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. Acked-by: James Morris Acked-by: Sergey Senozhatsky Acked-by: Petr Mladek Acked-by: Michael Ellerman Acked-by: Ingo Molnar 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.17.1 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 Wed Jul 4 08:36:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 141023 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp543265ljj; Wed, 4 Jul 2018 01:41:16 -0700 (PDT) X-Google-Smtp-Source: AAOMgpefJv0BOy3Ytah5rTi5U3ved2NneLk76v66YZarHHZs4n2hLR93SF2JNgcV13l4zE+0Q9ef X-Received: by 2002:a63:1e08:: with SMTP id e8-v6mr1042242pge.281.1530693676452; Wed, 04 Jul 2018 01:41:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530693676; cv=none; d=google.com; s=arc-20160816; b=j4YRnmhSgXBRiPf4RU/2zqSU0mVHMzWLYnNySPyQQULMXPWVg9SKnLFx6fEl3VqSqb v1rTw+XJpntGHGx6OgsdCVTt2n8rXdKYd1V6ydpzaOiIZgBWQ7W2rSeiIo90H50mpnnK xkT7D+xXN/4MEqvx3hdcwdDhmmORroEpxgB6UH8XwBmgMurOIceu37VWfK0H6XblYoqX zdjEhdDwGk4Zmgl4BS3sSTb/s0+TOAFnEfoZ5BxqKUUg0igOo1gauFW/aKBLGsINQ7XS pqeslExUhP6HdSL4BNAdrTRLSr1TgvSk66OjmSExjTRkIVlwRAzRVgcNTNMSwv0TuzGY EPPg== 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=sqbAdR8mVhPUbhBZJ1W38BSLp0pkEn3sNHW4MKbPLbQ=; b=jwfGmONHX5hG7TL1AVKXhEd3xD3b/6GgmYkiziwOouHmsPrXup/plWPMuruIdDtMRQ O3gaXbHOy6TBboIrlACjcqj/EJtsQqYPUTuEEa5TH8dhlw/N56JMG0oSutlQJf52E7ki BNBBDO3EhRNlOz0fsK1xw9sdboAXz9slIjN/1xNPoi0RIJoJ5AF/4y03Bj/TeCd67zKi LqrG3GzV+A1Epaq225lNBZAES2mROynoGsB0ZsViZZJZSKCObGyb41OCEsasUTy1HD++ Hnmif+3IBnbRDqXLPWB1Q+8GOIT+YQJzIJN3BzsJQvM1ztz70mALUQkq+U3WORK0Qx3v 7z/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IDNcrj1N; 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 g92-v6si3135469plg.0.2018.07.04.01.41.16; Wed, 04 Jul 2018 01:41: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=IDNcrj1N; 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 S934129AbeGDIlM (ORCPT + 31 others); Wed, 4 Jul 2018 04:41:12 -0400 Received: from mail-ed1-f68.google.com ([209.85.208.68]:42218 "EHLO mail-ed1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933653AbeGDIiz (ORCPT ); Wed, 4 Jul 2018 04:38:55 -0400 Received: by mail-ed1-f68.google.com with SMTP id g12-v6so3453102edi.9 for ; Wed, 04 Jul 2018 01:38:55 -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=sqbAdR8mVhPUbhBZJ1W38BSLp0pkEn3sNHW4MKbPLbQ=; b=IDNcrj1NlIQfgGHybPnbkoQiCFRN8HfZ/uGEfeoFAbZcFwUmSNtrr3SBeVTsgY/2q4 vA7HY38Uc/kmvDOwTlKRcAQl1tuXDYF+ZNH/IytTXD6s//FRTSj5JSW+gdEHE0xX9kNT xE3EfGwkeGYMaMfn4Qg1RFs0TgS9XKCnTI4Cg= 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=sqbAdR8mVhPUbhBZJ1W38BSLp0pkEn3sNHW4MKbPLbQ=; b=q4c2X8+OKjHZrE0uG5JrqPIaYP1qvTQ3OAi0ucK8+9LXJPGhZknHuRaSnKHfPYIO7Y LPCtNrhF4JSFmxTroeBZniZk0ELlYq6tj2+pOSSQcj5L/4e+Ly8bkdO64zj7eu5c6lIT rDW84csYcAjzit9kKn7M5ZDNss2MzMwjo5/mAsYK3LSC42rZf3GnQHRxtttKXG9LDphS 6UltMG5DSfaibA9ni5TS3tZF1LginglgQngZrWQlI6KHY3bDp8aLA81MQQlsFPEF75Ux awHGl+mP919vD70JqcGnXTo4+S9nPK7VOpYdS/XvqubqSzm1FWmC8Lm0DPKKX3Ibohpt f2pA== X-Gm-Message-State: APt69E2c4iA+np95IPkNLQGZ5/lxVQpgX1/2znwObv+RUrE8c94j8nDo H9ThVxMoWtG+RCU50V4K0NmfHfx4yb0= X-Received: by 2002:a50:ad0f:: with SMTP id y15-v6mr1715278edc.78.1530693534421; Wed, 04 Jul 2018 01:38:54 -0700 (PDT) Received: from ards-mac-mini.arnhem.chello.nl (dhcp-077-251-017-237.chello.nl. [77.251.17.237]) by smtp.gmail.com with ESMTPSA id k13-v6sm1821740edj.55.2018.07.04.01.38.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 01:38:53 -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 v10 5/6] PCI: Add support for relative addressing in quirk tables Date: Wed, 4 Jul 2018 10:36:50 +0200 Message-Id: <20180704083651.24360-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180704083651.24360-1-ard.biesheuvel@linaro.org> References: <20180704083651.24360-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 Acked-by: Michael Ellerman Acked-by: Ingo Molnar Signed-off-by: Ard Biesheuvel --- drivers/pci/quirks.c | 12 +++++++++--- include/linux/pci.h | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) -- 2.17.1 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 Wed Jul 4 08:36:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 141022 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp543236ljj; Wed, 4 Jul 2018 01:41:14 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcmgZgbibcgjMvMsaTR9aPHq2GbmT1a679mveZtyLF6xMPtEfwBN0ukIIlGY1Bfh+JHXixY X-Received: by 2002:a65:58c8:: with SMTP id e8-v6mr1069585pgu.96.1530693674151; Wed, 04 Jul 2018 01:41:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530693674; cv=none; d=google.com; s=arc-20160816; b=TlpsT8d1As8pVz8qMwBkE4mNtKAru0u/A9RA4x9688W/kAD6z6BzsjknRwv6SfK7IA z6K4uQqYZvWyLxXNNGu0cLIPs33gIP7H533kF4ysRJc5uG6edvWoAp2bsztUiBRFQGPu NdnFTHbbmDV25QGczc1I2V4/HAjC0yWad8QBLfoTqNm6/hYOq5KX2P6EP62Pp+bZrMlV p2FxvLtV6Arozj+B6DyNb7bdh02PhGg2kEjaEbJY3xZMPsHku5iXTs+/xg57RiCuVw5D HFXLwCJMiCr/++mPkEwCI/cAH4gPlP6SW9ss3CZ4o7ZeyElJyuxRP7YU/kOmYdYdnYSY 6GEA== 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=sNQ200Dbn8i5rABOHA2COrJbJeN/1Dd7TOxo9J6iksY=; b=VzUG0QW55aFRHR2anl413w4VB+8qIJgQm2a8i6ezQv3OmxEQYgFnWCBBdQHya0WNNV JEpTQjLJlw63yR4j4qhwk9g98xnz18MKnwqKYyJm6dtqHcmz80ID0gLw9ZdnH0SxCM/T QQ/XOR7tSLxDYkqMmxqxz9/bGY0c7gCcTy2xt1r6Xl4DWR/hu7gbu9O6MRcimbfMv4ua o7ypRxp7KMecNzDIfnOTftV+BYHY1WsKXXxDLmVGC2rTnSApK/Pxs0FlQqef9fZi9lwR /aB9iGvRR9EFMBCcbOTSXNj45AKn3p3YfwHImHo6CTx5GovGE5mUxXROJ/Lvi7j8i5Ug WL9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=egozONIv; 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 g92-v6si3135469plg.0.2018.07.04.01.41.13; Wed, 04 Jul 2018 01:41:14 -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=egozONIv; 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 S934066AbeGDIlK (ORCPT + 31 others); Wed, 4 Jul 2018 04:41:10 -0400 Received: from mail-ed1-f65.google.com ([209.85.208.65]:36457 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933741AbeGDIi5 (ORCPT ); Wed, 4 Jul 2018 04:38:57 -0400 Received: by mail-ed1-f65.google.com with SMTP id t3-v6so3468270eds.3 for ; Wed, 04 Jul 2018 01:38:56 -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=sNQ200Dbn8i5rABOHA2COrJbJeN/1Dd7TOxo9J6iksY=; b=egozONIvLTNRF14bkHiz1LxWn3ZE/RmJ9/KQP6mYSoT+Ck+jy5MGj1XCYoSQL34Yuz rD+btanjtPcwVK+bbMJRJvZpW1gFOruSWbwSuvLqcF8PnQ5dpsyYhO5mGNA7NSxuVjk6 iZXERhQaBbe3acJH1sm1UlXrI4aOg9dEpF7DM= 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=sNQ200Dbn8i5rABOHA2COrJbJeN/1Dd7TOxo9J6iksY=; b=kCME4ClF5x+IdUylL6BhOnQCmKWiDOqp90ruwWIfEozEpDw5eGwt0/W9FHFyCyqym2 3jepYPZsbb5k8qLJp7S8nXhyO7TqZmnwNUtRCNbMX2g+Ak3mK0qbd+W5fe+s3q3wHXlH ZckZpe8y75iL4jSj97SqpSQ9ewzCX3nCWDo76sLGk+gFhrRmM5MPMxbGqNAtyhUh0+Jn sBOV76jS05CAsKutCu/HILSSmewVM4MmJXUYoF7yZdpEq8ym1D3gYW521PLPh2qt2c8t zDFbg/ylE7+WFOWdGcW6tG8myoEKXK7yyh9U+pUHCqGkD/zT1lPPuvuhZIB09ceOOmmp 7Kxg== X-Gm-Message-State: APt69E0u9xP2qPg0P6Ts0zgkZOoQ2fykLem2ZevYoICKkyOqxXwan9Wk 9QtQMbYJADMTL7Dm/35dks8+19tHD8U= X-Received: by 2002:a50:da01:: with SMTP id z1-v6mr1734704edj.62.1530693535996; Wed, 04 Jul 2018 01:38:55 -0700 (PDT) Received: from ards-mac-mini.arnhem.chello.nl (dhcp-077-251-017-237.chello.nl. [77.251.17.237]) by smtp.gmail.com with ESMTPSA id k13-v6sm1821740edj.55.2018.07.04.01.38.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 01:38:55 -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 v10 6/6] kernel: tracepoints: add support for relative references Date: Wed, 4 Jul 2018 10:36:51 +0200 Message-Id: <20180704083651.24360-7-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180704083651.24360-1-ard.biesheuvel@linaro.org> References: <20180704083651.24360-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. Acked-by: Michael Ellerman Acked-by: 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.17.1 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