From patchwork Thu Aug 10 17:26:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9894311 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B4C8E60236 for ; Thu, 10 Aug 2017 17:28:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F5891FF61 for ; Thu, 10 Aug 2017 17:28:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6FD2F288ED; Thu, 10 Aug 2017 17:28:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B3BB628790 for ; Thu, 10 Aug 2017 17:28:53 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dfrEr-000870-A7; Thu, 10 Aug 2017 17:27:17 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dfrEp-00084N-Tl for xen-devel@lists.xenproject.org; Thu, 10 Aug 2017 17:27:16 +0000 Received: from [193.109.254.147] by server-5.bemta-6.messagelabs.com id A7/75-03368-3779C895; Thu, 10 Aug 2017 17:27:15 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrLIsWRWlGSWpSXmKPExsXiVRuso1s0vSf SYFmnuMX3LZOZHBg9Dn+4whLAGMWamZeUX5HAmnGrYy5jwRTJimcN+5kaGC+IdDFycQgJTGeU +DxnKTOIwyLwikXiU+sxRhBHQqCfVeJI+2WmLkZOICdOYsvbR2wQdoXE2Qf7WEFsIQElia0bI LqFBP4xShx5sZERJMEmoCWxp2E+E0hCRGCpoMTJ1mtgY5kFWpgknvzbBjZWWCBQYu2LhUDtHE DLVSW6doOFeQUsJaY9n8gCsc1E4knrJLChnCDxZTOZITZbSEz9tZJpAqPAAkaGVYzqxalFZal FuqZ6SUWZ6RkluYmZObqGBmZ6uanFxYnpqTmJScV6yfm5mxiBwcUABDsYp1/2P8QoycGkJMpb /Kk7UogvKT+lMiOxOCO+qDQntfgQowwHh5IE74dpPZFCgkWp6akVaZk5wDCHSUtw8CiJ8D4CS fMWFyTmFmemQ6ROMVpyTDqw/QsTR8eMn9+YOF5N+P+NSYglLz8vVUqcdx9IgwBIQ0ZpHtw4WC xeYpSVEuZlBDpQiKcgtSg3swRV/hWjOAejkjCvK8gUnsy8Eritr4AOYgI6KMK3E+SgkkSElFQ D43r2PPFZlnP3pt6U0/rxWZB1Q/328uY27fXP2Gf5acmqVN88HFJdsFa0P2ZZQu+M/eEezLJM vGc7Nv+suVbz0FpX1TvswR0GUZaTesWmdXfv/g/bELBrd1Nz0/drV80Ph8n2PEwIqLPoK46SF /8Z9nJKz8xMpgvLQ6QfrtGsTT57daZjTL2pEktxRqKhFnNRcSIANhscYMACAAA= X-Env-Sender: thgarnie@google.com X-Msg-Ref: server-8.tower-27.messagelabs.com!1502386033!100358176!1 X-Originating-IP: [74.125.83.44] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 21898 invoked from network); 10 Aug 2017 17:27:14 -0000 Received: from mail-pg0-f44.google.com (HELO mail-pg0-f44.google.com) (74.125.83.44) by server-8.tower-27.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 10 Aug 2017 17:27:14 -0000 Received: by mail-pg0-f44.google.com with SMTP id v77so5497864pgb.3 for ; Thu, 10 Aug 2017 10:27:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=zchEJ2nNIqdKqZtAfx6/uMYObu2bdbXYKsFSEdmHnzY=; b=rfJNML2XXSKGp2bEX9FtCAQmvZ46jSJCyoExg7m4JcjQQUQbxO8N0j1aN7NVkK2kjx OUUOJ40W07TXl0GiTJ1OhgPdPg5c+PsmPPk+TXQ5G6JIYMN2T5gn5ZnROPzcFhHU1Blt ADv4W858Pr/r2s8s9UaWtlE1bXBMsXkLE3JMTQEn5eAItYnAVOkvxjuxbmSKFLzMSbYS tQZ5/Ih+LN474ISWh6+HNfhdVy+DzB6AQ1Pdw2kkr9YpTeIcsiltuCTLz/fNKsSG4SL/ 8oWHo3i8g/L9akYiWNUNO/vAf/dehNvLZyP3x0yXTsYiTf2wVymhKTs68L2HBHGQA07h Fiqg== 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=zchEJ2nNIqdKqZtAfx6/uMYObu2bdbXYKsFSEdmHnzY=; b=BQmV/aaidrpXlyHIvGMAQrXHLI4cEIokrH7g9+6T9a/18e7RBmM1xf93Wqcrcwbjhl ilkbAv9zlg01oqJ4n5Qxe0d1tIBR4ePe1gHdV7/T8XP2QnIJyEx2yVrWIKm5o3ysRF71 Sqc2THdxO87e83lBggXB0zSt8cqlXVZgO/Ae5aGf3Pe1mEzLQdS4QU4Gfo4p5ufYDCGX 04p5GrfrxL1HM+e+/NF0nCIeQNtiTcb6HujEUZS/OBwvydCkzaVZ1PT5PhdprAWzDuBv XyxelR1CcPrh/hH+78NDiPzd1ynIbhaCCOF7divYc7jPGHFw/wnlVg0O31T+GIY5dgb8 DgYw== X-Gm-Message-State: AHYfb5jy8AMyALhi4d9IYeVi2fvSMZ28JM4izwOxJFhty8xArB4f2VWH vQ0oPUB2yXYVD8tm X-Received: by 10.98.1.199 with SMTP id 190mr6635670pfb.178.1502386032313; Thu, 10 Aug 2017 10:27:12 -0700 (PDT) Received: from skynet.sea.corp.google.com ([172.31.92.33]) by smtp.gmail.com with ESMTPSA id p77sm11894925pfi.153.2017.08.10.10.27.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 10 Aug 2017 10:27:11 -0700 (PDT) From: Thomas Garnier To: Herbert Xu , "David S . Miller" , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Arnd Bergmann , Thomas Garnier , Matthias Kaehlcke , Boris Ostrovsky , Juergen Gross , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Tom Lendacky , Andy Lutomirski , Borislav Petkov , Brian Gerst , "Kirill A . Shutemov" , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Tejun Heo , Christoph Lameter , Paul Gortmaker , Chris Metcalf , Andrew Morton , "Paul E . McKenney" , Nicolas Pitre , Christopher Li , "Rafael J . Wysocki" , Lukas Wunner , Mika Westerberg , Dou Liyang , Daniel Borkmann , Alexei Starovoitov , Masahiro Yamada , Markus Trippelsdorf , Steven Rostedt , Kees Cook , Rik van Riel , David Howells , Waiman Long , Kyle Huey , Peter Foley , Tim Chen , Catalin Marinas , Ard Biesheuvel , Michal Hocko , Matthew Wilcox , "H . J . Lu" , Paul Bolle , Rob Landley , Baoquan He , Daniel Micay Date: Thu, 10 Aug 2017 10:26:12 -0700 Message-Id: <20170810172615.51965-21-thgarnie@google.com> X-Mailer: git-send-email 2.14.0.434.g98096fd7a8-goog In-Reply-To: <20170810172615.51965-1-thgarnie@google.com> References: <20170810172615.51965-1-thgarnie@google.com> Cc: linux-arch@vger.kernel.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, linux-sparse@vger.kernel.org, linux-crypto@vger.kernel.org, kernel-hardening@lists.openwall.com, xen-devel@lists.xenproject.org Subject: [Xen-devel] [RFC v2 20/23] x86/pie: Add option to build the kernel as PIE for x86_64 X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add the CONFIG_X86_PIE option which builds the kernel as a Position Independent Executable (PIE). The kernel is currently build with the mcmodel=kernel option which forces it to stay on the top 2G of the virtual address space. With PIE, the kernel will be able to move below the -2G limit increasing the KASLR range from 1GB to 3GB. The modules do not support PIE due to how they are linked. Disable PIE for them and default to mcmodel=kernel for now. The PIE configuration is not yet compatible with XEN_PVH. Xen PVH generates 32-bit assembly and uses a long jump to transition to 64-bit. A long jump require an absolute reference that is not compatible with PIE. Performance/Size impact: Hackbench (50% and 1600% loads): - PIE disabled: no significant change (-0.50% / +0.50%) - PIE enabled: 7% to 8% on half load, 10% on heavy load. These results are aligned with the different research on user-mode PIE impact on cpu intensive benchmarks (around 10% on x86_64). slab_test (average of 10 runs): - PIE disabled: no significant change (-1% / +1%) - PIE enabled: 3% to 4% Kernbench (average of 10 Half and Optimal runs): Elapsed Time: - PIE disabled: no significant change (-0.22% / +0.06%) - PIE enabled: around 0.50% System Time: - PIE disabled: no significant change (-0.99% / -1.28%) - PIE enabled: 5% to 6% Size of vmlinux (Ubuntu configuration): File size: - PIE disabled: 472928672 bytes (-0.000169% from baseline) - PIE enabled: 216878461 bytes (-54.14% from baseline) .text sections: - PIE disabled: 9373572 bytes (+0.04% from baseline) - PIE enabled: 9499138 bytes (+1.38% from baseline) The big decrease in vmlinux file size is due to the lower number of relocations appended to the file. Signed-off-by: Thomas Garnier --- arch/x86/Kconfig | 7 +++++++ arch/x86/Makefile | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 2632fa8e8945..a419f4110872 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2132,6 +2132,13 @@ config X86_GLOBAL_STACKPROTECTOR bool depends on CC_STACKPROTECTOR +config X86_PIE + bool + depends on X86_64 && !XEN_PVH + select DEFAULT_HIDDEN + select MODULE_REL_CRCS if MODVERSIONS + select X86_GLOBAL_STACKPROTECTOR if CC_STACKPROTECTOR + config HOTPLUG_CPU bool "Support for hot-pluggable CPUs" depends on SMP diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 66af2704f096..05e01588b5af 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -45,8 +45,12 @@ export REALMODE_CFLAGS export BITS ifdef CONFIG_X86_NEED_RELOCS +ifdef CONFIG_X86_PIE + LDFLAGS_vmlinux := -pie -shared -Bsymbolic +else LDFLAGS_vmlinux := --emit-relocs endif +endif # # Prevent GCC from generating any FP code by mistake. @@ -141,7 +145,12 @@ else KBUILD_CFLAGS += $(cflags-y) KBUILD_CFLAGS += -mno-red-zone +ifdef CONFIG_X86_PIE + KBUILD_CFLAGS += -fPIC + KBUILD_CFLAGS_MODULE += -fno-PIC -mcmodel=kernel +else KBUILD_CFLAGS += -mcmodel=kernel +endif # -funit-at-a-time shrinks the kernel .text considerably # unfortunately it makes reading oopses harder.