[PATCH] x86-64: Force linker to use 2MB page size

From: H.J. Lu
Date: Mon Mar 19 2018 - 16:58:00 EST


Binutils 2.31 will enable -z separate-code by default for x86 to avoid
mixing code pages with data to improve cache performance as well as
security. To reduce x86-64 executable and shared object sizes, the
maximum page size is reduced from 2MB to 4KB. But x86-64 kernel must
be aligned to 2MB. Pass -z max-page-size=0x200000 to linker to force
2MB page size regardless of the default page size used by linker.

Tested with Linux kernel 4.15.6 on x86-64.

Signed-off-by: H.J. Lu <hjl.tools@xxxxxxxxx>

--
H.J.
From c5c27b82d2e19add5372a6b2eaa00119e896d9ff Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@xxxxxxxxx>
Date: Fri, 16 Feb 2018 14:07:38 -0800
Subject: [PATCH] x86-64: Force linker to use 2MB page size

Binutils 2.31 will enable -z separate-code by default for x86 to avoid
mixing code pages with data to improve cache performance as well as
security. To reduce x86-64 executable and shared object sizes, the
maximum page size is reduced from 2MB to 4KB. But x86-64 kernel must
be aligned to 2MB. Pass -z max-page-size=0x200000 to linker to force
2MB page size regardless of the default page size used by linker.

Tested with Linux kernel 4.15.6 on x86-64.

Signed-off-by: H.J. Lu <hjl.tools@xxxxxxxxx>
---
arch/x86/Makefile | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 68f4963501ae..dc2419883c97 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -229,6 +229,12 @@ KBUILD_AFLAGS += $(cfi) $(cfi-sigframe) $(cfi-sections) $(asinstr) $(avx_instr)
KBUILD_CFLAGS += $(cfi) $(cfi-sigframe) $(cfi-sections) $(asinstr) $(avx_instr) $(avx2_instr) $(avx512_instr) $(sha1_ni_instr) $(sha256_ni_instr)

LDFLAGS := -m elf_$(UTS_MACHINE)
+ifdef CONFIG_X86_64
+# X86-64 kernel must be aligned to 2MB. Pass -z max-page-size=0x200000 to
+# linker to force 2MB page size regardless of the default page size used
+# by linker.
+LDFLAGS += $(call ld-option, -z max-page-size=0x200000)
+endif

# Speed up the build
KBUILD_CFLAGS += -pipe
--
2.14.3