From patchwork Tue Jul 18 22:33:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9850085 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 5D2DC60392 for ; Tue, 18 Jul 2017 22:37:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4915E285CB for ; Tue, 18 Jul 2017 22:37:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3D51C285D8; Tue, 18 Jul 2017 22:37:28 +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 C0245285CB for ; Tue, 18 Jul 2017 22:37:27 +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 1dXb4l-0000we-S8; Tue, 18 Jul 2017 22:34:43 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dXb4k-0000rE-N2 for xen-devel@lists.xenproject.org; Tue, 18 Jul 2017 22:34:42 +0000 Received: from [193.109.254.147] by server-7.bemta-6.messagelabs.com id D2/30-03557-20D8E695; Tue, 18 Jul 2017 22:34:42 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrJIsWRWlGSWpSXmKPExsXiVRtsqsvYmxd pcGK3isX3LZOZHBg9Dn+4whLAGMWamZeUX5HAmnF9wXvmglcSFRe79zM1MC4V6WLk4hASmM4o 0TplJRuIwyLwikVi17UVzCCOhEA/q8TT3e2sXYycQE6cxOE/Mxkh7EqJmQ82gcWFBJQktm5Yy gwx6h+jxJnXu8ASbAJaEnsa5jOBJEQEZnNILO46wwLiCAh8ZJO4seUwWAuzQAuTxJN/24DKOD iEBfwkjr2uBOlmEVCVeNN0nBnE5hWwklj+aTsrSImEgLHE3y/6ICYnSPg+D8QRlhK9vQ0sExg FFzAyrGLUKE4tKkst0jW00EsqykzPKMlNzMzRNTQw08tNLS5OTE/NSUwq1kvOz93ECAw6BiDY wXhzY8AhRkkOJiVR3q3KeZFCfEn5KZUZicUZ8UWlOanFhxhlODiUJHh9u4FygkWp6akVaZk5w PCHSUtw8CiJ8PqApHmLCxJzizPTIVKnGC05Jh3Y/oWJo2PGz29MHK8m/P/GJMSSl5+XKiXOKw LSIADSkFGaBzcOFqOXGGWlhHkZgQ4U4ilILcrNLEGVf8UozsGoJMx7DGQKT2ZeCdzWV0AHMQE dJOybA3JQSSJCSqqBMdd76uwOr7RS+Y0dr7VDGDa2bf3YlbI2S2TuEe4DCvMvNOUdelPWd1f4 zCdZ37eGip/nxCoE5C2VbDlQHMPW02XWYbfaPIvFfWuFtOKT+SvNs6Um+F1salfqyOlQeD3FU OVv8jm51X/nn73HdYT39OffnV2TLFdzVq1X31j/f6OyZMXBNb6blViKMxINtZiLihMBKhO128 wCAAA= X-Env-Sender: thgarnie@google.com X-Msg-Ref: server-6.tower-27.messagelabs.com!1500417280!107958052!1 X-Originating-IP: [74.125.83.53] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.25; banners=-,-,- X-VirusChecked: Checked Received: (qmail 6511 invoked from network); 18 Jul 2017 22:34:41 -0000 Received: from mail-pg0-f53.google.com (HELO mail-pg0-f53.google.com) (74.125.83.53) by server-6.tower-27.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 18 Jul 2017 22:34:41 -0000 Received: by mail-pg0-f53.google.com with SMTP id u5so19922221pgq.3 for ; Tue, 18 Jul 2017 15:34:41 -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=bpzECORCtR+yUbsXEjnx6CANNXCH8OxCDIAIyxybdpg=; b=i8htuRbndGaBqOZDryocE1ttQf0qy9B3kLve7bjNXb31FxVVqqTC890tgGWfrfbQDF qmLK8fDtoXU5ScxJP45QgXhj6HfBsUhFAnhmB8v0yReOZao5rg6Gup4CpDHayM3troh5 FoFOXJPGpZ3u2FHP9YIi1xgo7yg2XvTcCdNTP+bqf5ZrLPTSVOICvhpQYu/J8yIE9rKO JfDHIA4x5n+ScgSZ6sYj2ulnckquXiH9XJdFfoPB/KVFmXO40w+E2JghgYIA3x0A8bBO QdF4acX4TzOmNiW6EnbL0wDVCO2/n0SLlu5LYEvfAx1tVgwGCka3JWtOULJ2YDhpX4NE e8fA== 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=bpzECORCtR+yUbsXEjnx6CANNXCH8OxCDIAIyxybdpg=; b=uF/zcbRjaYiO9H2zK0RO/i1hslp5oRh0jBzYHB8FW4CDTQooRUPKBT4hRQncRm347o Hn569HJWFNILt2Yng1MNHEvgr9+TeG1qqZZ8IlirSBf9kwGOiqG8ZXE9RRJRzDO+IECf HWlJpYqcGHVHq+mcgJ0IdALAVhQ4bxcOjzG5kMn/8rtCjfNxdDft4qGKjFSN6cg/4v9T +OLRCmnJzb49B0bCAPkYZ0pQQukruU4Dk+ytD2fZx8wGd8ul3E07zdoE+RQ4IBqkTiNx mGVIBADTMWA3d+SH6LaARJEDYF3eCLFJ13PTghE7f99lgSSeoxJrPkVuVGEmOeQ7QK2h mkXQ== X-Gm-Message-State: AIVw11395IQ19bfKBs5ni1xvWRLxebsF1cHN91c2FD6psU4zVEMZKgHR tuew3hq3sxQHHs2w X-Received: by 10.84.231.131 with SMTP id g3mr4224469plk.176.1500417279407; Tue, 18 Jul 2017 15:34:39 -0700 (PDT) Received: from skynet.sea.corp.google.com ([100.100.206.164]) by smtp.gmail.com with ESMTPSA id b6sm6444242pgn.67.2017.07.18.15.34.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Jul 2017 15:34:38 -0700 (PDT) From: Thomas Garnier To: Herbert Xu , "David S . Miller" , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Thomas Garnier , Arnd Bergmann , Matthias Kaehlcke , Boris Ostrovsky , Juergen Gross , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Andy Lutomirski , Borislav Petkov , "Kirill A . Shutemov" , Brian Gerst , Borislav Petkov , Christian Borntraeger , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Tejun Heo , Christoph Lameter , Kees Cook , Paul Gortmaker , Chris Metcalf , "Paul E . McKenney" , Andrew Morton , Christopher Li , Dou Liyang , Masahiro Yamada , Daniel Borkmann , Markus Trippelsdorf , Peter Foley , Steven Rostedt , Tim Chen , Ard Biesheuvel , Catalin Marinas , Matthew Wilcox , Michal Hocko , Rob Landley , Jiri Kosina , "H . J . Lu" , Paul Bolle , Baoquan He , Daniel Micay Date: Tue, 18 Jul 2017 15:33:30 -0700 Message-Id: <20170718223333.110371-20-thgarnie@google.com> X-Mailer: git-send-email 2.13.2.932.g7449e964c-goog In-Reply-To: <20170718223333.110371-1-thgarnie@google.com> References: <20170718223333.110371-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 19/22] 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 | 6 ++++++ arch/x86/Makefile | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 781521b7cf9e..b26ee6751021 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2080,6 +2080,12 @@ config RANDOMIZE_MEMORY_PHYSICAL_PADDING If unsure, leave at the default value. +config X86_PIE + bool + depends on X86_64 && !XEN_PVH + select DEFAULT_HIDDEN + select MODULE_REL_CRCS if MODVERSIONS + config HOTPLUG_CPU bool "Support for hot-pluggable CPUs" depends on SMP diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 1e902f926be3..452a9621af8f 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. @@ -132,7 +136,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.