From patchwork Mon Aug 14 10:52:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 109977 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp4170445qge; Mon, 14 Aug 2017 03:53:12 -0700 (PDT) X-Received: by 10.84.169.3 with SMTP id g3mr26970022plb.136.1502707992710; Mon, 14 Aug 2017 03:53:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502707992; cv=none; d=google.com; s=arc-20160816; b=iKONShAlC27w5z2ru2ytDQdf8dEoBaXChPgmPx5hU14VNdK3nZBf10OERv7mVa5qF1 99rkdVMWrpok13r7scgd/PCK0e/9zURDJHWPtcrgtxW+V4sP8l/9vEY2+Gq7L5tfcE/w VtiTKfA7eWneKiozC2xP1JEWm2KCqL9eNpLA1YuSA24PHif/+sw4vKGSN8Puo7z64pna zbpGeNNqmrzH8aL7GwvWrfDs5CGo30m8Y8CXe62v4O3diRVOdsHkfzqH0eK0WKELAH13 2p2byKJfXkq+KZ52qRQFjcoch3YGYVoT5nGGd1jYgi9tMja3v/A9u6u6n8kwhC9WsLTS ucMA== 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=E6kGreoUgUe/D9Eg3bcGirQ1NSKJ8qaYFFtGJuuuVEk=; b=BgDb6fqGsT97LVbumIXMK/3mdiapwtQDIoWMDjYLnIsD05LpT5ZWH2GLcxqJPjQvJW qY9BfHQH7n1MtjGJFgMGfeVRihgzw58Xg8yv+Lyef73NjZ29fwy2G814QGCXLoEwN8/c D1CcPlggSh4WbyL0bvP05L9Avmq2hY1qDtWbvq55Tkl7aQCcF3TCsU+ZqvRpJFG9Slfx B2SdIURfJ9scFq/4VaKgxcd0XP/Jam5/MZdUv0sqiYZ0HdPh8prZW9qNS7ACL0AHFv/y nRRva3VtEenBbSwyS8GW4VhV0oJUSpKnee0qRHTdkK5kvMJTIzDNJuQkJglLqkFQvcjU Y9VQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AJdphfGg; 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 68si4514463ple.590.2017.08.14.03.53.12; Mon, 14 Aug 2017 03:53: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=AJdphfGg; 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 S1752955AbdHNKxJ (ORCPT + 25 others); Mon, 14 Aug 2017 06:53:09 -0400 Received: from mail-wr0-f178.google.com ([209.85.128.178]:37057 "EHLO mail-wr0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752830AbdHNKxE (ORCPT ); Mon, 14 Aug 2017 06:53:04 -0400 Received: by mail-wr0-f178.google.com with SMTP id 33so33154037wrz.4 for ; Mon, 14 Aug 2017 03:53:03 -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=E6kGreoUgUe/D9Eg3bcGirQ1NSKJ8qaYFFtGJuuuVEk=; b=AJdphfGgN2qnullWPSPCBmAhdYqv+yELndCWi4y3jOaqDgb5t4six3fthdGHLwggQh St05w38t/xpcDF41RVqxe0v+pt4IFfiOUdXfmkw9f0QHf2GdG7GqQpglT5bZJCweUXh3 akcV7xdrbj5qGyz4QD9QUbt1ELd6lhMqpQrnQ= 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=E6kGreoUgUe/D9Eg3bcGirQ1NSKJ8qaYFFtGJuuuVEk=; b=RhMxBt4i/gjay77zmwCUugnwQniRYdtTW/xbcAWjMzvxqnfKPDd6HMkn4aa2Iifqn8 PDbqH0SuvhrwaGcIRpDWh/KiiMBoFMNaNyFl8SltHM7dvc5YqgUnbYHqm68vGg5MVvi5 78A7BzvzDuBIALLM4Yg5rgRkAy4OHL3BY33xrDOZt9WKrHo7tKUuBdpH+fja7dlUfkn7 pd3UDVvWEoTcbTGNJzIRu85r2z6kayCRx0WDsTmMl/pZotNwh9csuG+h8YbHVQpaXh8d 7p+FMcGLKbPCaKVZe+sW4bfUjoeyLvX3xfcQW8n8x3bsD83GyVLJgvBqW79IaIsyyte6 9kWA== X-Gm-Message-State: AHYfb5gev5sLEHEF9BEXyFKUbevRMQwkTPthIZROiyhRX2fSE9MC3jom IlyIkTwsiRULMl/crmISAA== X-Received: by 10.223.181.10 with SMTP id a10mr18404032wrd.239.1502707982882; Mon, 14 Aug 2017 03:53:02 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id b15sm1490732wra.68.2017.08.14.03.52.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Aug 2017 03:53:02 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Jessica Yu , x86@kernel.org Subject: [PATCH 1/5] arch: enable relative relocations for arm64, power, x86, s390 and x86 Date: Mon, 14 Aug 2017 11:52:27 +0100 Message-Id: <20170814105231.14608-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170814105231.14608-1-ard.biesheuvel@linaro.org> References: <20170814105231.14608-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 a new Kconfig symbol and define it for some architectures that should be able to support and benefit from it. Cc: Catalin Marinas Cc: Will Deacon Cc: Benjamin Herrenschmidt Cc: Paul Mackerras Cc: Michael Ellerman Cc: Martin Schwidefsky Cc: Heiko Carstens Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: x86@kernel.org Signed-off-by: Ard Biesheuvel --- arch/Kconfig | 10 ++++++++++ arch/arm64/Kconfig | 1 + arch/arm64/kernel/vmlinux.lds.S | 2 +- arch/powerpc/Kconfig | 1 + arch/s390/Kconfig | 1 + arch/x86/Kconfig | 1 + 6 files changed, 15 insertions(+), 1 deletion(-) -- 2.11.0 diff --git a/arch/Kconfig b/arch/Kconfig index 21d0089117fe..4e77d8e8697e 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -940,4 +940,14 @@ config REFCOUNT_FULL against various use-after-free conditions that can be used in security flaw exploits. +config HAVE_ARCH_PREL32_RELOCATIONS + bool + help + May be selected by an architecture if it supports place-relative + 32-bit relocations, both in the toolchain and in the module loader, + in which case relative references can be used in special sections + for PCI fixup, initcalls etc which are only half the size on 64 bit + architectures, and don't require runtime relocation on relocatable + kernels. + source "kernel/gcov/Kconfig" diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index dfd908630631..ce05d2d8143e 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -72,6 +72,7 @@ config ARM64 select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT + select HAVE_ARCH_PREL32_RELOCATIONS select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_TRACEHOOK select HAVE_ARCH_TRANSPARENT_HUGEPAGE diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index 987a00ee446c..5849bd4e2b06 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -171,7 +171,7 @@ SECTIONS CON_INITCALL SECURITY_INITCALL INIT_RAM_FS - *(.init.rodata.* .init.bss) /* from the EFI stub */ + *(.init.rodata.* .init.bss .init.discard.*) /* from the EFI stub */ } .exit.data : { ARM_EXIT_KEEP(EXIT_DATA) diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index 36f858c37ca7..f7e60ed5bdd0 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -163,6 +163,7 @@ config PPC select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT + select HAVE_ARCH_PREL32_RELOCATIONS select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_TRACEHOOK select ARCH_HAS_STRICT_KERNEL_RWX if (PPC_BOOK3S_64 && !RELOCATABLE && !HIBERNATION) diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig index 7eeb75d758c1..1206cc7daa0d 100644 --- a/arch/s390/Kconfig +++ b/arch/s390/Kconfig @@ -127,6 +127,7 @@ config S390 select HAVE_ARCH_AUDITSYSCALL select HAVE_ARCH_JUMP_LABEL select CPU_NO_EFFICIENT_FFS if !HAVE_MARCH_Z9_109_FEATURES + select HAVE_ARCH_PREL32_RELOCATIONS select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_SOFT_DIRTY select HAVE_ARCH_TRACEHOOK diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 781521b7cf9e..77246125883e 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -112,6 +112,7 @@ config X86 select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT select HAVE_ARCH_COMPAT_MMAP_BASES if MMU && COMPAT + select HAVE_ARCH_PREL32_RELOCATIONS select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_TRACEHOOK select HAVE_ARCH_TRANSPARENT_HUGEPAGE From patchwork Mon Aug 14 10:52:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 109978 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp4170544qge; Mon, 14 Aug 2017 03:53:20 -0700 (PDT) X-Received: by 10.84.247.15 with SMTP id n15mr27518703pll.287.1502708000881; Mon, 14 Aug 2017 03:53:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502708000; cv=none; d=google.com; s=arc-20160816; b=lc5K2Tco7AgcLCKdG+AxxgtMxwIruvedfIKxtJT0o5P3tW2MfCxPdcYd/IT5rsj/rq glNw+vG0fB7LuhZ0/SWvVKoiKyTeutJCogJXAFfnBgbQ2OS0w4q8eZMwMICzi7lnEfVJ 4dGRGGIdxcEqYeNWmv8oLFOrv7qVWlxlCqMUD2BB2Fxhs1xz1gA84OX7znPzIxQH2tm5 zhRg5Wt3EedJm1SyyFCY2IO9kFELfnNbiSqqpr6LdFNwM16SVDf467WPm6bNS8TPBm0t /8rYb8ZPbRH6qJ8fB7UehAzZZRArpj3ZXlOyD2VpThPhLP9AB4IAGi4BHBGqe/Z+tsVh FGjg== 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=+erxBDTrIWKoeCYDiXcLSr1VCW88bivh1rvMlB4c5z4=; b=fYAxWxPMn062R0EYMZjvV8B1pc7/JfLtsb1Trku8NpsQnE5cCcms1NP2dvOZKketWf 7eLVhH6pdU6AvjeMUF9pnialOCr+tpHlg7kaeh3z89VVoYYA3Yp0TnULElUJUQgxUUiN hVUXrNzINEeVamgQme53Phm90npT2gUKVbojMToirb4bYH65IODWkYOaU619pOrWowLS eik5d4GjWetUkN0P7/APU+LI0maT+JzqQ7EfMmMevPD0JtOHDf9JVk3RhxWYSTQ8J9uv HWJv64K1yjlRFZKtiv1sQOXDZfVtrIBIbt6OToxLpKYpY//YyY7TF68hLroFUsPUCqCg qC0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=a2gM/WVA; 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 v3si3951383pgq.53.2017.08.14.03.53.20; Mon, 14 Aug 2017 03:53:20 -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=a2gM/WVA; 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 S1752994AbdHNKxQ (ORCPT + 25 others); Mon, 14 Aug 2017 06:53:16 -0400 Received: from mail-wm0-f52.google.com ([74.125.82.52]:38238 "EHLO mail-wm0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752951AbdHNKxL (ORCPT ); Mon, 14 Aug 2017 06:53:11 -0400 Received: by mail-wm0-f52.google.com with SMTP id f15so40564958wmg.1 for ; Mon, 14 Aug 2017 03:53:11 -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=+erxBDTrIWKoeCYDiXcLSr1VCW88bivh1rvMlB4c5z4=; b=a2gM/WVAPF5ImwdjydjPfN2SZpfSRon0uj3hFuMO3H+NdJIPbJvv0TzXNTLPs/M6QF LzezS1bILeyxFSqGRvyer/US+78QbZoOHIIM65l0lzgkenm3RnFSl9PKR5XAaoaY0K16 hTtO5fNBIaiGaZGmI8Lgdb02/7rw3PTmG7cOM= 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=+erxBDTrIWKoeCYDiXcLSr1VCW88bivh1rvMlB4c5z4=; b=lATrxxmJphWVsIxIOD5oTBUJvYKTBOK+6eYyRcDTdHFr0kNyP8kRluVqsb6V7Euzb6 r7iwUsO+YeBn62/DQI53m+FkU7ZJ5xexbIQm0dD643eoDiqs3LkY6C4TTTKcRfavjq/C 1TaeLlZ8qnfxM1px2xcQ7lQHXw5QfTRTa04BJJbv8K2VFX/7kNM+AMFqZ2WucSr9j/Cy kStYMaHjT/b1k59pgebPWR8Dhl2aT5nbuIAUh8dw+Vmpyd8lRMfBhxCTBvo0b9Ni4jQ/ DUVe7SlsTDaLCUQYa/eEBL3cekeT137gzEYQI+SlcU7NXRQ/ENInPrKySiRmL75X2fXU l5oQ== X-Gm-Message-State: AHYfb5gkn9rkBOIlcOVZ56ZKFXXVLYflyYNf85JJGmzbbH70X5Hy7bZV fgK6792LOChO2iyVsjjooQ== X-Received: by 10.28.74.221 with SMTP id n90mr4499871wmi.27.1502707990032; Mon, 14 Aug 2017 03:53:10 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id b15sm1490732wra.68.2017.08.14.03.53.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Aug 2017 03:53:09 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Jessica Yu , Ingo Molnar Subject: [PATCH 2/5] module: use relative references for __ksymtab entries Date: Mon, 14 Aug 2017 11:52:28 +0100 Message-Id: <20170814105231.14608-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170814105231.14608-1-ard.biesheuvel@linaro.org> References: <20170814105231.14608-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org An ordinary arm64 defconfig build has ~64 KB worth of __ksymtab entries, each consisting of two 64-bit fields containing absolute references, to the symbol itself and to a char array containing its name, respectively. When we build the same configuration with KASLR enabled, we end up with an additional ~192 KB of relocations in the .init section, i.e., one 24 byte entry for each absolute reference, which all need to be processed at boot time. Given how the struct kernel_symbol that describes each entry is completely local to module.c (except for the references emitted by EXPORT_SYMBOL() itself), we can easily modify it to contain two 32-bit relative references instead. This reduces the size of the __ksymtab section by 50% for all 64-bit architectures, and gets rid of the runtime relocations entirely for architectures implementing KASLR, either via standard PIE linking (arm64) or using custom host tools (x86). Note that the binary search involving __ksymtab contents relies on each section being sorted by symbol name. This is implemented based on the input section names, not the names in the ksymtab entries, so this patch does not interfere with that. Given that the use of place-relative relocations requires support both in the toolchain and in the module loader, we cannot enable this feature for all architectures. So make it dependend on whether CONFIG_HAVE_ARCH_PREL32_RELOCATIONS is defined. Cc: Jessica Yu Cc: Arnd Bergmann Cc: Andrew Morton Cc: Ingo Molnar Cc: Kees Cook Cc: Thomas Garnier Cc: Nicolas Pitre Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/Kbuild | 1 + arch/x86/include/asm/export.h | 4 -- include/asm-generic/export.h | 12 ++++- include/linux/compiler.h | 11 +++++ include/linux/export.h | 47 +++++++++++++++----- kernel/module.c | 33 +++++++++++--- 6 files changed, 85 insertions(+), 23 deletions(-) -- 2.11.0 diff --git a/arch/x86/include/asm/Kbuild b/arch/x86/include/asm/Kbuild index 5d6a53fd7521..3e8a88dcaa1d 100644 --- a/arch/x86/include/asm/Kbuild +++ b/arch/x86/include/asm/Kbuild @@ -9,5 +9,6 @@ generated-y += xen-hypercalls.h generic-y += clkdev.h generic-y += dma-contiguous.h generic-y += early_ioremap.h +generic-y += export.h generic-y += mcs_spinlock.h generic-y += mm-arch-hooks.h diff --git a/arch/x86/include/asm/export.h b/arch/x86/include/asm/export.h deleted file mode 100644 index 138de56b13eb..000000000000 --- a/arch/x86/include/asm/export.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifdef CONFIG_64BIT -#define KSYM_ALIGN 16 -#endif -#include diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h index 719db1968d81..97ce606459ae 100644 --- a/include/asm-generic/export.h +++ b/include/asm-generic/export.h @@ -5,12 +5,10 @@ #define KSYM_FUNC(x) x #endif #ifdef CONFIG_64BIT -#define __put .quad #ifndef KSYM_ALIGN #define KSYM_ALIGN 8 #endif #else -#define __put .long #ifndef KSYM_ALIGN #define KSYM_ALIGN 4 #endif @@ -25,6 +23,16 @@ #define KSYM(name) name #endif +.macro __put, val, name +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + .long \val - ., \name - . +#elif defined(CONFIG_64BIT) + .quad \val, \name +#else + .long \val, \name +#endif +.endm + /* * note on .section use: @progbits vs %progbits nastiness doesn't matter, * since we immediately emit into those sections anyway. diff --git a/include/linux/compiler.h b/include/linux/compiler.h index eca8ad75e28b..5644d2e653f0 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -590,4 +590,15 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s (_________p1); \ }) +/* + * Force the compiler to emit 'sym' as a symbol, so that we can reference + * it from inline assembler. Necessary in case 'sym' could be inlined + * otherwise, or eliminated entirely due to lack of references that are + * visibile to the compiler. + */ +#define __ADDRESSABLE(sym) \ + static void * __attribute__((section(".discard.text"), used)) \ + __PASTE(__discard_##sym,__LINE__)(void) \ + { return (void *)&sym; } \ + #endif /* __LINUX_COMPILER_H */ diff --git a/include/linux/export.h b/include/linux/export.h index 1a1dfdb2a5c6..986d02e57253 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,48 @@ extern struct module __this_module; #define __CRC_SYMBOL(sym, sec) #endif +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +/* + * Emit the ksymtab entry as a pair of relative references: this reduces + * the size by half on 64-bit architectures, and eliminates the need for + * absolute relocations that require runtime processing on relocatable + * kernels. + */ +#define __KSYMTAB_ENTRY(sym, sec) \ + __ADDRESSABLE(sym) \ + asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \ + " .balign 8 \n" \ + VMLINUX_SYMBOL_STR(__ksymtab_##sym) ": \n" \ + " .long " VMLINUX_SYMBOL_STR(sym) "- . \n" \ + " .long " VMLINUX_SYMBOL_STR(__kstrtab_##sym) "- .\n" \ + " .previous \n") + +struct kernel_symbol +{ + signed int value_offset; + signed int name_offset; +}; +#else +#define __KSYMTAB_ENTRY(sym, sec) \ + static const struct kernel_symbol __ksymtab_##sym \ + __attribute__((section("___ksymtab" sec "+" #sym), used)) \ + = { (unsigned long)&sym, __kstrtab_##sym } + +struct kernel_symbol +{ + unsigned long value; + const char *name; +}; +#endif + /* For every exported symbol, place a struct in the __ksymtab section */ #define ___EXPORT_SYMBOL(sym, sec) \ extern typeof(sym) sym; \ __CRC_SYMBOL(sym, sec) \ static const char __kstrtab_##sym[] \ - __attribute__((section("__ksymtab_strings"), aligned(1))) \ + __attribute__((section("__ksymtab_strings"), used, aligned(1))) \ = VMLINUX_SYMBOL_STR(sym); \ - static const struct kernel_symbol __ksymtab_##sym \ - __used \ - __attribute__((section("___ksymtab" sec "+" #sym), used)) \ - = { (unsigned long)&sym, __kstrtab_##sym } + __KSYMTAB_ENTRY(sym, sec) #if defined(__KSYM_DEPS__) diff --git a/kernel/module.c b/kernel/module.c index 40f983cbea81..a45423dcc32d 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -539,12 +539,31 @@ static bool check_symbol(const struct symsearch *syms, return true; } +static unsigned long kernel_symbol_value(const struct kernel_symbol *sym) +{ +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + return (unsigned long)&sym->value_offset + sym->value_offset; +#else + return sym->value; +#endif +} + +static const char *kernel_symbol_name(const struct kernel_symbol *sym) +{ +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + return (const char *)((unsigned long)&sym->name_offset + + sym->name_offset); +#else + return sym->name; +#endif +} + static int cmp_name(const void *va, const void *vb) { const char *a; const struct kernel_symbol *b; a = va; b = vb; - return strcmp(a, b->name); + return strcmp(a, kernel_symbol_name(b)); } static bool find_symbol_in_section(const struct symsearch *syms, @@ -2190,7 +2209,7 @@ void *__symbol_get(const char *symbol) sym = NULL; preempt_enable(); - return sym ? (void *)sym->value : NULL; + return sym ? (void *)kernel_symbol_value(sym) : NULL; } EXPORT_SYMBOL_GPL(__symbol_get); @@ -2220,10 +2239,12 @@ static int verify_export_symbols(struct module *mod) for (i = 0; i < ARRAY_SIZE(arr); i++) { for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) { - if (find_symbol(s->name, &owner, NULL, true, false)) { + if (find_symbol(kernel_symbol_name(s), &owner, NULL, + true, false)) { pr_err("%s: exports duplicate symbol %s" " (owned by %s)\n", - mod->name, s->name, module_name(owner)); + mod->name, kernel_symbol_name(s), + module_name(owner)); return -ENOEXEC; } } @@ -2272,7 +2293,7 @@ static int simplify_symbols(struct module *mod, const struct load_info *info) ksym = resolve_symbol_wait(mod, info, name); /* Ok if resolved. */ if (ksym && !IS_ERR(ksym)) { - sym[i].st_value = ksym->value; + sym[i].st_value = kernel_symbol_value(ksym); break; } @@ -2532,7 +2553,7 @@ static int is_exported(const char *name, unsigned long value, ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab); else ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms); - return ks != NULL && ks->value == value; + return ks != NULL && kernel_symbol_value(ks) == value; } /* As per nm */ From patchwork Mon Aug 14 10:52:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 109979 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp4170597qge; Mon, 14 Aug 2017 03:53:24 -0700 (PDT) X-Received: by 10.98.69.7 with SMTP id s7mr24983167pfa.231.1502708004834; Mon, 14 Aug 2017 03:53:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502708004; cv=none; d=google.com; s=arc-20160816; b=x+Ovz5YsPjT1ceqQVsdvxLg1EDs2xj/5MkH+qG9qON4SgXA04d7xaqa6b6aC0fXHkA kSC47CnphrfmfhV5nZrt5vE36G3PoGPAPIHLYDyomhTLT1R3U8o6bB1SYDRpl1H6aFSp 9sSdA8p+7JbWzuIkmLJ5PokkReq68E18hW9m9pwCvgPWYrpkSaeiNGSz+t32Nj+exFpP NWM2PzFjV73oo73MxieLYrHM4zj5oIfimZ1iaQuvRAjY5ebsKQGlPUq/T2CcxESHUhqz 1P2+8hHL8P1ERWuH9CaMKF8oPVxexL+kktpPrQns0ziY1sN3TLz1GDha5upTPcQxLp9/ IHkg== 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=KUktAMBNiR4VldSs3RYzjjbVX5C11nfPK/CCOZrDP6A=; b=fHnJvB87hJtqtLsd5n+rpq1orlt9I2UkzhWl/M8n8E0tqwYIUepWtclhqV8pykwe5O R+ekf4sYNnJWZUXVRJxqTJWRVXGlBaujfrWbQMbtWofsYT4rh6h5tvMMwpRAZ+s+AzsQ 2nTR6KfcHKmkNJfcYHqwMnuQ4Q2rFSQlHsSazbQLW2Kg7gERyRWDcJAfzALlMmqv/Kvo MU4UaHOnRMSGms83m9WdP9b9qvMnk8f6iDU4qAMKjwK6xnxxy4gqUJ4pmCRRdv7E+xc7 imRxDDJ5kzBNKaGjv92sX3mVYv95uiORVWq4iU/pLKxUgax1b2GqmUlgNob4kXa8rMRl wsLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=eEaq432d; 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 v3si3951383pgq.53.2017.08.14.03.53.24; Mon, 14 Aug 2017 03:53:24 -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=eEaq432d; 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 S1753018AbdHNKxU (ORCPT + 25 others); Mon, 14 Aug 2017 06:53:20 -0400 Received: from mail-wr0-f181.google.com ([209.85.128.181]:38547 "EHLO mail-wr0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752986AbdHNKxQ (ORCPT ); Mon, 14 Aug 2017 06:53:16 -0400 Received: by mail-wr0-f181.google.com with SMTP id m57so2924163wrm.5 for ; Mon, 14 Aug 2017 03:53:16 -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=KUktAMBNiR4VldSs3RYzjjbVX5C11nfPK/CCOZrDP6A=; b=eEaq432dmqExvSYjNORKNKTooB9HpL0y+Zc3bYfb7mgl8o1+M1Qq93fGj82Ji6u4ka TIOk1QXwsBS7K6h4Ztp58rxSm02rg8n+kgDteHfW+R4ntnxq86usiDNVNQqnr7wEzrqp EzfNHpEXPc54YrbBqUYC9cAUgp1gMAZaiAOaw= 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=KUktAMBNiR4VldSs3RYzjjbVX5C11nfPK/CCOZrDP6A=; b=OgQwIKM6l4Mxfy4lMiKvqq8cd0XZuiaZI+CknQhgDhFEiOXURpXkL3cS5mse4nhlQ2 zB6cYDtsMZ6zUVbQixY/KXyoIJ6YSKu0eBYMW+8LdN1wOxm5qtCUVQtQlKplk1tjO/8k KTBGZ4H30QMQp9i+Ghnn6U14LbwdBbrHP6222wN4HyhG3aC5rbLHTAm8tU7HFqTz44td gC+7m8nqZ9qZ/upf2KLY6HdZVy5g9tF9pmJMvXlwir3icc2LR0ICkqLBDZGj4eNiqWKU HuLaUDCuKg1C22bLvfC5YzMSkt+Dq7OTUI5e5UWPEwteoY8m3k06OtDXIx8Ten50JtF3 hd1w== X-Gm-Message-State: AHYfb5jbnQT1JSIoyMz9vzNH49KLH1NtJVPqIUHmvxAyE6ZYfPTdbaid A2SNt/MaM8pbTQmCmvPZ+w== X-Received: by 10.223.161.84 with SMTP id r20mr16984155wrr.91.1502707995269; Mon, 14 Aug 2017 03:53:15 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id b15sm1490732wra.68.2017.08.14.03.53.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Aug 2017 03:53:14 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Jessica Yu Subject: [PATCH 3/5] init: allow initcall tables to be emitted using relative references Date: Mon, 14 Aug 2017 11:52:29 +0100 Message-Id: <20170814105231.14608-4-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170814105231.14608-1-ard.biesheuvel@linaro.org> References: <20170814105231.14608-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Allow the initcall tables to be emitted using relative references that are only half the size on 64-bit architectures and don't require fixups at runtime on relocatable kernels. Cc: Petr Mladek Cc: Sergey Senozhatsky Cc: Steven Rostedt Cc: James Morris Cc: "Serge E. Hallyn" Signed-off-by: Ard Biesheuvel --- include/linux/init.h | 44 +++++++++++++++----- init/main.c | 32 +++++++------- kernel/printk/printk.c | 4 +- security/security.c | 4 +- 4 files changed, 53 insertions(+), 31 deletions(-) -- 2.11.0 diff --git a/include/linux/init.h b/include/linux/init.h index 94769d687cf0..a65628998ce3 100644 --- a/include/linux/init.h +++ b/include/linux/init.h @@ -110,8 +110,24 @@ typedef int (*initcall_t)(void); typedef void (*exitcall_t)(void); -extern initcall_t __con_initcall_start[], __con_initcall_end[]; -extern initcall_t __security_initcall_start[], __security_initcall_end[]; +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +typedef signed int initcall_entry_t; + +static inline initcall_t initcall_from_entry(initcall_entry_t *entry) +{ + return (initcall_t)((unsigned long)entry + *entry); +} +#else +typedef initcall_t initcall_entry_t; + +static inline initcall_t initcall_from_entry(initcall_entry_t *entry) +{ + return *entry; +} +#endif + +extern initcall_entry_t __con_initcall_start[], __con_initcall_end[]; +extern initcall_entry_t __security_initcall_start[], __security_initcall_end[]; /* Used for contructor calls. */ typedef void (*ctor_fn_t)(void); @@ -161,9 +177,20 @@ extern bool initcall_debug; * as KEEP() in the linker script. */ -#define __define_initcall(fn, id) \ +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +#define ___define_initcall(fn, id, __sec) \ + __ADDRESSABLE(fn) \ + asm(".section \"" #__sec ".init\", \"a\" \n" \ + "__initcall_" #fn #id ": \n" \ + ".long " VMLINUX_SYMBOL_STR(fn) " - . \n" \ + ".previous \n"); +#else +#define ___define_initcall(fn, id, __sec) \ static initcall_t __initcall_##fn##id __used \ - __attribute__((__section__(".initcall" #id ".init"))) = fn; + __attribute__((__section__(#__sec ".init"))) = fn; +#endif + +#define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id) /* * Early initcalls run before initializing SMP. @@ -202,13 +229,8 @@ extern bool initcall_debug; #define __exitcall(fn) \ static exitcall_t __exitcall_##fn __exit_call = fn -#define console_initcall(fn) \ - static initcall_t __initcall_##fn \ - __used __section(.con_initcall.init) = fn - -#define security_initcall(fn) \ - static initcall_t __initcall_##fn \ - __used __section(.security_initcall.init) = fn +#define console_initcall(fn) ___define_initcall(fn,, .con_initcall) +#define security_initcall(fn) ___define_initcall(fn,, .security_initcall) struct obs_kernel_param { const char *str; diff --git a/init/main.c b/init/main.c index 052481fbe363..ac2a91f71107 100644 --- a/init/main.c +++ b/init/main.c @@ -833,18 +833,18 @@ int __init_or_module do_one_initcall(initcall_t fn) } -extern initcall_t __initcall_start[]; -extern initcall_t __initcall0_start[]; -extern initcall_t __initcall1_start[]; -extern initcall_t __initcall2_start[]; -extern initcall_t __initcall3_start[]; -extern initcall_t __initcall4_start[]; -extern initcall_t __initcall5_start[]; -extern initcall_t __initcall6_start[]; -extern initcall_t __initcall7_start[]; -extern initcall_t __initcall_end[]; - -static initcall_t *initcall_levels[] __initdata = { +extern initcall_entry_t __initcall_start[]; +extern initcall_entry_t __initcall0_start[]; +extern initcall_entry_t __initcall1_start[]; +extern initcall_entry_t __initcall2_start[]; +extern initcall_entry_t __initcall3_start[]; +extern initcall_entry_t __initcall4_start[]; +extern initcall_entry_t __initcall5_start[]; +extern initcall_entry_t __initcall6_start[]; +extern initcall_entry_t __initcall7_start[]; +extern initcall_entry_t __initcall_end[]; + +static initcall_entry_t *initcall_levels[] __initdata = { __initcall0_start, __initcall1_start, __initcall2_start, @@ -870,7 +870,7 @@ static char *initcall_level_names[] __initdata = { static void __init do_initcall_level(int level) { - initcall_t *fn; + initcall_entry_t *fn; strcpy(initcall_command_line, saved_command_line); parse_args(initcall_level_names[level], @@ -880,7 +880,7 @@ static void __init do_initcall_level(int level) NULL, &repair_env_string); for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++) - do_one_initcall(*fn); + do_one_initcall(initcall_from_entry(fn)); } static void __init do_initcalls(void) @@ -911,10 +911,10 @@ static void __init do_basic_setup(void) static void __init do_pre_smp_initcalls(void) { - initcall_t *fn; + initcall_entry_t *fn; for (fn = __initcall_start; fn < __initcall0_start; fn++) - do_one_initcall(*fn); + do_one_initcall(initcall_from_entry(fn)); } /* diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index fc47863f629c..39d343c1c4b2 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2623,7 +2623,7 @@ EXPORT_SYMBOL(unregister_console); */ void __init console_init(void) { - initcall_t *call; + initcall_entry_t *call; /* Setup the default TTY line discipline. */ n_tty_init(); @@ -2634,7 +2634,7 @@ void __init console_init(void) */ call = __con_initcall_start; while (call < __con_initcall_end) { - (*call)(); + initcall_from_entry(call)(); call++; } } diff --git a/security/security.c b/security/security.c index 30132378d103..63b5f52bb098 100644 --- a/security/security.c +++ b/security/security.c @@ -44,10 +44,10 @@ static __initdata char chosen_lsm[SECURITY_NAME_MAX + 1] = static void __init do_security_initcalls(void) { - initcall_t *call; + initcall_entry_t *call; call = __security_initcall_start; while (call < __security_initcall_end) { - (*call) (); + initcall_from_entry(call)(); call++; } } From patchwork Mon Aug 14 10:52:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 109980 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp4170655qge; Mon, 14 Aug 2017 03:53:30 -0700 (PDT) X-Received: by 10.99.3.197 with SMTP id 188mr24556772pgd.288.1502708010541; Mon, 14 Aug 2017 03:53:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502708010; cv=none; d=google.com; s=arc-20160816; b=ezOm98M+AyC4VFVpgX0zNURQmxXkueJ30BmueI0kUUwYzzQKsnahH9d3I29JBTIcJi dC6RcCLOGGODvp3OOnI1p7aDDg9HLniXGKMwY7tkiZ/li5qkXEFSIx6OS8DWjui/z3hF 82gPlI2TvsbQgwrOIOaCxAWAensY8+9fh3I+/J7jMjusz92uzqEbdbVSyjvuFWkULerz ZIBsw2sSY4wEFPcfkrxMcUdJniJHYO48V16wMG3qfh2vgCnkj0CTRP+UtUHbZBXYErzA cQ840AlvWK3Jwen23mKBKVhzHm4N4gMm23ZZDYMMsH7Pc79h32eV9MMGzarNMCADWoD9 5YOw== 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=T1/I/dl/glTN9Fw6FNXgw+ZEb6RKfMBrOWi+jqpuGSk=; b=jFMOSrhGTb2mdPW6Nzzb7zxBkIFFYUHaZU49H9m5KiJw1vsMZ7KE0LTM7dDNf7bGz7 eTuhG6yPivO2h97Sr2qOY5EnrLIMSOJ+83byd+aAcPQHP5isddEO7Qwr594lUkDtTtlY TqL9Q0F8UGJU09GRFU+I9YrovtvUqfayQbtWiAntSQ1ps8RFEGR5tpy/SZK/hcqiJ6lI 5q+Me7yGp0M15R+kQ2vspUgmFAJMIIFxaQ4SRHJCceVF5ykCW80Byjr6z5AYK9UGtS/K Zn2MfLiLGthpxQoXX+EE16bx+CQyPdLGf9z85TnQtVD0XePej10bRdJXjGhFLhM6r3l8 ynCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=UQiV3kye; 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 z19si3910380pgn.611.2017.08.14.03.53.30; Mon, 14 Aug 2017 03:53:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=UQiV3kye; 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 S1753040AbdHNKx0 (ORCPT + 25 others); Mon, 14 Aug 2017 06:53:26 -0400 Received: from mail-wm0-f49.google.com ([74.125.82.49]:37705 "EHLO mail-wm0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752986AbdHNKxW (ORCPT ); Mon, 14 Aug 2017 06:53:22 -0400 Received: by mail-wm0-f49.google.com with SMTP id i66so40651378wmg.0 for ; Mon, 14 Aug 2017 03:53:21 -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=T1/I/dl/glTN9Fw6FNXgw+ZEb6RKfMBrOWi+jqpuGSk=; b=UQiV3kye6z5oVA5oXcF9E+jxs/FpJERXRBEwO75LSG2KjNyCe8IYxKGaSTqTtlbm8Z Chv+7wrWPAhRcQQGS/b4oL9InG4duhYLG+FW1Ez+HROKvxTPJhT+hfbwBiRV4Q7b6Bn/ 0Qs40c304jQSiXZUBo4MXz3hej96feeC0dQ3E= 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=T1/I/dl/glTN9Fw6FNXgw+ZEb6RKfMBrOWi+jqpuGSk=; b=ulVY8DeK9e35QDyoywAtxagM7PQgtCyCIL0jXe50VsR5+Z+bPEuRJKSKaYeqD47iav SqrHflFHT2jTWpdjHI922If0/KZ9PzFMJq+TpQmcD357CQCy53U1JSLf/5CUwH5utIB0 rwfiqymQn+CunjtAKN6VPXVrdBjWuiDhh5XDDUk7XX0jkw7LiaSvH9oRCstozuTuDBwE PqP6H1ZmBOIERbywNePFnbsA0u+u2b0hMY/a8C5ZDnpoEe6Q/av7YAsaIir8jPJRz6+K usNxTDDrWhpfAlN7yFaG8Ofk/bc6Vjue8Ab6asWQ3NdjodRM4UywyxjhjvW66RJB2dvE YMLA== X-Gm-Message-State: AHYfb5hbsB3HLChLo/IUvhBIJy7rTczvf/v1jl+4Zi6JT9o+/pe7Q3x4 Y1/q4jfQq2lzRP3rFtjW4Q== X-Received: by 10.28.13.78 with SMTP id 75mr3584753wmn.11.1502708000547; Mon, 14 Aug 2017 03:53:20 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id b15sm1490732wra.68.2017.08.14.03.53.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Aug 2017 03:53:19 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Jessica Yu Subject: [PATCH 4/5] drivers: pci: add support for relative addressing in quirk tables Date: Mon, 14 Aug 2017 11:52:30 +0100 Message-Id: <20170814105231.14608-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170814105231.14608-1-ard.biesheuvel@linaro.org> References: <20170814105231.14608-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Allow the PCI quirk tables to be emitted in a way that avoids absolute references to the hook functions. This reduces the size of the entries, and, more importantly, makes them invariant under runtime relocation (e.g., for KASLR) Cc: Bjorn Helgaas Signed-off-by: Ard Biesheuvel --- drivers/pci/quirks.c | 13 ++++++++++--- include/linux/pci.h | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) -- 2.11.0 diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c index 6967c6b4cf6b..10126612e342 100644 --- a/drivers/pci/quirks.c +++ b/drivers/pci/quirks.c @@ -3547,9 +3547,16 @@ static void pci_do_fixups(struct pci_dev *dev, struct pci_fixup *f, f->vendor == (u16) PCI_ANY_ID) && (f->device == dev->device || f->device == (u16) PCI_ANY_ID)) { - calltime = fixup_debug_start(dev, f->hook); - f->hook(dev); - fixup_debug_report(dev, calltime, f->hook); + void (*hook)(struct pci_dev *dev); +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + hook = (void *)((unsigned long)&f->hook_offset + + f->hook_offset); +#else + hook = f->hook; +#endif + calltime = fixup_debug_start(dev, hook); + hook(dev); + fixup_debug_report(dev, calltime, hook); } } diff --git a/include/linux/pci.h b/include/linux/pci.h index 4869e66dd659..203544f33345 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1734,7 +1734,11 @@ struct pci_fixup { u16 device; /* You can use PCI_ANY_ID here of course */ u32 class; /* You can use PCI_ANY_ID here too */ unsigned int class_shift; /* should be 0, 8, 16 */ +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + signed int hook_offset; +#else void (*hook)(struct pci_dev *dev); +#endif }; enum pci_fixup_pass { @@ -1748,12 +1752,28 @@ enum pci_fixup_pass { pci_fixup_suspend_late, /* pci_device_suspend_late() */ }; +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +#define __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \ + class_shift, hook) \ + __ADDRESSABLE(hook) \ + asm(".section " #sec ", \"a\" \n" \ + ".balign 16 \n" \ + ".short " #vendor ", " #device " \n" \ + ".long " #class ", " #class_shift " \n" \ + ".long " VMLINUX_SYMBOL_STR(hook) " - . \n" \ + ".previous \n"); +#define DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \ + class_shift, hook) \ + __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \ + class_shift, hook) +#else /* Anonymous variables would be nice... */ #define DECLARE_PCI_FIXUP_SECTION(section, name, vendor, device, class, \ class_shift, hook) \ static const struct pci_fixup __PASTE(__pci_fixup_##name,__LINE__) __used \ __attribute__((__section__(#section), aligned((sizeof(void *))))) \ = { vendor, device, class, class_shift, hook }; +#endif #define DECLARE_PCI_FIXUP_CLASS_EARLY(vendor, device, class, \ class_shift, hook) \ From patchwork Mon Aug 14 10:52:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 109981 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp4170769qge; Mon, 14 Aug 2017 03:53:37 -0700 (PDT) X-Received: by 10.99.122.28 with SMTP id v28mr23749263pgc.98.1502708017733; Mon, 14 Aug 2017 03:53:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502708017; cv=none; d=google.com; s=arc-20160816; b=Koe5YIMWkvBOK/Vt81v7xzEvGPLl1ec7Tn1hhZCmRDVw8wYAh5TKV5+tL+Y8DWMT7k tpRS9jXNQZ5S5aifjfZ/SGuLtl61cJW+3+ke9E6C7Ie2xDSF+3KfQpXkGpRoJXi4yWqw kCOQTJS8lAocOwNScv0mtsEzxjZLT6D2Nola05V0bYLIzD1C+yldRUQFlig2JJZnPMmT pJPILVPYbaQtkvKpsdtzIUYLswX9lfwC2p+VFRa9WTjt+PEJLDv5CdW+Ln/HXTy1w389 b2vEAn2xl74z8wAvu1VOXHEwpWhYK675heOfNMlFBf9wGAZubcpa6CrE/xxe+gD4TPvt jMJQ== 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=TjGcqIftzqzs+2R1IOUAN5tRwF3sOEXGjb79whz3uHA=; b=YFYwCr/kDNQfSQd00Lf7ohN6NISRQ/hHMa1OrBUYDlFdBWR9NjlpEWo4IaB7ObhB87 iP3QZpRCeyzabMA7Ec2kN9+LMGq3wZw4SSi76xsPhtfMK8uYV8hY7NzTQgGkL07a3k1n GVYTyCQOnTkcR2DtTIqxcrfDUZAzB/TH+b90U8xMevZrlgmAPKWO/deO8sQcE02kJLgd rjdBFdmCAVuWEazN+K3kbN+EKDt53cNJ06ZwSvaya3CH9TGZbtcXw8gm4nHfk6K8iOE1 zxwXrapHqiNK0FOpyvPYUnD6qTdZDmxrqHBNVVQmjEoZiihCao22G3r035anRPnpH9+9 MLGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VW0ZglyM; 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 m3si4434508pld.47.2017.08.14.03.53.37; Mon, 14 Aug 2017 03:53:37 -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=VW0ZglyM; 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 S1753061AbdHNKxc (ORCPT + 25 others); Mon, 14 Aug 2017 06:53:32 -0400 Received: from mail-wr0-f177.google.com ([209.85.128.177]:37125 "EHLO mail-wr0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753020AbdHNKx1 (ORCPT ); Mon, 14 Aug 2017 06:53:27 -0400 Received: by mail-wr0-f177.google.com with SMTP id 33so33156955wrz.4 for ; Mon, 14 Aug 2017 03:53:27 -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=TjGcqIftzqzs+2R1IOUAN5tRwF3sOEXGjb79whz3uHA=; b=VW0ZglyMMFjtxin3p/n/BbzMJQ0EI+4o7gh5+O34u+6R74bJ0A2iq8xNAjVVPp5da4 MPpv82Y14CQstkFTT2M0b8I9qqSUPR1OWxTkVMSdQy/uSNta8h/D0dgX8pxN89Q5nC0j Mjn81q0S99V/6rDqXZRocJ9s1Y1T9xqwgx49o= 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=TjGcqIftzqzs+2R1IOUAN5tRwF3sOEXGjb79whz3uHA=; b=XyDNOl+MzKkYfoCCkACjC92Cyuk/lW8TqY5Mgh09Ph/2pYItIRp27TNeBJfaTtgLIg yV3DijdK3Sixv+EOpaviSnQTlMEUYmV58x0OvNWrkBafa6IZUioU72UWGNNnkNkvSiQh N1/HJWMyaWP6uF7uPKFQbA+IjPTKhGUSSa5lxbK2pLr9wCRQCbjTITbb4sBP2oFi7IvW HfFWfaU4vJnNvRIHWv8WZzsHEaTdSB5koNGW4us4FHuwB6Mitd7Qsr/0VBHy/Uw1B65L R0b0345uqRQNIe+aSfmz/P0hXbuyMN7A86vmu96Cip1VR6NpHqtzXFSj7k/3oOxTGUhT YpeA== X-Gm-Message-State: AHYfb5g+fwwA8bhlwnCMiknqfpjw2pguUyAaXCOCSrNY48+ix91iX42y 7uOLhVJ6xUoj7HcCk83L/g== X-Received: by 10.223.130.100 with SMTP id 91mr2179781wrb.34.1502708006266; Mon, 14 Aug 2017 03:53:26 -0700 (PDT) Received: from localhost.localdomain ([154.146.161.128]) by smtp.gmail.com with ESMTPSA id b15sm1490732wra.68.2017.08.14.03.53.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Aug 2017 03:53:25 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Ard Biesheuvel , "H. Peter Anvin" , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Michael Ellerman , Thomas Garnier , Thomas Gleixner , "Serge E. Hallyn" , Bjorn Helgaas , Benjamin Herrenschmidt , Paul Mackerras , Catalin Marinas , Petr Mladek , Ingo Molnar , James Morris , Andrew Morton , Nicolas Pitre , Steven Rostedt , Martin Schwidefsky , Sergey Senozhatsky , Jessica Yu Subject: [PATCH 5/5] kernel: tracepoints: add support for relative references Date: Mon, 14 Aug 2017 11:52:31 +0100 Message-Id: <20170814105231.14608-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170814105231.14608-1-ard.biesheuvel@linaro.org> References: <20170814105231.14608-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To avoid the need for relocating absolute references to tracepoint structures at boot time when running relocatable kernels (which may take a disproportionate amount of space), add the option to emit these tables as relative references instead. Cc: Steven Rostedt Cc: Ingo Molnar Signed-off-by: Ard Biesheuvel --- include/linux/tracepoint.h | 19 +++++++++++++++---- kernel/tracepoint.c | 19 ++++++++++++++----- 2 files changed, 29 insertions(+), 9 deletions(-) -- 2.11.0 diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index a26ffbe09e71..d02bf1a695e8 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -228,6 +228,19 @@ extern void syscall_unregfunc(void); return static_key_false(&__tracepoint_##name.key); \ } +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS +#define __TRACEPOINT_ENTRY(name) \ + asm(" .section \"__tracepoints_ptrs\", \"a\" \n" \ + " .balign 4 \n" \ + " .long " VMLINUX_SYMBOL_STR(__tracepoint_##name) " - .\n" \ + " .previous \n") +#else +#define __TRACEPOINT_ENTRY(name) \ + static struct tracepoint * const __tracepoint_ptr_##name __used \ + __attribute__((section("__tracepoints_ptrs"))) = \ + &__tracepoint_##name +#endif + /* * We have no guarantee that gcc and the linker won't up-align the tracepoint * structures, so we create an array of pointers that will be used for iteration @@ -237,11 +250,9 @@ extern void syscall_unregfunc(void); static const char __tpstrtab_##name[] \ __attribute__((section("__tracepoints_strings"))) = #name; \ struct tracepoint __tracepoint_##name \ - __attribute__((section("__tracepoints"))) = \ + __attribute__((section("__tracepoints"), used)) = \ { __tpstrtab_##name, STATIC_KEY_INIT_FALSE, reg, unreg, NULL };\ - static struct tracepoint * const __tracepoint_ptr_##name __used \ - __attribute__((section("__tracepoints_ptrs"))) = \ - &__tracepoint_##name; + __TRACEPOINT_ENTRY(name); #define DEFINE_TRACE(name) \ DEFINE_TRACE_FN(name, NULL, NULL); diff --git a/kernel/tracepoint.c b/kernel/tracepoint.c index 685c50ae6300..1c6689603764 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -28,8 +28,8 @@ #include #include -extern struct tracepoint * const __start___tracepoints_ptrs[]; -extern struct tracepoint * const __stop___tracepoints_ptrs[]; +extern const unsigned char __start___tracepoints_ptrs[]; +extern const unsigned char __stop___tracepoints_ptrs[]; /* Set to 1 to enable tracepoint debug output */ static const int tracepoint_debug; @@ -503,17 +503,26 @@ 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, +static void for_each_tracepoint_range(const void *begin, const void *end, void (*fct)(struct tracepoint *tp, void *priv), void *priv) { +#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS + const signed int *iter; + + if (!begin) + return; + for (iter = begin; iter < (signed int *)end; iter++) { + fct((struct tracepoint *)((unsigned long)iter + *iter), priv); + } +#else struct tracepoint * const *iter; if (!begin) return; - for (iter = begin; iter < end; iter++) + for (iter = begin; iter < (struct tracepoint * const *)end; iter++) fct(*iter, priv); +#endif } /**