From patchwork Wed Oct 11 20:30:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 10000539 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 93B9E6037F for ; Wed, 11 Oct 2017 20:33:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 884D228B37 for ; Wed, 11 Oct 2017 20:33:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7B81328B6A; Wed, 11 Oct 2017 20:33:16 +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 F107B28B37 for ; Wed, 11 Oct 2017 20:33:15 +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 1e2NfS-0001AJ-HA; Wed, 11 Oct 2017 20:31:50 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e2NfR-00016i-6c for xen-devel@lists.xenproject.org; Wed, 11 Oct 2017 20:31:49 +0000 Received: from [85.158.139.211] by server-6.bemta-5.messagelabs.com id 35/54-07895-4BF7ED95; Wed, 11 Oct 2017 20:31:48 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrHIsWRWlGSWpSXmKPExsVyMfTAWt3N9fc iDXp3SVl83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBmnmnwLvitXdCw6wdrAuF+6i5GTQ0hgBqPE wp7ALkYuDhaBVywSKz8tYAJxJAT6WSX+fn/BCFIlIZAlsXbVMjYIO02is2cZO4RdLnF+0wM2i ElKEls3LGUGaRYS+Mco8ebhOrAEm4CWxJ6G+WBTRQROCEts+vCbEcRhFjjDJHG87yRrFyMHh7 CAh8SurwkgDSwCqhI/Tx1mBbF5BSwlWm73QG2zkDh27DALiM0JFP++8RAzxGYLiZWzzrNOYBR cwMiwilGjOLWoLLVI19BYL6koMz2jJDcxM0fX0MBULze1uDgxPTUnMalYLzk/dxMjMOQYgGAH 479tnocYJTmYlER5H8bcixTiS8pPqcxILM6ILyrNSS0+xCjDwaEkwXu3DignWJSanlqRlpkDD H6YtAQHj5IIbxNImre4IDG3ODMdInWK0Z7jwp1Lf5g4Duy5BSQf3bgLJDtuAkkhlrz8vFQpcd 5SkDYBkLaM0jy4obBovcQoKyXMywh0phBPQWpRbmYJqvwrRnEORiVh3sMgU3gy80rgdr8COos J6CzRtDsgZ5UkIqSkGhjlv9zi+Z1g3yAYuU1+b5zyh4QiLrsfPC5WMoI5c8LEv4hUXz4dsMGg 98EKs5Wem1ddF03z2r2y4WzyonC1X/s3mz1O1Rfd3hHgr+pkFdH7QcvQcbIQr6FNoo+AefvF9 fq3QiPmL/PM2vDssdGWF28v7xZfmTo1a/rhuMuPn5wIOrXilbvbgSQlluKMREMt5qLiRABEwH DX0QIAAA== X-Env-Sender: thgarnie@google.com X-Msg-Ref: server-12.tower-206.messagelabs.com!1507753906!70276834!1 X-Originating-IP: [209.85.192.173] 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 13126 invoked from network); 11 Oct 2017 20:31:47 -0000 Received: from mail-pf0-f173.google.com (HELO mail-pf0-f173.google.com) (209.85.192.173) by server-12.tower-206.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 11 Oct 2017 20:31:47 -0000 Received: by mail-pf0-f173.google.com with SMTP id b79so1952705pfk.5 for ; Wed, 11 Oct 2017 13:31:47 -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=hx8j/aAyhLIqLNFLGSNLcQhQo3LNYGiCKQDS6WAZduI=; b=c/aGnDiBaGHoF3TFma6riHarNoKEJKQ+UpcodG7G+fPVlls8kh9FfuRrEhVn+VIiSw E0NCgrpTkbWyyAxWYckjCmChHZB+/W+1nD6ljsBIH/EH4inqkNZsfXWZcAUPtAhaARk6 Ij55f2/i50yWEo5af0ihVwn1vTxQB2DI7w0m/pkH1bYUJku7ywvkrrE5S/ABHe0jgkeU +qjNRtM3WWSF8K5XOUmWvOinpYRuc2RMg+iBr1GLGePDQhgOCbfGHJXiYi1TUPjCTJlX tDnDgiVbx6q4XNK0VHCJ/qKEFo8H0L4x41gGCixUw5mGMxsC+PgHBImwBjkdZME3KARt 9UNA== 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=hx8j/aAyhLIqLNFLGSNLcQhQo3LNYGiCKQDS6WAZduI=; b=M1H1aILakwy31tO+1wwVaU27yHoRmvwiBmZhYLffXcBpjHtJoIMKU56HHRsb6ouGiv vJiWvXjeQk12mMoen+rDFatumZaoM5fzjB+52AriU6hg5jQdfl3hTGlKtPh41fJ9pIsH rosB+zgsTsX6TVk0p00DuLMk3iZg/2a5tgeUR7xOzjv9AHoCggCle9o6ScWzqZdeJMrq EixN/XnVQlafGvSW5CbxaYXZpBo5BHacieI/lEllsQ3pdNCXKJK3qBBbm8dl6F4caon2 A7ZkVhSTP2qqEH6Frgk9M24Z6Hvyq23x6lYCodCI0CQpBbBuxLJ5Uq/pCQeHbipbC1se uFiA== X-Gm-Message-State: AMCzsaXRSGc3LFBs0/4K8raOreHNG7U67gHdps35Dq5AmXMppOoMK6If az/QVr27YRHuKhB/WeLumCpmtg== X-Google-Smtp-Source: AOwi7QDd7LA8JkbIzjFwDRJ4dCCDlW3DjkT2lutQtGOVMqRbM4Jkr/PlF1dIGXlKF0vBKI5OBzdWPw== X-Received: by 10.84.140.35 with SMTP id 32mr181558pls.378.1507753905346; Wed, 11 Oct 2017 13:31:45 -0700 (PDT) Received: from skynet.sea.corp.google.com ([172.31.92.33]) by smtp.gmail.com with ESMTPSA id n12sm20691913pfb.149.2017.10.11.13.31.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 11 Oct 2017 13:31:44 -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 , Kees Cook , Andrey Ryabinin , Matthias Kaehlcke , Tom Lendacky , Andy Lutomirski , "Kirill A . Shutemov" , Borislav Petkov , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Juergen Gross , Chris Wright , Alok Kataria , Rusty Russell , Tejun Heo , Christoph Lameter , Boris Ostrovsky , Paul Gortmaker , Andrew Morton , Alexey Dobriyan , "Paul E . McKenney" , Nicolas Pitre , Borislav Petkov , "Luis R . Rodriguez" , Greg Kroah-Hartman , Christopher Li , Steven Rostedt , Jason Baron , Mika Westerberg , Dou Liyang , "Rafael J . Wysocki" , Lukas Wunner , Masahiro Yamada , Alexei Starovoitov , Daniel Borkmann , Markus Trippelsdorf , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Rik van Riel , David Howells , Ard Biesheuvel , Waiman Long , Kyle Huey , Jonathan Corbet , Michal Hocko , Peter Foley , Paul Bolle , Jiri Kosina , "H . J . Lu" , Rob Landley , Baoquan He , =?UTF-8?q?Jan=20H=20=2E=20Sch=C3=B6nherr?= , Daniel Micay Date: Wed, 11 Oct 2017 13:30:25 -0700 Message-Id: <20171011203027.11248-26-thgarnie@google.com> X-Mailer: git-send-email 2.15.0.rc0.271.g36b669edcc-goog In-Reply-To: <20171011203027.11248-1-thgarnie@google.com> References: <20171011203027.11248-1-thgarnie@google.com> Cc: linux-arch@vger.kernel.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, x86@kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-sparse@vger.kernel.org, linux-crypto@vger.kernel.org, kernel-hardening@lists.openwall.com, xen-devel@lists.xenproject.org Subject: [Xen-devel] [PATCH v1 25/27] x86/pie: Add option to build the kernel as PIE 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 current limit. The --emit-relocs linker option was kept instead of using -pie to limit the impact on mapped sections. Any incompatible relocation will be catch by the arch/x86/tools/relocs binary at compile time. If segment based stack cookies are enabled, try to use the compiler option to select the segment register. If not available, automatically enabled global stack cookie in auto mode. Otherwise, recommend compiler update or global stack cookie option. Performance/Size impact: Size of vmlinux (Default configuration): File size: - PIE disabled: +0.000031% - PIE enabled: -3.210% (less relocations) .text section: - PIE disabled: +0.000644% - PIE enabled: +0.837% Size of vmlinux (Ubuntu configuration): File size: - PIE disabled: -0.201% - PIE enabled: -0.082% .text section: - PIE disabled: same - PIE enabled: +1.319% Size of vmlinux (Default configuration + ORC): File size: - PIE enabled: -3.167% .text section: - PIE enabled: +0.814% Size of vmlinux (Ubuntu configuration + ORC): File size: - PIE enabled: -3.167% .text section: - PIE enabled: +1.26% The size increase is mainly due to not having access to the 32-bit signed relocation that can be used with mcmodel=kernel. A small part is due to reduced optimization for PIE code. This bug [1] was opened with gcc to provide a better code generation for kernel PIE. Hackbench (50% and 1600% on thread/process for pipe/sockets): - PIE disabled: no significant change (avg +0.1% on latest test). - PIE enabled: between -0.50% to +0.86% in average (default and Ubuntu config). slab_test (average of 10 runs): - PIE disabled: no significant change (-2% on latest run, likely noise). - PIE enabled: between -1% and +0.8% on latest runs. Kernbench (average of 10 Half and Optimal runs): Elapsed Time: - PIE disabled: no significant change (avg -0.239%) - PIE enabled: average +0.07% System Time: - PIE disabled: no significant change (avg -0.277%) - PIE enabled: average +0.7% [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82303 Signed-off-by: Thomas Garnier merge PIE --- arch/x86/Kconfig | 7 +++++++ arch/x86/Makefile | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 1f2b731c9ec3..bbd28a46ab55 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2148,6 +2148,13 @@ config X86_GLOBAL_STACKPROTECTOR If unsure, say N +config X86_PIE + bool + depends on X86_64 + select DEFAULT_HIDDEN + select DYNAMIC_MODULE_BASE + 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 b592d57c531b..beae9504c3f4 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -135,7 +135,34 @@ else KBUILD_CFLAGS += -mno-red-zone ifdef CONFIG_X86_PIE + KBUILD_CFLAGS += -fPIC KBUILD_LDFLAGS_MODULE += -T $(srctree)/arch/x86/kernel/module.lds + +ifndef CONFIG_CC_STACKPROTECTOR_NONE +ifndef CONFIG_X86_GLOBAL_STACKPROTECTOR + stackseg-flag := -mstack-protector-guard-reg=%gs + ifeq ($(call cc-option-yn,$(stackseg-flag)),n) + ifdef CONFIG_CC_STACKPROTECTOR_AUTO + $(warning Cannot use CONFIG_CC_STACKPROTECTOR_* while \ + building a position independent kernel. \ + Default to global stack protector \ + (CONFIG_X86_GLOBAL_STACKPROTECTOR).) + CONFIG_X86_GLOBAL_STACKPROTECTOR := y + KBUILD_CFLAGS += -DCONFIG_X86_GLOBAL_STACKPROTECTOR + KBUILD_AFLAGS += -DCONFIG_X86_GLOBAL_STACKPROTECTOR + else + $(error echo Cannot use \ + CONFIG_CC_STACKPROTECTOR_(REGULAR|STRONG) \ + while building a position independent binary \ + Update your compiler or use \ + CONFIG_X86_GLOBAL_STACKPROTECTOR) + endif + else + KBUILD_CFLAGS += $(stackseg-flag) + endif +endif +endif + else KBUILD_CFLAGS += -mcmodel=kernel endif