Re: [PATCH 2/3] kbuild: add cc-if-name-version and compiler-specific variants

2017-11-30 Thread Sami Tolvanen
On Thu, Nov 30, 2017 at 09:21:49PM +0900, Masahiro Yamada wrote:
> BTW, did this patch work in your test?

Yes, but it looks like /bin/sh is symlinked to bash on my system. I can
reproduce the issue with dash.

> As for the macro names, maybe
> 
> __cc-ifversion, __cc-if-fullversion for consistency?

> I think you can do like follows to avoid code duplication.
> 
> # backward compatibility
> cc-ifversion = $(gcc-ifversion)
> cc-if-fullversion = $(gcc-if-fullversion)

Sure, both changes sound good to me. I'll fix these in v2.

Sami


Re: [PATCH 2/3] kbuild: add cc-if-name-version and compiler-specific variants

2017-11-30 Thread Sami Tolvanen
On Thu, Nov 30, 2017 at 09:21:49PM +0900, Masahiro Yamada wrote:
> BTW, did this patch work in your test?

Yes, but it looks like /bin/sh is symlinked to bash on my system. I can
reproduce the issue with dash.

> As for the macro names, maybe
> 
> __cc-ifversion, __cc-if-fullversion for consistency?

> I think you can do like follows to avoid code duplication.
> 
> # backward compatibility
> cc-ifversion = $(gcc-ifversion)
> cc-if-fullversion = $(gcc-if-fullversion)

Sure, both changes sound good to me. I'll fix these in v2.

Sami


[PATCH 4/7] arm64: fix -m for GNU gold

2017-11-29 Thread Sami Tolvanen
GNU gold supports different emulations than bfd. Use aarch64_elf64_*_vec
instead of aarch64linux.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
Acked-by: Yury Norov <yno...@caviumnetworks.com>
---
 arch/arm64/Makefile | 8 
 1 file changed, 8 insertions(+)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index b35788c909f1..82a0df6e865e 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -64,14 +64,22 @@ KBUILD_CPPFLAGS += -mbig-endian
 CHECKFLAGS += -D__AARCH64EB__
 AS += -EB
 LD += -EB
+ifeq ($(ld-name),gold)
+LDFLAGS+= -maarch64_elf64_be_vec
+else
 LDFLAGS+= -maarch64linuxb
+endif
 UTS_MACHINE:= aarch64_be
 else
 KBUILD_CPPFLAGS+= -mlittle-endian
 CHECKFLAGS += -D__AARCH64EL__
 AS += -EL
 LD += -EL
+ifeq ($(ld-name),gold)
+LDFLAGS+= -maarch64_elf64_le_vec
+else
 LDFLAGS+= -maarch64linux
+endif
 UTS_MACHINE:= aarch64
 endif
 
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 1/7] kbuild: add ld-name macro and support for GNU gold

2017-11-29 Thread Sami Tolvanen
GNU gold may require different flags than GNU ld. Add a macro for
detecting the linker and conditionally add gold specific flags from
LDFLAGS_GOLD.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
Reviewed-by: Nick Desaulniers <ndesaulni...@google.com>
---
 Makefile   | 5 +
 scripts/Kbuild.include | 4 
 2 files changed, 9 insertions(+)

diff --git a/Makefile b/Makefile
index f761bf475ba5..a16c98d6e3ba 100644
--- a/Makefile
+++ b/Makefile
@@ -814,6 +814,11 @@ include scripts/Makefile.kasan
 include scripts/Makefile.extrawarn
 include scripts/Makefile.ubsan
 
+# Add any flags specific to ld.gold
+ifeq ($(ld-name),gold)
+LDFLAGS+= $(LDFLAGS_GOLD)
+endif
+
 # Add any arch overrides and user supplied CPPFLAGS, AFLAGS and CFLAGS as the
 # last assignments
 KBUILD_CPPFLAGS += $(ARCH_CPPFLAGS) $(KCPPFLAGS)
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index b6d7d347b203..a7c7843c2cf1 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -277,6 +277,10 @@ ld-option = $(call try-run-cached,\
 # Important: no spaces around options
 ar-option = $(call try-run-cached, $(AR) rc$(1) "$$TMP",$(1),$(2))
 
+# ld-name
+# Expands to either bfd or gold
+ld-name = $(shell $(LD) -v 2>&1 | grep -q "GNU gold" && echo gold || echo bfd)
+
 # ld-version
 # Note this is mainly for HJ Lu's 3 number binutil versions
 ld-version = $(call shell-cached,$(LD) --version | 
$(srctree)/scripts/ld-version.sh)
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 4/7] arm64: fix -m for GNU gold

2017-11-29 Thread Sami Tolvanen
GNU gold supports different emulations than bfd. Use aarch64_elf64_*_vec
instead of aarch64linux.

Signed-off-by: Sami Tolvanen 
Acked-by: Yury Norov 
---
 arch/arm64/Makefile | 8 
 1 file changed, 8 insertions(+)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index b35788c909f1..82a0df6e865e 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -64,14 +64,22 @@ KBUILD_CPPFLAGS += -mbig-endian
 CHECKFLAGS += -D__AARCH64EB__
 AS += -EB
 LD += -EB
+ifeq ($(ld-name),gold)
+LDFLAGS+= -maarch64_elf64_be_vec
+else
 LDFLAGS+= -maarch64linuxb
+endif
 UTS_MACHINE:= aarch64_be
 else
 KBUILD_CPPFLAGS+= -mlittle-endian
 CHECKFLAGS += -D__AARCH64EL__
 AS += -EL
 LD += -EL
+ifeq ($(ld-name),gold)
+LDFLAGS+= -maarch64_elf64_le_vec
+else
 LDFLAGS+= -maarch64linux
+endif
 UTS_MACHINE:= aarch64
 endif
 
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 1/7] kbuild: add ld-name macro and support for GNU gold

2017-11-29 Thread Sami Tolvanen
GNU gold may require different flags than GNU ld. Add a macro for
detecting the linker and conditionally add gold specific flags from
LDFLAGS_GOLD.

Signed-off-by: Sami Tolvanen 
Reviewed-by: Nick Desaulniers 
---
 Makefile   | 5 +
 scripts/Kbuild.include | 4 
 2 files changed, 9 insertions(+)

diff --git a/Makefile b/Makefile
index f761bf475ba5..a16c98d6e3ba 100644
--- a/Makefile
+++ b/Makefile
@@ -814,6 +814,11 @@ include scripts/Makefile.kasan
 include scripts/Makefile.extrawarn
 include scripts/Makefile.ubsan
 
+# Add any flags specific to ld.gold
+ifeq ($(ld-name),gold)
+LDFLAGS+= $(LDFLAGS_GOLD)
+endif
+
 # Add any arch overrides and user supplied CPPFLAGS, AFLAGS and CFLAGS as the
 # last assignments
 KBUILD_CPPFLAGS += $(ARCH_CPPFLAGS) $(KCPPFLAGS)
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index b6d7d347b203..a7c7843c2cf1 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -277,6 +277,10 @@ ld-option = $(call try-run-cached,\
 # Important: no spaces around options
 ar-option = $(call try-run-cached, $(AR) rc$(1) "$$TMP",$(1),$(2))
 
+# ld-name
+# Expands to either bfd or gold
+ld-name = $(shell $(LD) -v 2>&1 | grep -q "GNU gold" && echo gold || echo bfd)
+
 # ld-version
 # Note this is mainly for HJ Lu's 3 number binutil versions
 ld-version = $(call shell-cached,$(LD) --version | 
$(srctree)/scripts/ld-version.sh)
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 3/7] kbuild: fix LD_DEAD_CODE_DATA_ELIMINATION with GNU gold

2017-11-29 Thread Sami Tolvanen
Don't remove .head.text or .exitcall.exit when linking with --gc-sections,
and include .init.text.* in .init.text and .init.rodata.* in .init.rodata.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 include/asm-generic/vmlinux.lds.h | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/asm-generic/vmlinux.lds.h 
b/include/asm-generic/vmlinux.lds.h
index ee8b707d9fa9..eb4199c12bd2 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -515,7 +515,7 @@
VMLINUX_SYMBOL(__softirqentry_text_end) = .;
 
 /* Section used for early init (in .S files) */
-#define HEAD_TEXT  *(.head.text)
+#define HEAD_TEXT  KEEP(*(.head.text))
 
 #define HEAD_TEXT_SECTION  
\
.head.text : AT(ADDR(.head.text) - LOAD_OFFSET) {   \
@@ -560,7 +560,7 @@
MEM_DISCARD(init.data)  \
KERNEL_CTORS()  \
MCOUNT_REC()\
-   *(.init.rodata) \
+   *(.init.rodata .init.rodata.*)  \
FTRACE_EVENTS() \
TRACE_SYSCALLS()\
KPROBE_BLACKLIST()  \
@@ -579,7 +579,7 @@
EARLYCON_TABLE()
 
 #define INIT_TEXT  \
-   *(.init.text)   \
+   *(.init.text .init.text.*)  \
*(.text.startup)\
MEM_DISCARD(init.text)
 
@@ -596,7 +596,7 @@
MEM_DISCARD(exit.text)
 
 #define EXIT_CALL  \
-   *(.exitcall.exit)
+   KEEP(*(.exitcall.exit))
 
 /*
  * bss (Block Started by Symbol) - uninitialized data
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 3/7] kbuild: fix LD_DEAD_CODE_DATA_ELIMINATION with GNU gold

2017-11-29 Thread Sami Tolvanen
Don't remove .head.text or .exitcall.exit when linking with --gc-sections,
and include .init.text.* in .init.text and .init.rodata.* in .init.rodata.

Signed-off-by: Sami Tolvanen 
---
 include/asm-generic/vmlinux.lds.h | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/asm-generic/vmlinux.lds.h 
b/include/asm-generic/vmlinux.lds.h
index ee8b707d9fa9..eb4199c12bd2 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -515,7 +515,7 @@
VMLINUX_SYMBOL(__softirqentry_text_end) = .;
 
 /* Section used for early init (in .S files) */
-#define HEAD_TEXT  *(.head.text)
+#define HEAD_TEXT  KEEP(*(.head.text))
 
 #define HEAD_TEXT_SECTION  
\
.head.text : AT(ADDR(.head.text) - LOAD_OFFSET) {   \
@@ -560,7 +560,7 @@
MEM_DISCARD(init.data)  \
KERNEL_CTORS()  \
MCOUNT_REC()\
-   *(.init.rodata) \
+   *(.init.rodata .init.rodata.*)  \
FTRACE_EVENTS() \
TRACE_SYSCALLS()\
KPROBE_BLACKLIST()  \
@@ -579,7 +579,7 @@
EARLYCON_TABLE()
 
 #define INIT_TEXT  \
-   *(.init.text)   \
+   *(.init.text .init.text.*)  \
*(.text.startup)\
MEM_DISCARD(init.text)
 
@@ -596,7 +596,7 @@
MEM_DISCARD(exit.text)
 
 #define EXIT_CALL  \
-   *(.exitcall.exit)
+   KEEP(*(.exitcall.exit))
 
 /*
  * bss (Block Started by Symbol) - uninitialized data
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 5/7] arm64: keep .altinstructions and .altinstr_replacement

2017-11-29 Thread Sami Tolvanen
Make sure the linker doesn't remove .altinstructions or
.altinstr_replacement when CONFIG_LD_DEAD_CODE_DATA_ELIMINATION is
enabled.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/kernel/vmlinux.lds.S | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index 7da3e5c366a0..15479995869c 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -138,11 +138,11 @@ SECTIONS
. = ALIGN(4);
.altinstructions : {
__alt_instructions = .;
-   *(.altinstructions)
+   KEEP(*(.altinstructions))
__alt_instructions_end = .;
}
.altinstr_replacement : {
-   *(.altinstr_replacement)
+   KEEP(*(.altinstr_replacement))
}
 
. = ALIGN(PAGE_SIZE);
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 5/7] arm64: keep .altinstructions and .altinstr_replacement

2017-11-29 Thread Sami Tolvanen
Make sure the linker doesn't remove .altinstructions or
.altinstr_replacement when CONFIG_LD_DEAD_CODE_DATA_ELIMINATION is
enabled.

Signed-off-by: Sami Tolvanen 
---
 arch/arm64/kernel/vmlinux.lds.S | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index 7da3e5c366a0..15479995869c 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -138,11 +138,11 @@ SECTIONS
. = ALIGN(4);
.altinstructions : {
__alt_instructions = .;
-   *(.altinstructions)
+   KEEP(*(.altinstructions))
__alt_instructions_end = .;
}
.altinstr_replacement : {
-   *(.altinstr_replacement)
+   KEEP(*(.altinstr_replacement))
}
 
. = ALIGN(PAGE_SIZE);
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 6/7] arm64: explicitly pass --no-fix-cortex-a53-843419 to GNU gold

2017-11-29 Thread Sami Tolvanen
Some versions of GNU gold are known to produce broken code with
--fix-cortex-a53-843419 as explained in this bug:

  https://sourceware.org/bugzilla/show_bug.cgi?id=21491

If ARM64_ERRATUM_843419 is disabled and we're using GNU gold, pass
--no-fix-cortex-a53-843419 to the linker to ensure the erratum
fix is not used even if the linker is configured to enable it by
default.

This change also adds a warning if the erratum fix is enabled and
gold version <1.14 is used.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/Makefile | 9 +
 1 file changed, 9 insertions(+)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index 82a0df6e865e..81a12713f7af 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -26,10 +26,19 @@ ifeq ($(CONFIG_ARM64_ERRATUM_843419),y)
   ifeq ($(call ld-option, --fix-cortex-a53-843419),)
 $(warning ld does not support --fix-cortex-a53-843419; kernel may be 
susceptible to erratum)
   else
+ifeq ($(call gold-ifversion, -lt, 11400, y), y)
+$(warning This version of GNU gold may generate incorrect code with 
--fix-cortex-a53-843419;\
+   see https://sourceware.org/bugzilla/show_bug.cgi?id=21491)
+endif
 LDFLAGS_vmlinux+= --fix-cortex-a53-843419
   endif
 endif
 
+ifeq ($(CONFIG_ARM64_ERRATUM_843419),)
+# https://sourceware.org/bugzilla/show_bug.cgi?id=21491
+LDFLAGS_GOLD   += --no-fix-cortex-a53-843419
+endif
+
 KBUILD_DEFCONFIG := defconfig
 
 # Check for binutils support for specific extensions
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 6/7] arm64: explicitly pass --no-fix-cortex-a53-843419 to GNU gold

2017-11-29 Thread Sami Tolvanen
Some versions of GNU gold are known to produce broken code with
--fix-cortex-a53-843419 as explained in this bug:

  https://sourceware.org/bugzilla/show_bug.cgi?id=21491

If ARM64_ERRATUM_843419 is disabled and we're using GNU gold, pass
--no-fix-cortex-a53-843419 to the linker to ensure the erratum
fix is not used even if the linker is configured to enable it by
default.

This change also adds a warning if the erratum fix is enabled and
gold version <1.14 is used.

Signed-off-by: Sami Tolvanen 
---
 arch/arm64/Makefile | 9 +
 1 file changed, 9 insertions(+)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index 82a0df6e865e..81a12713f7af 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -26,10 +26,19 @@ ifeq ($(CONFIG_ARM64_ERRATUM_843419),y)
   ifeq ($(call ld-option, --fix-cortex-a53-843419),)
 $(warning ld does not support --fix-cortex-a53-843419; kernel may be 
susceptible to erratum)
   else
+ifeq ($(call gold-ifversion, -lt, 11400, y), y)
+$(warning This version of GNU gold may generate incorrect code with 
--fix-cortex-a53-843419;\
+   see https://sourceware.org/bugzilla/show_bug.cgi?id=21491)
+endif
 LDFLAGS_vmlinux+= --fix-cortex-a53-843419
   endif
 endif
 
+ifeq ($(CONFIG_ARM64_ERRATUM_843419),)
+# https://sourceware.org/bugzilla/show_bug.cgi?id=21491
+LDFLAGS_GOLD   += --no-fix-cortex-a53-843419
+endif
+
 KBUILD_DEFCONFIG := defconfig
 
 # Check for binutils support for specific extensions
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 2/7] kbuild: add ld-if-name-version and linker-specific macros

2017-11-29 Thread Sami Tolvanen
Add macros for testing both linker name and version.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 scripts/Kbuild.include | 12 
 1 file changed, 12 insertions(+)

diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index a7c7843c2cf1..5d286d69e8dc 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -289,6 +289,18 @@ ld-version = $(call shell-cached,$(LD) --version | 
$(srctree)/scripts/ld-version
 # Usage:  $(call ld-ifversion, -ge, 22252, y)
 ld-ifversion = $(shell [ $(ld-version) $(1) $(2) ] && echo $(3) || echo $(4))
 
+# ld-if-name-version
+# Usage:  $(call ld-if-name-version, gold, -ge, 11200, y)
+ld-if-name-version = $(shell [ $(ld-name) == $(1) ] && [ $(ld-version) $(2) 
$(3) ] && echo $(4) || echo $(5))
+
+# bfd-ifversion
+# Usage:  $(call bfd-ifversion, -ge, 22700, y)
+bfd-ifversion = $(call ld-if-name-version, bfd, $(1), $(2), $(3), $(4))
+
+# gold-ifversion
+# Usage:  $(call gold-ifversion, -ge, 11200, y)
+gold-ifversion = $(call ld-if-name-version, gold, $(1), $(2), $(3), $(4))
+
 ##
 
 ###
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 7/7] arm64: add a workaround for GNU gold with ARM64_MODULE_PLTS

2017-11-29 Thread Sami Tolvanen
All current versions of GNU gold crash when linking kernel modules
with ARM64_MODULE_PLTS due to a known bug:

  https://sourceware.org/bugzilla/show_bug.cgi?id=14592

To work around the problem, this change removes NOLOAD from .plt
and .init.plt.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/kernel/module.lds | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/module.lds b/arch/arm64/kernel/module.lds
index f7c9781a9d48..eacb5c67f61e 100644
--- a/arch/arm64/kernel/module.lds
+++ b/arch/arm64/kernel/module.lds
@@ -1,4 +1,4 @@
 SECTIONS {
-   .plt (NOLOAD) : { BYTE(0) }
-   .init.plt (NOLOAD) : { BYTE(0) }
+   .plt : { BYTE(0) }
+   .init.plt : { BYTE(0) }
 }
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 2/7] kbuild: add ld-if-name-version and linker-specific macros

2017-11-29 Thread Sami Tolvanen
Add macros for testing both linker name and version.

Signed-off-by: Sami Tolvanen 
---
 scripts/Kbuild.include | 12 
 1 file changed, 12 insertions(+)

diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index a7c7843c2cf1..5d286d69e8dc 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -289,6 +289,18 @@ ld-version = $(call shell-cached,$(LD) --version | 
$(srctree)/scripts/ld-version
 # Usage:  $(call ld-ifversion, -ge, 22252, y)
 ld-ifversion = $(shell [ $(ld-version) $(1) $(2) ] && echo $(3) || echo $(4))
 
+# ld-if-name-version
+# Usage:  $(call ld-if-name-version, gold, -ge, 11200, y)
+ld-if-name-version = $(shell [ $(ld-name) == $(1) ] && [ $(ld-version) $(2) 
$(3) ] && echo $(4) || echo $(5))
+
+# bfd-ifversion
+# Usage:  $(call bfd-ifversion, -ge, 22700, y)
+bfd-ifversion = $(call ld-if-name-version, bfd, $(1), $(2), $(3), $(4))
+
+# gold-ifversion
+# Usage:  $(call gold-ifversion, -ge, 11200, y)
+gold-ifversion = $(call ld-if-name-version, gold, $(1), $(2), $(3), $(4))
+
 ##
 
 ###
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 7/7] arm64: add a workaround for GNU gold with ARM64_MODULE_PLTS

2017-11-29 Thread Sami Tolvanen
All current versions of GNU gold crash when linking kernel modules
with ARM64_MODULE_PLTS due to a known bug:

  https://sourceware.org/bugzilla/show_bug.cgi?id=14592

To work around the problem, this change removes NOLOAD from .plt
and .init.plt.

Signed-off-by: Sami Tolvanen 
---
 arch/arm64/kernel/module.lds | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/module.lds b/arch/arm64/kernel/module.lds
index f7c9781a9d48..eacb5c67f61e 100644
--- a/arch/arm64/kernel/module.lds
+++ b/arch/arm64/kernel/module.lds
@@ -1,4 +1,4 @@
 SECTIONS {
-   .plt (NOLOAD) : { BYTE(0) }
-   .init.plt (NOLOAD) : { BYTE(0) }
+   .plt : { BYTE(0) }
+   .init.plt : { BYTE(0) }
 }
-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 0/7] Add support for GNU gold

2017-11-29 Thread Sami Tolvanen
These patches add macros for detecting the linker name and version,
and apply fixes and workarounds needed to link the arm64 kernel with
GNU gold.

Sami Tolvanen (7):
  kbuild: add ld-name macro and support for GNU gold
  kbuild: add ld-if-name-version and linker-specific macros
  kbuild: fix LD_DEAD_CODE_DATA_ELIMINATION with GNU gold
  arm64: fix -m for GNU gold
  arm64: keep .altinstructions and .altinstr_replacement
  arm64: explicitly pass --no-fix-cortex-a53-843419 to GNU gold
  arm64: add a workaround for GNU gold with ARM64_MODULE_PLTS

 Makefile  |  5 +
 arch/arm64/Makefile   | 17 +
 arch/arm64/kernel/module.lds  |  4 ++--
 arch/arm64/kernel/vmlinux.lds.S   |  4 ++--
 include/asm-generic/vmlinux.lds.h |  8 
 scripts/Kbuild.include| 16 
 6 files changed, 46 insertions(+), 8 deletions(-)

-- 
2.15.0.531.g2ccb3012c9-goog



[PATCH 0/7] Add support for GNU gold

2017-11-29 Thread Sami Tolvanen
These patches add macros for detecting the linker name and version,
and apply fixes and workarounds needed to link the arm64 kernel with
GNU gold.

Sami Tolvanen (7):
  kbuild: add ld-name macro and support for GNU gold
  kbuild: add ld-if-name-version and linker-specific macros
  kbuild: fix LD_DEAD_CODE_DATA_ELIMINATION with GNU gold
  arm64: fix -m for GNU gold
  arm64: keep .altinstructions and .altinstr_replacement
  arm64: explicitly pass --no-fix-cortex-a53-843419 to GNU gold
  arm64: add a workaround for GNU gold with ARM64_MODULE_PLTS

 Makefile  |  5 +
 arch/arm64/Makefile   | 17 +
 arch/arm64/kernel/module.lds  |  4 ++--
 arch/arm64/kernel/vmlinux.lds.S   |  4 ++--
 include/asm-generic/vmlinux.lds.h |  8 
 scripts/Kbuild.include| 16 
 6 files changed, 46 insertions(+), 8 deletions(-)

-- 
2.15.0.531.g2ccb3012c9-goog



Re: [v2,12/18] kbuild: add support for clang LTO

2017-11-29 Thread Sami Tolvanen
On Tue, Nov 21, 2017 at 11:01:52AM +1000, Nicholas Piggin wrote:
> I just wonder are you doing this because there is some worthwhile
> performance gain? Or to enable more testing and development of LTO?
> Any clues for why a user would want to enable it.

I'm primarily interested in CFI, which with clang requires LTO; not
for the optimizations, but for source visibility. We do expect to see
performance improvements with LTO though, especially if combined with
PGO.

> Thanks, if you could. Possibly file a request with LLVMgold too, it
> seems to be that toolchain support for archives is quite strong, so it
> will be good to keep pushing for that.

It turns out LLVMgold is fine with mixed IR/object archives, but we need
to use llvm-ar to generate symbol tables for them, and there are some
compatibility issues with objdump that I had to work around. I'll send
v3 for review once I receive some feedback for the clang/gold patches we
need first.

Sami


Re: [v2,12/18] kbuild: add support for clang LTO

2017-11-29 Thread Sami Tolvanen
On Tue, Nov 21, 2017 at 11:01:52AM +1000, Nicholas Piggin wrote:
> I just wonder are you doing this because there is some worthwhile
> performance gain? Or to enable more testing and development of LTO?
> Any clues for why a user would want to enable it.

I'm primarily interested in CFI, which with clang requires LTO; not
for the optimizations, but for source visibility. We do expect to see
performance improvements with LTO though, especially if combined with
PGO.

> Thanks, if you could. Possibly file a request with LLVMgold too, it
> seems to be that toolchain support for archives is quite strong, so it
> will be good to keep pushing for that.

It turns out LLVMgold is fine with mixed IR/object archives, but we need
to use llvm-ar to generate symbol tables for them, and there are some
compatibility issues with objdump that I had to work around. I'll send
v3 for review once I receive some feedback for the clang/gold patches we
need first.

Sami


Re: Link time optimization for LTO/x86

2017-11-29 Thread Sami Tolvanen
Hi Andi,

On Mon, Nov 27, 2017 at 01:34:02PM -0800, Andi Kleen wrote:
> This is an updated version of my older LTO patchkit for gcc/x86
> This version doesn't need special binutils, but requires gcc 5+.
> It also is compatible with near all options (except MODVERSIONS)

Would you mind changing the config option prefix to LTO_GCC, for
example? It looks like there are a few places where gcc LTO needs
to be disabled, but clang LTO works fine, and vice versa.

My earlier clang LTO patches also had a solution for MODVERSIONS,
which might work with gcc as well.

Sami


Re: Link time optimization for LTO/x86

2017-11-29 Thread Sami Tolvanen
Hi Andi,

On Mon, Nov 27, 2017 at 01:34:02PM -0800, Andi Kleen wrote:
> This is an updated version of my older LTO patchkit for gcc/x86
> This version doesn't need special binutils, but requires gcc 5+.
> It also is compatible with near all options (except MODVERSIONS)

Would you mind changing the config option prefix to LTO_GCC, for
example? It looks like there are a few places where gcc LTO needs
to be disabled, but clang LTO works fine, and vice versa.

My earlier clang LTO patches also had a solution for MODVERSIONS,
which might work with gcc as well.

Sami


Re: [PATCH 3/3] arm64: use -mno-implicit-float instead of -mgeneral-regs-only

2017-11-29 Thread Sami Tolvanen
On Wed, Nov 29, 2017 at 12:15:14PM +, Ard Biesheuvel wrote:
> Do we still need these patches now that the AES code has been fixed?

With your AES patch that Herbert just applied, this patch is no longer
needed. Version macros in the first two patches will still be useful in
future though.

Sami


Re: [PATCH 3/3] arm64: use -mno-implicit-float instead of -mgeneral-regs-only

2017-11-29 Thread Sami Tolvanen
On Wed, Nov 29, 2017 at 12:15:14PM +, Ard Biesheuvel wrote:
> Do we still need these patches now that the AES code has been fixed?

With your AES patch that Herbert just applied, this patch is no longer
needed. Version macros in the first two patches will still be useful in
future though.

Sami


[PATCH 3/3] arm64: use -mno-implicit-float instead of -mgeneral-regs-only

2017-11-28 Thread Sami Tolvanen
From: Greg Hackmann <ghackm...@google.com>

LLVM bug 30792 causes clang's AArch64 backend to crash compiling
arch/arm64/crypto/aes-ce-cipher.c.  Replacing -mgeneral-regs-only with
-mno-implicit-float is the suggested workaround.

Signed-off-by: Greg Hackmann <ghackm...@google.com>
Cc: Matthias Kaehlcke <m...@chromium.org>
[added clang-ifversion to enable the workaround only for clang <6.0]
Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/Makefile | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index b35788c909f1..d2efb5a0212f 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -49,7 +49,10 @@ $(warning Detected assembler with broken .inst; disassembly 
will be unreliable)
   endif
 endif
 
-KBUILD_CFLAGS  += -mgeneral-regs-only $(lseinstr) $(brokengasinst)
+# This is a workaround for https://bugs.llvm.org/show_bug.cgi?id=30792.
+KBUILD_CFLAGS  += $(call clang-ifversion, -lt, 0600, -mno-implicit-float, 
-mgeneral-regs-only)
+
+KBUILD_CFLAGS  += $(lseinstr) $(brokengasinst)
 KBUILD_CFLAGS  += -fno-asynchronous-unwind-tables
 KBUILD_CFLAGS  += $(call cc-option, -mpc-relative-literal-loads)
 KBUILD_AFLAGS  += $(lseinstr) $(brokengasinst)
-- 
2.15.0.417.g466bffb3ac-goog



[PATCH 3/3] arm64: use -mno-implicit-float instead of -mgeneral-regs-only

2017-11-28 Thread Sami Tolvanen
From: Greg Hackmann 

LLVM bug 30792 causes clang's AArch64 backend to crash compiling
arch/arm64/crypto/aes-ce-cipher.c.  Replacing -mgeneral-regs-only with
-mno-implicit-float is the suggested workaround.

Signed-off-by: Greg Hackmann 
Cc: Matthias Kaehlcke 
[added clang-ifversion to enable the workaround only for clang <6.0]
Signed-off-by: Sami Tolvanen 
---
 arch/arm64/Makefile | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index b35788c909f1..d2efb5a0212f 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -49,7 +49,10 @@ $(warning Detected assembler with broken .inst; disassembly 
will be unreliable)
   endif
 endif
 
-KBUILD_CFLAGS  += -mgeneral-regs-only $(lseinstr) $(brokengasinst)
+# This is a workaround for https://bugs.llvm.org/show_bug.cgi?id=30792.
+KBUILD_CFLAGS  += $(call clang-ifversion, -lt, 0600, -mno-implicit-float, 
-mgeneral-regs-only)
+
+KBUILD_CFLAGS  += $(lseinstr) $(brokengasinst)
 KBUILD_CFLAGS  += -fno-asynchronous-unwind-tables
 KBUILD_CFLAGS  += $(call cc-option, -mpc-relative-literal-loads)
 KBUILD_AFLAGS  += $(lseinstr) $(brokengasinst)
-- 
2.15.0.417.g466bffb3ac-goog



[PATCH 1/3] kbuild: add clang-version.sh

2017-11-28 Thread Sami Tolvanen
Based on gcc-version.sh, clang-version.sh prints out the correct
version of clang.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 scripts/clang-version.sh | 33 +
 1 file changed, 33 insertions(+)
 create mode 100755 scripts/clang-version.sh

diff --git a/scripts/clang-version.sh b/scripts/clang-version.sh
new file mode 100755
index ..5f14ddc54032
--- /dev/null
+++ b/scripts/clang-version.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-version [-p] clang-command
+#
+# Prints the compiler version of `command' in a canonical 4-digit form
+# such as `0500' for clang-5.0 etc.
+#
+# With the -p option, prints the patchlevel as well, for example `050001' for
+# clang-5.0.1 etc.
+#
+
+if [ "$1" = "-p" ] ; then
+   with_patchlevel=1;
+   shift;
+fi
+
+compiler="$*"
+
+if [ ${#compiler} -eq 0 ]; then
+   echo "Error: No compiler specified."
+   printf "Usage:\n\t$0 \n"
+   exit 1
+fi
+
+MAJOR=$(echo __clang_major__ | $compiler -E -x c - | tail -n 1)
+MINOR=$(echo __clang_minor__ | $compiler -E -x c - | tail -n 1)
+if [ "x$with_patchlevel" != "x" ] ; then
+   PATCHLEVEL=$(echo __clang_patchlevel__ | $compiler -E -x c - | tail -n 
1)
+   printf "%02d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
+else
+   printf "%02d%02d\\n" $MAJOR $MINOR
+fi
-- 
2.15.0.417.g466bffb3ac-goog



[PATCH 1/3] kbuild: add clang-version.sh

2017-11-28 Thread Sami Tolvanen
Based on gcc-version.sh, clang-version.sh prints out the correct
version of clang.

Signed-off-by: Sami Tolvanen 
---
 scripts/clang-version.sh | 33 +
 1 file changed, 33 insertions(+)
 create mode 100755 scripts/clang-version.sh

diff --git a/scripts/clang-version.sh b/scripts/clang-version.sh
new file mode 100755
index ..5f14ddc54032
--- /dev/null
+++ b/scripts/clang-version.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-version [-p] clang-command
+#
+# Prints the compiler version of `command' in a canonical 4-digit form
+# such as `0500' for clang-5.0 etc.
+#
+# With the -p option, prints the patchlevel as well, for example `050001' for
+# clang-5.0.1 etc.
+#
+
+if [ "$1" = "-p" ] ; then
+   with_patchlevel=1;
+   shift;
+fi
+
+compiler="$*"
+
+if [ ${#compiler} -eq 0 ]; then
+   echo "Error: No compiler specified."
+   printf "Usage:\n\t$0 \n"
+   exit 1
+fi
+
+MAJOR=$(echo __clang_major__ | $compiler -E -x c - | tail -n 1)
+MINOR=$(echo __clang_minor__ | $compiler -E -x c - | tail -n 1)
+if [ "x$with_patchlevel" != "x" ] ; then
+   PATCHLEVEL=$(echo __clang_patchlevel__ | $compiler -E -x c - | tail -n 
1)
+   printf "%02d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
+else
+   printf "%02d%02d\\n" $MAJOR $MINOR
+fi
-- 
2.15.0.417.g466bffb3ac-goog



[PATCH 2/3] kbuild: add cc-if-name-version and compiler-specific variants

2017-11-28 Thread Sami Tolvanen
This change adds macros for testing both compiler name and
version. Current cc-version, cc-ifversion etc. macros that test
gcc version are left unchanged to prevent compatibility issues
with existing tests.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 scripts/Kbuild.include | 31 +++
 1 file changed, 31 insertions(+)

diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 065324a8046f..b6d7d347b203 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -215,6 +215,37 @@ cc-disable-warning = $(call try-run-cached,\
 # Expands to either gcc or clang
 cc-name = $(call shell-cached,$(CC) -v 2>&1 | grep -q "clang version" && echo 
clang || echo gcc)
 
+# __cc-version
+# Returns compiler version
+__cc-version = $(shell $(CONFIG_SHELL) 
$(srctree)/scripts/$(cc-name)-version.sh $(CC))
+
+# __cc-fullversion
+# Returns full compiler version
+__cc-fullversion = $(shell $(CONFIG_SHELL) \
+   $(srctree)/scripts/$(cc-name)-version.sh -p $(CC))
+
+# cc-if-name-version
+# Matches compiler name and version
+# Usage:  EXTRA_CFLAGS += $(call cc-if-name-version, gcc, -lt, 0402, -O1)
+cc-if-name-version = $(shell [ $(cc-name) == $(1) ] && [ $(__cc-version) $(2) 
$(3) ] && echo $(4) || echo $(5))
+
+# cc-if-name-fullversion
+# Matches compiler name and full version
+# Usage:  EXTRA_CFLAGS += $(call cc-if-name-fullversion, gcc, -lt, 040502, -O1)
+cc-if-name-fullversion = $(shell [ $(cc-name) == $(1) ] && [ 
$(__cc-fullversion) $(2) $(3) ] && echo $(4) || echo $(5))
+
+# gcc-ifversion
+gcc-ifversion = $(call cc-if-name-version, gcc, $(1), $(2), $(3), $(4))
+
+# gcc-if-fullversion
+gcc-if-fullversion = (call cc-if-name-fullversion, gcc, $(1), $(2), $(3), $(4))
+
+# clang-ifversion
+clang-ifversion =  $(call cc-if-name-version, clang, $(1), $(2), $(3), $(4))
+
+# clang-if-fullversion
+clang-if-fullversion = (call cc-if-name-fullversion, clang, $(1), $(2), $(3), 
$(4))
+
 # cc-version
 cc-version = $(call shell-cached,$(CONFIG_SHELL) 
$(srctree)/scripts/gcc-version.sh $(CC))
 
-- 
2.15.0.417.g466bffb3ac-goog



[PATCH 2/3] kbuild: add cc-if-name-version and compiler-specific variants

2017-11-28 Thread Sami Tolvanen
This change adds macros for testing both compiler name and
version. Current cc-version, cc-ifversion etc. macros that test
gcc version are left unchanged to prevent compatibility issues
with existing tests.

Signed-off-by: Sami Tolvanen 
---
 scripts/Kbuild.include | 31 +++
 1 file changed, 31 insertions(+)

diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 065324a8046f..b6d7d347b203 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -215,6 +215,37 @@ cc-disable-warning = $(call try-run-cached,\
 # Expands to either gcc or clang
 cc-name = $(call shell-cached,$(CC) -v 2>&1 | grep -q "clang version" && echo 
clang || echo gcc)
 
+# __cc-version
+# Returns compiler version
+__cc-version = $(shell $(CONFIG_SHELL) 
$(srctree)/scripts/$(cc-name)-version.sh $(CC))
+
+# __cc-fullversion
+# Returns full compiler version
+__cc-fullversion = $(shell $(CONFIG_SHELL) \
+   $(srctree)/scripts/$(cc-name)-version.sh -p $(CC))
+
+# cc-if-name-version
+# Matches compiler name and version
+# Usage:  EXTRA_CFLAGS += $(call cc-if-name-version, gcc, -lt, 0402, -O1)
+cc-if-name-version = $(shell [ $(cc-name) == $(1) ] && [ $(__cc-version) $(2) 
$(3) ] && echo $(4) || echo $(5))
+
+# cc-if-name-fullversion
+# Matches compiler name and full version
+# Usage:  EXTRA_CFLAGS += $(call cc-if-name-fullversion, gcc, -lt, 040502, -O1)
+cc-if-name-fullversion = $(shell [ $(cc-name) == $(1) ] && [ 
$(__cc-fullversion) $(2) $(3) ] && echo $(4) || echo $(5))
+
+# gcc-ifversion
+gcc-ifversion = $(call cc-if-name-version, gcc, $(1), $(2), $(3), $(4))
+
+# gcc-if-fullversion
+gcc-if-fullversion = (call cc-if-name-fullversion, gcc, $(1), $(2), $(3), $(4))
+
+# clang-ifversion
+clang-ifversion =  $(call cc-if-name-version, clang, $(1), $(2), $(3), $(4))
+
+# clang-if-fullversion
+clang-if-fullversion = (call cc-if-name-fullversion, clang, $(1), $(2), $(3), 
$(4))
+
 # cc-version
 cc-version = $(call shell-cached,$(CONFIG_SHELL) 
$(srctree)/scripts/gcc-version.sh $(CC))
 
-- 
2.15.0.417.g466bffb3ac-goog



[PATCH 0/3] Add version macros for clang and fix arm64 for clang <6.0

2017-11-28 Thread Sami Tolvanen
In order to build an arm64 kernel with clang <6.0, we need to work
around LLVM bug 30792 by using -mno-implicit-float instead of
-mgeneral-regs-only.

This patch set adds macros for checking clang version, and enable the
workaround only for affected compiler versions.

Greg Hackmann (1):
  arm64: use -mno-implicit-float instead of -mgeneral-regs-only

Sami Tolvanen (2):
  kbuild: add clang-version.sh
  kbuild: add cc-if-name-version and compiler-specific variants

 arch/arm64/Makefile  |  5 -
 scripts/Kbuild.include   | 31 +++
 scripts/clang-version.sh | 33 +
 3 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100755 scripts/clang-version.sh

-- 
2.15.0.417.g466bffb3ac-goog



[PATCH 0/3] Add version macros for clang and fix arm64 for clang <6.0

2017-11-28 Thread Sami Tolvanen
In order to build an arm64 kernel with clang <6.0, we need to work
around LLVM bug 30792 by using -mno-implicit-float instead of
-mgeneral-regs-only.

This patch set adds macros for checking clang version, and enable the
workaround only for affected compiler versions.

Greg Hackmann (1):
  arm64: use -mno-implicit-float instead of -mgeneral-regs-only

Sami Tolvanen (2):
  kbuild: add clang-version.sh
  kbuild: add cc-if-name-version and compiler-specific variants

 arch/arm64/Makefile  |  5 -
 scripts/Kbuild.include   | 31 +++
 scripts/clang-version.sh | 33 +
 3 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100755 scripts/clang-version.sh

-- 
2.15.0.417.g466bffb3ac-goog



Re: [PATCH v2 00/18] Add support for clang LTO

2017-11-20 Thread Sami Tolvanen
On Mon, Nov 20, 2017 at 03:21:40PM +, Mark Rutland wrote:
> Would it be possible to split this into:
> 
> (1) (basic) arm64 clang support
> (2) gold support (no LTO)
> (3) LTO support
> 
> ... with any necessary workarounds added as-required to the relevant
> series?

Absolutely. I'll split the patches into more sensible sets.

Sami


Re: [PATCH v2 00/18] Add support for clang LTO

2017-11-20 Thread Sami Tolvanen
On Mon, Nov 20, 2017 at 03:21:40PM +, Mark Rutland wrote:
> Would it be possible to split this into:
> 
> (1) (basic) arm64 clang support
> (2) gold support (no LTO)
> (3) LTO support
> 
> ... with any necessary workarounds added as-required to the relevant
> series?

Absolutely. I'll split the patches into more sensible sets.

Sami


Re: [PATCH v2 16/18] arm64: crypto: disable LTO for aes-ce-cipher.c

2017-11-20 Thread Sami Tolvanen
On Mon, Nov 20, 2017 at 03:25:31PM +, Ard Biesheuvel wrote:
> However, under LTO this all changes, and it is no longer guaranteed
> that the NEON registers are only touched between the kernel mode
> neon begin/end calls.

LTO operates on LLVM IR, so disabling LTO for this file should make
sure there won't be any unsafe optimizations. Are there other places
in the kernel that might have this issue?

> So the correct way to fix this would be to move the asm into its own
> .S file, and call it from between the kernel_mode_neon_begin/end
> calls. That should also fix your compat issue.

Sure, that would also work.
 
Sami


Re: [PATCH v2 16/18] arm64: crypto: disable LTO for aes-ce-cipher.c

2017-11-20 Thread Sami Tolvanen
On Mon, Nov 20, 2017 at 03:25:31PM +, Ard Biesheuvel wrote:
> However, under LTO this all changes, and it is no longer guaranteed
> that the NEON registers are only touched between the kernel mode
> neon begin/end calls.

LTO operates on LLVM IR, so disabling LTO for this file should make
sure there won't be any unsafe optimizations. Are there other places
in the kernel that might have this issue?

> So the correct way to fix this would be to move the asm into its own
> .S file, and call it from between the kernel_mode_neon_begin/end
> calls. That should also fix your compat issue.

Sure, that would also work.
 
Sami


Re: [PATCH v2 16/18] arm64: crypto: disable LTO for aes-ce-cipher.c

2017-11-20 Thread Sami Tolvanen
On Mon, Nov 20, 2017 at 03:20:14PM +, Mark Rutland wrote:
> Could you elaborate on what the integrated asembler doesn't like?

Here's the error, looks like in aes_sub:

:1:69: error: invalid operand for instruction
dup v1.4s, w12  ;movi   v0.16b, #0  ;aese   
v0.16b, v1.16b  ;umov   w12, v0.4s[0]   ;

 ^
LLVM ERROR: Error parsing inline asm

Sami


Re: [PATCH v2 16/18] arm64: crypto: disable LTO for aes-ce-cipher.c

2017-11-20 Thread Sami Tolvanen
On Mon, Nov 20, 2017 at 03:20:14PM +, Mark Rutland wrote:
> Could you elaborate on what the integrated asembler doesn't like?

Here's the error, looks like in aes_sub:

:1:69: error: invalid operand for instruction
dup v1.4s, w12  ;movi   v0.16b, #0  ;aese   
v0.16b, v1.16b  ;umov   w12, v0.4s[0]   ;

 ^
LLVM ERROR: Error parsing inline asm

Sami


Re: [PATCH v2 09/18] arm64: explicitly pass --no-fix-cortex-a53-843419 to GNU gold

2017-11-20 Thread Sami Tolvanen
On Mon, Nov 20, 2017 at 02:47:20PM +, Mark Rutland wrote:
> However, we could instead check ld-version, produce a warning, and
> abort the build in such cases.

I believe the version number of gold didn't change in binutils 2.28.1,
where this was fixed, but we could certainly warn about older versions
of gold.

Sami


Re: [PATCH v2 09/18] arm64: explicitly pass --no-fix-cortex-a53-843419 to GNU gold

2017-11-20 Thread Sami Tolvanen
On Mon, Nov 20, 2017 at 02:47:20PM +, Mark Rutland wrote:
> However, we could instead check ld-version, produce a warning, and
> abort the build in such cases.

I believe the version number of gold didn't change in binutils 2.28.1,
where this was fixed, but we could certainly warn about older versions
of gold.

Sami


Re: [v2,12/18] kbuild: add support for clang LTO

2017-11-20 Thread Sami Tolvanen
On Sat, Nov 18, 2017 at 01:21:39PM +1000, Nicholas Piggin wrote:
> Do you have any kind of numbers for this, out of curiosity? Binary
> size, performance, build time?

I don't have performance numbers to share. Are there any specific
benchmarks you'd be interested in seeing? Build time typically
increases with LTO and in my experience, binary size tends to increase
by ~10-15% as well.

> Why is this needed? It would have been nice to get rid of the
> !THIN_ARCHIVES option if you can make the patches work with the thin
> archives paths.

I believe LLVMgold doesn't know how to deal with an archive of LLVM IR
files, but I can certainly use thin archives as an index and extract
the path names for linking. I'll look into it.

Sami


Re: [v2,12/18] kbuild: add support for clang LTO

2017-11-20 Thread Sami Tolvanen
On Sat, Nov 18, 2017 at 01:21:39PM +1000, Nicholas Piggin wrote:
> Do you have any kind of numbers for this, out of curiosity? Binary
> size, performance, build time?

I don't have performance numbers to share. Are there any specific
benchmarks you'd be interested in seeing? Build time typically
increases with LTO and in my experience, binary size tends to increase
by ~10-15% as well.

> Why is this needed? It would have been nice to get rid of the
> !THIN_ARCHIVES option if you can make the patches work with the thin
> archives paths.

I believe LLVMgold doesn't know how to deal with an archive of LLVM IR
files, but I can certainly use thin archives as an index and extract
the path names for linking. I'll look into it.

Sami


Re: [PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-17 Thread Sami Tolvanen
On Fri, Nov 17, 2017 at 09:54:48AM +, Ard Biesheuvel wrote:
> OK, so all IR objects are converted into a single .o file
> encapsulating the module image. Does this give the same benefits as
> LTO linking IR objects to a fully linked executable?

Yes, it does.

> Even if it does, partial linking is not something the toolchain
> people are usually crazy about, so it would be nice to have some
> confirmation that this is a usage model that is fully supported.

I confirmed with our LLVM developers that while this is less common,
it's fully supported.

I will also drop this patch in v3 as passing code model to LLVMgold
fixes the issue with LTO.

Sami


Re: [PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-17 Thread Sami Tolvanen
On Fri, Nov 17, 2017 at 09:54:48AM +, Ard Biesheuvel wrote:
> OK, so all IR objects are converted into a single .o file
> encapsulating the module image. Does this give the same benefits as
> LTO linking IR objects to a fully linked executable?

Yes, it does.

> Even if it does, partial linking is not something the toolchain
> people are usually crazy about, so it would be nice to have some
> confirmation that this is a usage model that is fully supported.

I confirmed with our LLVM developers that while this is less common,
it's fully supported.

I will also drop this patch in v3 as passing code model to LLVMgold
fixes the issue with LTO.

Sami


Re: [PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:20:40PM +, Ard Biesheuvel wrote:
> So at which point is the IR in a partially linked object file
> converted into executable code?

At the final module link step (cmd_ld_ko_o) in scripts/Makefile.modpost,
added in patch 12.

Sami


Re: [PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:20:40PM +, Ard Biesheuvel wrote:
> So at which point is the IR in a partially linked object file
> converted into executable code?

At the final module link step (cmd_ld_ko_o) in scripts/Makefile.modpost,
added in patch 12.

Sami


Re: [PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 10:14:17PM +, Ard Biesheuvel wrote:
> OK, so my concern here is that this code probably only operates on
> fully linked binaries, and not partially linked object files like
> kernel modules.

Right. That makes sense.

> What is preventing us from using the large model with clang?

We pass -mcmodel=large to clang, but I just confirmed that the
attribute isn't stored in LLVM IR, which means it's not used during
link time compilation. I'll see if we can solve this problem by
passing the correct code model directly to LLVMgold instead. Thanks
for pointing this out.

Sami



Re: [PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 10:14:17PM +, Ard Biesheuvel wrote:
> OK, so my concern here is that this code probably only operates on
> fully linked binaries, and not partially linked object files like
> kernel modules.

Right. That makes sense.

> What is preventing us from using the large model with clang?

We pass -mcmodel=large to clang, but I just confirmed that the
attribute isn't stored in LLVM IR, which means it's not used during
link time compilation. I'll see if we can solve this problem by
passing the correct code model directly to LLVMgold instead. Thanks
for pointing this out.

Sami



Re: [PATCH v2 00/18] Add support for clang LTO

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:53:00PM +0300, Yury Norov wrote:
> I would suggest you to add the patch that checks their versions and
> disables LTO if needed.

Sure, sounds reasonable. I'll add this in the next version.

Sami


Re: [PATCH v2 00/18] Add support for clang LTO

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:53:00PM +0300, Yury Norov wrote:
> I would suggest you to add the patch that checks their versions and
> disables LTO if needed.

Sure, sounds reasonable. I'll add this in the next version.

Sami


Re: [PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 04:34:03PM +, Ard Biesheuvel wrote:
> You still have not explained to us how GOLD avoids the erratum.

Sorry, I didn't realize you were asking that. If gold spots erratum
sequences, looks like it creates stubs to break them up:

  https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gold/aarch64.cc#l8396

It also attempts to optimize the code by replacing adrps in these
sequences with adr where possible, but otherwise doesn't appear to
touch them:

  https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gold/aarch64.cc#l2053

Sami


Re: [PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 04:34:03PM +, Ard Biesheuvel wrote:
> You still have not explained to us how GOLD avoids the erratum.

Sorry, I didn't realize you were asking that. If gold spots erratum
sequences, looks like it creates stubs to break them up:

  https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gold/aarch64.cc#l8396

It also attempts to optimize the code by replacing adrps in these
sequences with adr where possible, but otherwise doesn't appear to
touch them:

  https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gold/aarch64.cc#l2053

Sami


Re: [PATCH v2 18/18] arm64: select ARCH_SUPPORTS_LTO_CLANG

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:13:07AM -0800, Paul E. McKenney wrote:
> Ah, if "this patch set" meant "adding LTO", I stand corrected and I
> apologize for my confusion.

Again, I'm not proposing for LTO to be enabled by default. These patches
just make it possible to enable it. Are you saying the possibility
that a future compiler update breaks something is a blocker even for
experimental features?

> I agree that we need LTO/PGO to be housebroken from an LKMM viewpoint
> before it is enabled.

Can you elaborate what's needed from clang before this can move
forward? For example, if you have specific test cases in mind, we can
always work on including them in the LLVM test suite.

Sami


Re: [PATCH v2 18/18] arm64: select ARCH_SUPPORTS_LTO_CLANG

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:13:07AM -0800, Paul E. McKenney wrote:
> Ah, if "this patch set" meant "adding LTO", I stand corrected and I
> apologize for my confusion.

Again, I'm not proposing for LTO to be enabled by default. These patches
just make it possible to enable it. Are you saying the possibility
that a future compiler update breaks something is a blocker even for
experimental features?

> I agree that we need LTO/PGO to be housebroken from an LKMM viewpoint
> before it is enabled.

Can you elaborate what's needed from clang before this can move
forward? For example, if you have specific test cases in mind, we can
always work on including them in the LLVM test suite.

Sami


Re: [PATCH v2 11/18] arm64: make mrs_s and msr_s macros work with LTO

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:01:44AM -0600, Segher Boessenkool wrote:
> The thing with .purgem can work.  Inelegant, sure, but it can work :-)

It works, there are already functions in the kernel that use these
macros more than once. I agree that this might not be the most elegant
solution, but at least it allows us to use the same code path for both
compilers.

> Just make sure you do the macro define, the code that uses it, and the
> undefine, all in the same inline asm statement.

Yes, that's what we're doing here. Only KVM uses msr_s/msr_s directly,
everything else uses the (read|write)_sysreg_s wrappers instead, which
means the DEFINE/UNDEFINE macros are only needed in few places.

Sami


Re: [PATCH v2 11/18] arm64: make mrs_s and msr_s macros work with LTO

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:01:44AM -0600, Segher Boessenkool wrote:
> The thing with .purgem can work.  Inelegant, sure, but it can work :-)

It works, there are already functions in the kernel that use these
macros more than once. I agree that this might not be the most elegant
solution, but at least it allows us to use the same code path for both
compilers.

> Just make sure you do the macro define, the code that uses it, and the
> undefine, all in the same inline asm statement.

Yes, that's what we're doing here. Only KVM uses msr_s/msr_s directly,
everything else uses the (read|write)_sysreg_s wrappers instead, which
means the DEFINE/UNDEFINE macros are only needed in few places.

Sami


Re: [PATCH v2 11/18] arm64: make mrs_s and msr_s macros work with LTO

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 07:56:50AM -0600, Segher Boessenkool wrote:
> The compiler is fine, the assembler is fine (and the linker has
> nothing to do with it).  Your code is not fine.

Would you care to elaborate? The current code assumes that macros are
visible in other inline assembly blocks, and LLVM developers seem to
feel this isn't correct behavior. This patch fixes the current code so
it works with both assemblers.

Sami


Re: [PATCH v2 11/18] arm64: make mrs_s and msr_s macros work with LTO

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 07:56:50AM -0600, Segher Boessenkool wrote:
> The compiler is fine, the assembler is fine (and the linker has
> nothing to do with it).  Your code is not fine.

Would you care to elaborate? The current code assumes that macros are
visible in other inline assembly blocks, and LLVM developers seem to
feel this isn't correct behavior. This patch fixes the current code so
it works with both assemblers.

Sami


Re: [PATCH v2 11/18] arm64: make mrs_s and msr_s macros work with LTO

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:54:33AM +, Will Deacon wrote:
> What limitations? Can you elaborate please? Is this a fix?

The commit message in v1 was more informative, I'll change this back for v3:

--

Clang's integrated assembler does not allow assembly macros defined
in one inline asm block using the .macro directive to be used across
separate asm blocks. LLVM developers consider this a feature and not a
bug, recommending code refactoring:

https://bugs.llvm.org/show_bug.cgi?id=19749

As binutils doesn't allow macros to be redefined, this change adds C
preprocessor macros that define the assembly macros globally for binutils
and locally for clang's integrated assembler.

Sami


Re: [PATCH v2 11/18] arm64: make mrs_s and msr_s macros work with LTO

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:54:33AM +, Will Deacon wrote:
> What limitations? Can you elaborate please? Is this a fix?

The commit message in v1 was more informative, I'll change this back for v3:

--

Clang's integrated assembler does not allow assembly macros defined
in one inline asm block using the .macro directive to be used across
separate asm blocks. LLVM developers consider this a feature and not a
bug, recommending code refactoring:

https://bugs.llvm.org/show_bug.cgi?id=19749

As binutils doesn't allow macros to be redefined, this change adds C
preprocessor macros that define the assembly macros globally for binutils
and locally for clang's integrated assembler.

Sami


Re: [PATCH v2 10/18] arm64: add a workaround for GNU gold with ARM64_MODULE_PLTS

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:50:12AM +, Will Deacon wrote:
> Why don't we just not do LTO if the toolchain is busted?

Because LTO can not only potentially improve performance, especially
when combined with PGO (Profile Guided Optimization), but it also
makes it possible to enable features like Control Flow Integrity that
can make kernel vulnerabilities more difficult to exploit:

  https://clang.llvm.org/docs/ControlFlowIntegrity.html

> This feels like it will end up being a game of whack-a-mole as code
> could be introduced that tickles known bugs on older toolchains.

I'm not sure this is much different from dealing with older versions
of the existing toolchain. Otherwise, we wouldn't need cc-version or
other similar macros, for example.

Sami


Re: [PATCH v2 10/18] arm64: add a workaround for GNU gold with ARM64_MODULE_PLTS

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:50:12AM +, Will Deacon wrote:
> Why don't we just not do LTO if the toolchain is busted?

Because LTO can not only potentially improve performance, especially
when combined with PGO (Profile Guided Optimization), but it also
makes it possible to enable features like Control Flow Integrity that
can make kernel vulnerabilities more difficult to exploit:

  https://clang.llvm.org/docs/ControlFlowIntegrity.html

> This feels like it will end up being a game of whack-a-mole as code
> could be introduced that tickles known bugs on older toolchains.

I'm not sure this is much different from dealing with older versions
of the existing toolchain. Otherwise, we wouldn't need cc-version or
other similar macros, for example.

Sami


Re: [PATCH v2 09/18] arm64: explicitly pass --no-fix-cortex-a53-843419 to GNU gold

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:47:23AM +, Will Deacon wrote:
> But if ARM64_ERRATUM_843419 is enabled, we'll go ahead and generate
> broken code?

This bug has been fixed in all current versions of GNU gold, but of
course, if someone is using a broken linker, it will generate broken
code. We can't do anything about that in the kernel.

The reason for this patch is that the linker can be configured to
enable the erratum fix by default, and we must explicitly disable it
if we don't want the fix, whether it's because of a broken linker, or
in our case, because of issues with kernel modules.

Sami


Re: [PATCH v2 09/18] arm64: explicitly pass --no-fix-cortex-a53-843419 to GNU gold

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:47:23AM +, Will Deacon wrote:
> But if ARM64_ERRATUM_843419 is enabled, we'll go ahead and generate
> broken code?

This bug has been fixed in all current versions of GNU gold, but of
course, if someone is using a broken linker, it will generate broken
code. We can't do anything about that in the kernel.

The reason for this patch is that the linker can be configured to
enable the erratum fix by default, and we must explicitly disable it
if we don't want the fix, whether it's because of a broken linker, or
in our case, because of issues with kernel modules.

Sami


Re: [PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:44:06AM +, Will Deacon wrote:
> Right, and this would also mean that we silently load vulnerable
> modules that are linked with either LD that doesn't support
> --fix-cortex-a53-843419 or simply wasn't passed.

You'll see a warning at least if the linker doesn't support the flag,
but yes, you're correct. In v1 of this patch set, LTO depended on this
erratum not being selected, but I changed it in v2 based on Ard's
suggestion.

I'm fine with not being able to use LTO on devices that are affected
by this erratum, so either option works for me. I can even change
this so the user must explicitly disable the erratum in order to use
LTO. Thoughts?

Sami


Re: [PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:44:06AM +, Will Deacon wrote:
> Right, and this would also mean that we silently load vulnerable
> modules that are linked with either LD that doesn't support
> --fix-cortex-a53-843419 or simply wasn't passed.

You'll see a warning at least if the linker doesn't support the flag,
but yes, you're correct. In v1 of this patch set, LTO depended on this
erratum not being selected, but I changed it in v2 based on Ard's
suggestion.

I'm fine with not being able to use LTO on devices that are affected
by this erratum, so either option works for me. I can even change
this so the user must explicitly disable the erratum in order to use
LTO. Thoughts?

Sami


Re: [PATCH v2 06/18] arm64: kvm: use -fno-jump-tables with clang

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:46:17AM +, Will Deacon wrote:
> Can you elaborate a bit more on exactly what you saw failing here,
> please?

Mark noticed that clang built kernels fail to boot when the kernel
starts at EL2:

  http://lkml.iu.edu/hypermail/linux/kernel/1711.0/02817.html

Turns out starting with LLVM r308050, the compiler decides to use
a jump table in __init_stage2_translation, and generates code that
branches into an EL1 virtual address.

> Whilst it's obviously broken to jump to EL1 from EL2 w/o VHE, the
> commit message is a bit cryptic.

I agree, I'll add a more descriptive commit message for v3.

Sami


Re: [PATCH v2 06/18] arm64: kvm: use -fno-jump-tables with clang

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:46:17AM +, Will Deacon wrote:
> Can you elaborate a bit more on exactly what you saw failing here,
> please?

Mark noticed that clang built kernels fail to boot when the kernel
starts at EL2:

  http://lkml.iu.edu/hypermail/linux/kernel/1711.0/02817.html

Turns out starting with LLVM r308050, the compiler decides to use
a jump table in __init_stage2_translation, and generates code that
branches into an EL1 virtual address.

> Whilst it's obviously broken to jump to EL1 from EL2 w/o VHE, the
> commit message is a bit cryptic.

I agree, I'll add a more descriptive commit message for v3.

Sami


Re: [PATCH v2 18/18] arm64: select ARCH_SUPPORTS_LTO_CLANG

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:58:11AM +, Will Deacon wrote:
> I'll be honest with you: I'm absolutely terrified about enabling this.

That's understandable, I wouldn't want to enable this by default
quite yet either. This patch doesn't enable LTO for arm64, just makes
it possible to enable the feature. I'm perfectly fine with marking
CONFIG_LTO_CLANG experimental if it makes people more comfortable.

> How much testing has this seen?

I've been running clang LTO kernels for a few months on a Pixel 2 device
without any issues. This is on a 4.4 kernel though.

> Right now, the C standard isn't on our side here and we're relying on
> the compiler not doing this kind of thing. Can we continue to rely on
> that in the face of LTO?

I'll have to check with our LLVM experts, but I have not run into these
issues with current compiler versions. Looking at Andi's old patches,
looks like gcc might be more aggressive in reordering things with LTO
than clang.

Sami


Re: [PATCH v2 18/18] arm64: select ARCH_SUPPORTS_LTO_CLANG

2017-11-16 Thread Sami Tolvanen
On Thu, Nov 16, 2017 at 11:58:11AM +, Will Deacon wrote:
> I'll be honest with you: I'm absolutely terrified about enabling this.

That's understandable, I wouldn't want to enable this by default
quite yet either. This patch doesn't enable LTO for arm64, just makes
it possible to enable the feature. I'm perfectly fine with marking
CONFIG_LTO_CLANG experimental if it makes people more comfortable.

> How much testing has this seen?

I've been running clang LTO kernels for a few months on a Pixel 2 device
without any issues. This is on a 4.4 kernel though.

> Right now, the C standard isn't on our side here and we're relying on
> the compiler not doing this kind of thing. Can we continue to rely on
> that in the face of LTO?

I'll have to check with our LLVM experts, but I have not run into these
issues with current compiler versions. Looking at Andi's old patches,
looks like gcc might be more aggressive in reordering things with LTO
than clang.

Sami


Re: [PATCH v2 03/18] kbuild: move gcc-version.sh to cc-version.sh and add clang support

2017-11-15 Thread Sami Tolvanen
On Wed, Nov 15, 2017 at 01:48:52PM -0800, Kees Cook wrote:
> It might make sense to split this patch: do the move and refactoring,
> then add clang support.

Sure.

> Though, won't this confuse some tests? A lot of cc-version tests are
> expecting only gcc, yes?

There's already a chance of this happening with cc-version. Currently,
gcc-version.sh returns 0402 for clang 5.0, which probably doesn't have
the same issues as gcc 4.2 did.

While I didn't see anything new that would break on platforms that
clang can currently compile, you're correct, we should probably have a
macro that also checks for the compiler, or have separate macros for
different compilers.

I'll address these in v3.

Sami


Re: [PATCH v2 03/18] kbuild: move gcc-version.sh to cc-version.sh and add clang support

2017-11-15 Thread Sami Tolvanen
On Wed, Nov 15, 2017 at 01:48:52PM -0800, Kees Cook wrote:
> It might make sense to split this patch: do the move and refactoring,
> then add clang support.

Sure.

> Though, won't this confuse some tests? A lot of cc-version tests are
> expecting only gcc, yes?

There's already a chance of this happening with cc-version. Currently,
gcc-version.sh returns 0402 for clang 5.0, which probably doesn't have
the same issues as gcc 4.2 did.

While I didn't see anything new that would break on platforms that
clang can currently compile, you're correct, we should probably have a
macro that also checks for the compiler, or have separate macros for
different compilers.

I'll address these in v3.

Sami


[PATCH v2 03/18] kbuild: move gcc-version.sh to cc-version.sh and add clang support

2017-11-15 Thread Sami Tolvanen
Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 scripts/Kbuild.include |  4 ++--
 scripts/cc-version.sh  | 45 +
 scripts/gcc-version.sh | 33 -
 3 files changed, 47 insertions(+), 35 deletions(-)
 create mode 100755 scripts/cc-version.sh
 delete mode 100755 scripts/gcc-version.sh

diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 584d6cecd7c0..de41ab74121e 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -143,11 +143,11 @@ cc-disable-warning = $(call try-run,\
 cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || 
echo gcc)
 
 # cc-version
-cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
+cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/cc-version.sh $(CC))
 
 # cc-fullversion
 cc-fullversion = $(shell $(CONFIG_SHELL) \
-   $(srctree)/scripts/gcc-version.sh -p $(CC))
+   $(srctree)/scripts/cc-version.sh -p $(CC))
 
 # cc-ifversion
 # Usage:  EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
diff --git a/scripts/cc-version.sh b/scripts/cc-version.sh
new file mode 100755
index ..6d085a8a07e8
--- /dev/null
+++ b/scripts/cc-version.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# cc-version [-p] cc-command
+#
+# Prints the compiler version of `command' in a canonical 4-digit form
+# such as `0295' for gcc-2.95, `0303' for gcc-3.3, etc.
+#
+# With the -p option, prints the patchlevel as well, for example `029503' for
+# gcc-2.95.3, `030301' for gcc-3.3.1, etc.
+#
+
+if [ "$1" = "-p" ] ; then
+   with_patchlevel=1;
+   shift;
+fi
+
+compiler="$*"
+
+if [ ${#compiler} -eq 0 ]; then
+   echo "Error: No compiler specified."
+   printf "Usage:\n\t$0 \n"
+   exit 1
+fi
+
+clang=$(echo __clang__ | $compiler -E -x c - | tail -n1)
+
+if [ "$clang" == "1" ]; then
+   major_macro="__clang_major__"
+   minor_macro="__clang_minor__"
+   patchlevel_macro="__clang_patchlevel__"
+else
+   major_macro="__GNUC__"
+   minor_macro="__GNUC_MINOR__"
+   patchlevel_macro="__GNUC_PATCHLEVEL__"
+fi
+
+MAJOR=$(echo $major_macro | $compiler -E -x c - | tail -n 1)
+MINOR=$(echo $minor_macro | $compiler -E -x c - | tail -n 1)
+if [ "x$with_patchlevel" != "x" ] ; then
+   PATCHLEVEL=$(echo $patchlevel_macro | $compiler -E -x c - | tail -n 1)
+   printf "%02d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
+else
+   printf "%02d%02d\\n" $MAJOR $MINOR
+fi
diff --git a/scripts/gcc-version.sh b/scripts/gcc-version.sh
deleted file mode 100755
index 11bb909845e7..
--- a/scripts/gcc-version.sh
+++ /dev/null
@@ -1,33 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: GPL-2.0
-#
-# gcc-version [-p] gcc-command
-#
-# Prints the gcc version of `gcc-command' in a canonical 4-digit form
-# such as `0295' for gcc-2.95, `0303' for gcc-3.3, etc.
-#
-# With the -p option, prints the patchlevel as well, for example `029503' for
-# gcc-2.95.3, `030301' for gcc-3.3.1, etc.
-#
-
-if [ "$1" = "-p" ] ; then
-   with_patchlevel=1;
-   shift;
-fi
-
-compiler="$*"
-
-if [ ${#compiler} -eq 0 ]; then
-   echo "Error: No compiler specified."
-   printf "Usage:\n\t$0 \n"
-   exit 1
-fi
-
-MAJOR=$(echo __GNUC__ | $compiler -E -x c - | tail -n 1)
-MINOR=$(echo __GNUC_MINOR__ | $compiler -E -x c - | tail -n 1)
-if [ "x$with_patchlevel" != "x" ] ; then
-   PATCHLEVEL=$(echo __GNUC_PATCHLEVEL__ | $compiler -E -x c - | tail -n 1)
-   printf "%02d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
-else
-   printf "%02d%02d\\n" $MAJOR $MINOR
-fi
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 03/18] kbuild: move gcc-version.sh to cc-version.sh and add clang support

2017-11-15 Thread Sami Tolvanen
Signed-off-by: Sami Tolvanen 
---
 scripts/Kbuild.include |  4 ++--
 scripts/cc-version.sh  | 45 +
 scripts/gcc-version.sh | 33 -
 3 files changed, 47 insertions(+), 35 deletions(-)
 create mode 100755 scripts/cc-version.sh
 delete mode 100755 scripts/gcc-version.sh

diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 584d6cecd7c0..de41ab74121e 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -143,11 +143,11 @@ cc-disable-warning = $(call try-run,\
 cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || 
echo gcc)
 
 # cc-version
-cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
+cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/cc-version.sh $(CC))
 
 # cc-fullversion
 cc-fullversion = $(shell $(CONFIG_SHELL) \
-   $(srctree)/scripts/gcc-version.sh -p $(CC))
+   $(srctree)/scripts/cc-version.sh -p $(CC))
 
 # cc-ifversion
 # Usage:  EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
diff --git a/scripts/cc-version.sh b/scripts/cc-version.sh
new file mode 100755
index ..6d085a8a07e8
--- /dev/null
+++ b/scripts/cc-version.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# cc-version [-p] cc-command
+#
+# Prints the compiler version of `command' in a canonical 4-digit form
+# such as `0295' for gcc-2.95, `0303' for gcc-3.3, etc.
+#
+# With the -p option, prints the patchlevel as well, for example `029503' for
+# gcc-2.95.3, `030301' for gcc-3.3.1, etc.
+#
+
+if [ "$1" = "-p" ] ; then
+   with_patchlevel=1;
+   shift;
+fi
+
+compiler="$*"
+
+if [ ${#compiler} -eq 0 ]; then
+   echo "Error: No compiler specified."
+   printf "Usage:\n\t$0 \n"
+   exit 1
+fi
+
+clang=$(echo __clang__ | $compiler -E -x c - | tail -n1)
+
+if [ "$clang" == "1" ]; then
+   major_macro="__clang_major__"
+   minor_macro="__clang_minor__"
+   patchlevel_macro="__clang_patchlevel__"
+else
+   major_macro="__GNUC__"
+   minor_macro="__GNUC_MINOR__"
+   patchlevel_macro="__GNUC_PATCHLEVEL__"
+fi
+
+MAJOR=$(echo $major_macro | $compiler -E -x c - | tail -n 1)
+MINOR=$(echo $minor_macro | $compiler -E -x c - | tail -n 1)
+if [ "x$with_patchlevel" != "x" ] ; then
+   PATCHLEVEL=$(echo $patchlevel_macro | $compiler -E -x c - | tail -n 1)
+   printf "%02d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
+else
+   printf "%02d%02d\\n" $MAJOR $MINOR
+fi
diff --git a/scripts/gcc-version.sh b/scripts/gcc-version.sh
deleted file mode 100755
index 11bb909845e7..
--- a/scripts/gcc-version.sh
+++ /dev/null
@@ -1,33 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: GPL-2.0
-#
-# gcc-version [-p] gcc-command
-#
-# Prints the gcc version of `gcc-command' in a canonical 4-digit form
-# such as `0295' for gcc-2.95, `0303' for gcc-3.3, etc.
-#
-# With the -p option, prints the patchlevel as well, for example `029503' for
-# gcc-2.95.3, `030301' for gcc-3.3.1, etc.
-#
-
-if [ "$1" = "-p" ] ; then
-   with_patchlevel=1;
-   shift;
-fi
-
-compiler="$*"
-
-if [ ${#compiler} -eq 0 ]; then
-   echo "Error: No compiler specified."
-   printf "Usage:\n\t$0 \n"
-   exit 1
-fi
-
-MAJOR=$(echo __GNUC__ | $compiler -E -x c - | tail -n 1)
-MINOR=$(echo __GNUC_MINOR__ | $compiler -E -x c - | tail -n 1)
-if [ "x$with_patchlevel" != "x" ] ; then
-   PATCHLEVEL=$(echo __GNUC_PATCHLEVEL__ | $compiler -E -x c - | tail -n 1)
-   printf "%02d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
-else
-   printf "%02d%02d\\n" $MAJOR $MINOR
-fi
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 05/18] arm64: fix -m for GNU gold

2017-11-15 Thread Sami Tolvanen
GNU gold supports different emulations than bfd. Use aarch64_elf64_*_vec
instead of aarch64linux.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/Makefile | 8 
 1 file changed, 8 insertions(+)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index ecd5ed11c764..6059c8169513 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -70,14 +70,22 @@ KBUILD_CPPFLAGS += -mbig-endian
 CHECKFLAGS += -D__AARCH64EB__
 AS += -EB
 LD += -EB
+ifeq ($(ld-name),gold)
+LDFLAGS+= -maarch64_elf64_be_vec
+else
 LDFLAGS+= -maarch64linuxb
+endif
 UTS_MACHINE:= aarch64_be
 else
 KBUILD_CPPFLAGS+= -mlittle-endian
 CHECKFLAGS += -D__AARCH64EL__
 AS += -EL
 LD += -EL
+ifeq ($(ld-name),gold)
+LDFLAGS+= -maarch64_elf64_le_vec
+else
 LDFLAGS+= -maarch64linux
+endif
 UTS_MACHINE:= aarch64
 endif
 
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 04/18] arm64: use -mno-implicit-float instead of -mgeneral-regs-only

2017-11-15 Thread Sami Tolvanen
From: Greg Hackmann <ghackm...@google.com>

LLVM bug 30792 causes clang's AArch64 backend to crash compiling
arch/arm64/crypto/aes-ce-cipher.c.  Replacing -mgeneral-regs-only with
-mno-implicit-float is the suggested workaround.

Signed-off-by: Greg Hackmann <ghackm...@google.com>
Cc: Matthias Kaehlcke <m...@chromium.org>
[added cc-ifversion to enable the workaround only for clang <6.0]
Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/Makefile | 8 +++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index b35788c909f1..ecd5ed11c764 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -49,7 +49,13 @@ $(warning Detected assembler with broken .inst; disassembly 
will be unreliable)
   endif
 endif
 
-KBUILD_CFLAGS  += -mgeneral-regs-only $(lseinstr) $(brokengasinst)
+ifeq ($(cc-name),clang)
+# This is a workaround for https://bugs.llvm.org/show_bug.cgi?id=30792.
+KBUILD_CFLAGS  += $(call cc-ifversion, -lt, 0600, -mno-implicit-float, 
-mgeneral-regs-only)
+else
+KBUILD_CFLAGS  += -mgeneral-regs-only
+endif
+KBUILD_CFLAGS  += $(lseinstr) $(brokengasinst)
 KBUILD_CFLAGS  += -fno-asynchronous-unwind-tables
 KBUILD_CFLAGS  += $(call cc-option, -mpc-relative-literal-loads)
 KBUILD_AFLAGS  += $(lseinstr) $(brokengasinst)
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 05/18] arm64: fix -m for GNU gold

2017-11-15 Thread Sami Tolvanen
GNU gold supports different emulations than bfd. Use aarch64_elf64_*_vec
instead of aarch64linux.

Signed-off-by: Sami Tolvanen 
---
 arch/arm64/Makefile | 8 
 1 file changed, 8 insertions(+)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index ecd5ed11c764..6059c8169513 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -70,14 +70,22 @@ KBUILD_CPPFLAGS += -mbig-endian
 CHECKFLAGS += -D__AARCH64EB__
 AS += -EB
 LD += -EB
+ifeq ($(ld-name),gold)
+LDFLAGS+= -maarch64_elf64_be_vec
+else
 LDFLAGS+= -maarch64linuxb
+endif
 UTS_MACHINE:= aarch64_be
 else
 KBUILD_CPPFLAGS+= -mlittle-endian
 CHECKFLAGS += -D__AARCH64EL__
 AS += -EL
 LD += -EL
+ifeq ($(ld-name),gold)
+LDFLAGS+= -maarch64_elf64_le_vec
+else
 LDFLAGS+= -maarch64linux
+endif
 UTS_MACHINE:= aarch64
 endif
 
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 04/18] arm64: use -mno-implicit-float instead of -mgeneral-regs-only

2017-11-15 Thread Sami Tolvanen
From: Greg Hackmann 

LLVM bug 30792 causes clang's AArch64 backend to crash compiling
arch/arm64/crypto/aes-ce-cipher.c.  Replacing -mgeneral-regs-only with
-mno-implicit-float is the suggested workaround.

Signed-off-by: Greg Hackmann 
Cc: Matthias Kaehlcke 
[added cc-ifversion to enable the workaround only for clang <6.0]
Signed-off-by: Sami Tolvanen 
---
 arch/arm64/Makefile | 8 +++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index b35788c909f1..ecd5ed11c764 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -49,7 +49,13 @@ $(warning Detected assembler with broken .inst; disassembly 
will be unreliable)
   endif
 endif
 
-KBUILD_CFLAGS  += -mgeneral-regs-only $(lseinstr) $(brokengasinst)
+ifeq ($(cc-name),clang)
+# This is a workaround for https://bugs.llvm.org/show_bug.cgi?id=30792.
+KBUILD_CFLAGS  += $(call cc-ifversion, -lt, 0600, -mno-implicit-float, 
-mgeneral-regs-only)
+else
+KBUILD_CFLAGS  += -mgeneral-regs-only
+endif
+KBUILD_CFLAGS  += $(lseinstr) $(brokengasinst)
 KBUILD_CFLAGS  += -fno-asynchronous-unwind-tables
 KBUILD_CFLAGS  += $(call cc-option, -mpc-relative-literal-loads)
 KBUILD_AFLAGS  += $(lseinstr) $(brokengasinst)
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 07/18] arm64: keep .altinstructions and .altinstr_replacement

2017-11-15 Thread Sami Tolvanen
Make sure the linker doesn't remove .altinstructions or
.altinstr_replacement when CONFIG_LD_DEAD_CODE_DATA_ELIMINATION is
enabled.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/kernel/vmlinux.lds.S | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index 7da3e5c366a0..15479995869c 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -138,11 +138,11 @@ SECTIONS
. = ALIGN(4);
.altinstructions : {
__alt_instructions = .;
-   *(.altinstructions)
+   KEEP(*(.altinstructions))
__alt_instructions_end = .;
}
.altinstr_replacement : {
-   *(.altinstr_replacement)
+   KEEP(*(.altinstr_replacement))
}
 
. = ALIGN(PAGE_SIZE);
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 07/18] arm64: keep .altinstructions and .altinstr_replacement

2017-11-15 Thread Sami Tolvanen
Make sure the linker doesn't remove .altinstructions or
.altinstr_replacement when CONFIG_LD_DEAD_CODE_DATA_ELIMINATION is
enabled.

Signed-off-by: Sami Tolvanen 
---
 arch/arm64/kernel/vmlinux.lds.S | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index 7da3e5c366a0..15479995869c 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -138,11 +138,11 @@ SECTIONS
. = ALIGN(4);
.altinstructions : {
__alt_instructions = .;
-   *(.altinstructions)
+   KEEP(*(.altinstructions))
__alt_instructions_end = .;
}
.altinstr_replacement : {
-   *(.altinstr_replacement)
+   KEEP(*(.altinstr_replacement))
}
 
. = ALIGN(PAGE_SIZE);
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 09/18] arm64: explicitly pass --no-fix-cortex-a53-843419 to GNU gold

2017-11-15 Thread Sami Tolvanen
Some versions of GNU gold are known to produce broken code with
--fix-cortex-a53-843419 as explained in this bug:

  https://sourceware.org/bugzilla/show_bug.cgi?id=21491

If ARM64_ERRATUM_843419 is disabled and we're using GNU gold, pass
--no-fix-cortex-a53-843419 to the linker to ensure the erratum fix is not
used even if the linker is configured to enable it by default.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/Makefile | 5 +
 1 file changed, 5 insertions(+)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index 6059c8169513..ca700b201736 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -30,6 +30,11 @@ LDFLAGS_vmlinux  += --fix-cortex-a53-843419
   endif
 endif
 
+ifeq ($(CONFIG_ARM64_ERRATUM_843419),)
+# https://sourceware.org/bugzilla/show_bug.cgi?id=21491
+LDFLAGS_GOLD   += --no-fix-cortex-a53-843419
+endif
+
 KBUILD_DEFCONFIG := defconfig
 
 # Check for binutils support for specific extensions
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-15 Thread Sami Tolvanen
CONFIG_LTO_CLANG depends on GNU gold, which can generate ADR_PREL_PG_HI21*
relocations even with --fix-cortex-a53-843419.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/kernel/module.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c
index f469e0435903..fec9a578f122 100644
--- a/arch/arm64/kernel/module.c
+++ b/arch/arm64/kernel/module.c
@@ -336,14 +336,12 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
 AARCH64_INSN_IMM_ADR);
break;
-#ifndef CONFIG_ARM64_ERRATUM_843419
case R_AARCH64_ADR_PREL_PG_HI21_NC:
overflow_check = false;
case R_AARCH64_ADR_PREL_PG_HI21:
ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21,
 AARCH64_INSN_IMM_ADR);
break;
-#endif
case R_AARCH64_ADD_ABS_LO12_NC:
case R_AARCH64_LDST8_ABS_LO12_NC:
overflow_check = false;
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 09/18] arm64: explicitly pass --no-fix-cortex-a53-843419 to GNU gold

2017-11-15 Thread Sami Tolvanen
Some versions of GNU gold are known to produce broken code with
--fix-cortex-a53-843419 as explained in this bug:

  https://sourceware.org/bugzilla/show_bug.cgi?id=21491

If ARM64_ERRATUM_843419 is disabled and we're using GNU gold, pass
--no-fix-cortex-a53-843419 to the linker to ensure the erratum fix is not
used even if the linker is configured to enable it by default.

Signed-off-by: Sami Tolvanen 
---
 arch/arm64/Makefile | 5 +
 1 file changed, 5 insertions(+)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index 6059c8169513..ca700b201736 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -30,6 +30,11 @@ LDFLAGS_vmlinux  += --fix-cortex-a53-843419
   endif
 endif
 
+ifeq ($(CONFIG_ARM64_ERRATUM_843419),)
+# https://sourceware.org/bugzilla/show_bug.cgi?id=21491
+LDFLAGS_GOLD   += --no-fix-cortex-a53-843419
+endif
+
 KBUILD_DEFCONFIG := defconfig
 
 # Check for binutils support for specific extensions
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 08/18] arm64: don't disable ADR_PREL_PG_HI21* with ARM64_ERRATUM_843419

2017-11-15 Thread Sami Tolvanen
CONFIG_LTO_CLANG depends on GNU gold, which can generate ADR_PREL_PG_HI21*
relocations even with --fix-cortex-a53-843419.

Signed-off-by: Sami Tolvanen 
---
 arch/arm64/kernel/module.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c
index f469e0435903..fec9a578f122 100644
--- a/arch/arm64/kernel/module.c
+++ b/arch/arm64/kernel/module.c
@@ -336,14 +336,12 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
 AARCH64_INSN_IMM_ADR);
break;
-#ifndef CONFIG_ARM64_ERRATUM_843419
case R_AARCH64_ADR_PREL_PG_HI21_NC:
overflow_check = false;
case R_AARCH64_ADR_PREL_PG_HI21:
ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21,
 AARCH64_INSN_IMM_ADR);
break;
-#endif
case R_AARCH64_ADD_ABS_LO12_NC:
case R_AARCH64_LDST8_ABS_LO12_NC:
overflow_check = false;
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 11/18] arm64: make mrs_s and msr_s macros work with LTO

2017-11-15 Thread Sami Tolvanen
From: Alex Matveev <alxm...@gmail.com>

Use UNDEFINE_MRS_S and UNDEFINE_MSR_S to define corresponding macros
in-place and workaround gcc and clang limitations on redefining macros
across different assembler blocks.

Signed-off-by: Alex Matveev <alxm...@gmail.com>
Signed-off-by: Yury Norov <yno...@caviumnetworks.com>
Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/include/asm/kvm_hyp.h |  8 --
 arch/arm64/include/asm/sysreg.h  | 55 +++-
 2 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h
index 4572a9b560fa..20bfb8e676e0 100644
--- a/arch/arm64/include/asm/kvm_hyp.h
+++ b/arch/arm64/include/asm/kvm_hyp.h
@@ -29,7 +29,9 @@
({  \
u64 reg;\
asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
-"mrs_s %0, " __stringify(r##vh),\
+DEFINE_MRS_S   \
+"mrs_s %0, " __stringify(r##vh) "\n"\
+UNDEFINE_MRS_S,\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : "=r" (reg)); \
reg;\
@@ -39,7 +41,9 @@
do {\
u64 __val = (u64)(v);   \
asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", %x0",\
-"msr_s " __stringify(r##vh) ", %x0",\
+DEFINE_MSR_S   \
+"msr_s " __stringify(r##vh) ", %x0\n"\
+UNDEFINE_MSR_S,\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : : "rZ" (__val)); \
} while (0)
diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h
index 08cc88574659..3ae147c7e160 100644
--- a/arch/arm64/include/asm/sysreg.h
+++ b/arch/arm64/include/asm/sysreg.h
@@ -584,20 +584,39 @@
 
 #include 
 
-asm(
-"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
-"  .equ.L__reg_num_x\\num, \\num\n"
-"  .endr\n"
+#define __DEFINE_MRS_MSR_S_REGNUM  \
+"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
 \
+"  .equ.L__reg_num_x\\num, \\num\n"\
+"  .endr\n"\
 "  .equ.L__reg_num_xzr, 31\n"
-"\n"
-"  .macro  mrs_s, rt, sreg\n"
-   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt))
+
+#define DEFINE_MRS_S   \
+   __DEFINE_MRS_MSR_S_REGNUM   \
+"  .macro  mrs_s, rt, sreg\n"  \
+   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt)) \
 "  .endm\n"
-"\n"
-"  .macro  msr_s, sreg, rt\n"
-   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt))
+
+#define DEFINE_MSR_S   \
+   __DEFINE_MRS_MSR_S_REGNUM   \
+"  .macro  msr_s, sreg, rt\n"  \
+   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt)) \
 "  .endm\n"
-);
+
+#define UNDEFINE_MRS_S \
+"  .purgem mrs_s\n"
+
+#define UNDEFINE_MSR_S \
+"  .purgem msr_s\n"
+
+#define __mrs_s(r, v)  \
+   DEFINE_MRS_S\
+"  mrs_s %0, " __stringify(r) "\n" \
+   UNDEFINE_MRS_S : "=r" (v)
+
+#define __msr_s(r, v)  \
+   DEFINE_MSR_S\
+"  msr_s " __stringify(r) ", %x0\n"\
+   UNDEFINE_MSR_S : : "rZ" (v)
 
 /*
  * Unlike read_cpuid, calls to read_sysreg are never expected to be
@@ -623,15 +642,15 @@ asm(
  * For registers without architectural names, or simply unsupported by
  * GAS.
  */
-#def

[PATCH v2 17/18] arm64: disable RANDOMIZE_MODULE_REGION_FULL with LTO_CLANG

2017-11-15 Thread Sami Tolvanen
RANDOMIZE_MODULE_REGION_FULL results in "overflow in relocation type 275"
when loading a module linked with GNU gold. As a workaround, disable when
LTO_CLANG is selected.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index ba6aab55d464..3a70f763e18a 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -1064,7 +1064,7 @@ config RANDOMIZE_BASE
 
 config RANDOMIZE_MODULE_REGION_FULL
bool "Randomize the module region independently from the core kernel"
-   depends on RANDOMIZE_BASE
+   depends on RANDOMIZE_BASE && !LTO_CLANG
default y
help
  Randomizes the location of the module region without considering the
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 11/18] arm64: make mrs_s and msr_s macros work with LTO

2017-11-15 Thread Sami Tolvanen
From: Alex Matveev 

Use UNDEFINE_MRS_S and UNDEFINE_MSR_S to define corresponding macros
in-place and workaround gcc and clang limitations on redefining macros
across different assembler blocks.

Signed-off-by: Alex Matveev 
Signed-off-by: Yury Norov 
Signed-off-by: Sami Tolvanen 
---
 arch/arm64/include/asm/kvm_hyp.h |  8 --
 arch/arm64/include/asm/sysreg.h  | 55 +++-
 2 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h
index 4572a9b560fa..20bfb8e676e0 100644
--- a/arch/arm64/include/asm/kvm_hyp.h
+++ b/arch/arm64/include/asm/kvm_hyp.h
@@ -29,7 +29,9 @@
({  \
u64 reg;\
asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
-"mrs_s %0, " __stringify(r##vh),\
+DEFINE_MRS_S   \
+"mrs_s %0, " __stringify(r##vh) "\n"\
+UNDEFINE_MRS_S,\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : "=r" (reg)); \
reg;\
@@ -39,7 +41,9 @@
do {\
u64 __val = (u64)(v);   \
asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", %x0",\
-"msr_s " __stringify(r##vh) ", %x0",\
+DEFINE_MSR_S   \
+"msr_s " __stringify(r##vh) ", %x0\n"\
+UNDEFINE_MSR_S,\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : : "rZ" (__val)); \
} while (0)
diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h
index 08cc88574659..3ae147c7e160 100644
--- a/arch/arm64/include/asm/sysreg.h
+++ b/arch/arm64/include/asm/sysreg.h
@@ -584,20 +584,39 @@
 
 #include 
 
-asm(
-"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
-"  .equ.L__reg_num_x\\num, \\num\n"
-"  .endr\n"
+#define __DEFINE_MRS_MSR_S_REGNUM  \
+"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
 \
+"  .equ.L__reg_num_x\\num, \\num\n"\
+"  .endr\n"\
 "  .equ.L__reg_num_xzr, 31\n"
-"\n"
-"  .macro  mrs_s, rt, sreg\n"
-   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt))
+
+#define DEFINE_MRS_S   \
+   __DEFINE_MRS_MSR_S_REGNUM   \
+"  .macro  mrs_s, rt, sreg\n"  \
+   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt)) \
 "  .endm\n"
-"\n"
-"  .macro  msr_s, sreg, rt\n"
-   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt))
+
+#define DEFINE_MSR_S   \
+   __DEFINE_MRS_MSR_S_REGNUM   \
+"  .macro  msr_s, sreg, rt\n"  \
+   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt)) \
 "  .endm\n"
-);
+
+#define UNDEFINE_MRS_S \
+"  .purgem mrs_s\n"
+
+#define UNDEFINE_MSR_S \
+"  .purgem msr_s\n"
+
+#define __mrs_s(r, v)  \
+   DEFINE_MRS_S\
+"  mrs_s %0, " __stringify(r) "\n" \
+   UNDEFINE_MRS_S : "=r" (v)
+
+#define __msr_s(r, v)  \
+   DEFINE_MSR_S\
+"  msr_s " __stringify(r) ", %x0\n"\
+   UNDEFINE_MSR_S : : "rZ" (v)
 
 /*
  * Unlike read_cpuid, calls to read_sysreg are never expected to be
@@ -623,15 +642,15 @@ asm(
  * For registers without architectural names, or simply unsupported by
  * GAS.
  */
-#define read_sysreg_s(r) ({\
-   u64 __val;   

[PATCH v2 17/18] arm64: disable RANDOMIZE_MODULE_REGION_FULL with LTO_CLANG

2017-11-15 Thread Sami Tolvanen
RANDOMIZE_MODULE_REGION_FULL results in "overflow in relocation type 275"
when loading a module linked with GNU gold. As a workaround, disable when
LTO_CLANG is selected.

Signed-off-by: Sami Tolvanen 
---
 arch/arm64/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index ba6aab55d464..3a70f763e18a 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -1064,7 +1064,7 @@ config RANDOMIZE_BASE
 
 config RANDOMIZE_MODULE_REGION_FULL
bool "Randomize the module region independently from the core kernel"
-   depends on RANDOMIZE_BASE
+   depends on RANDOMIZE_BASE && !LTO_CLANG
default y
help
  Randomizes the location of the module region without considering the
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 10/18] arm64: add a workaround for GNU gold with ARM64_MODULE_PLTS

2017-11-15 Thread Sami Tolvanen
CONFIG_CLANG_LTO depends on GNU gold and due to a known bug, the
linker crashes when ARM64_MODULE_PLTS is enabled:

  https://sourceware.org/bugzilla/show_bug.cgi?id=14592

To work around the problem, this change removes NOLOAD from .plt
and .init.plt, which allows us to link modules with ld.gold.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/kernel/module.lds | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/module.lds b/arch/arm64/kernel/module.lds
index f7c9781a9d48..eacb5c67f61e 100644
--- a/arch/arm64/kernel/module.lds
+++ b/arch/arm64/kernel/module.lds
@@ -1,4 +1,4 @@
 SECTIONS {
-   .plt (NOLOAD) : { BYTE(0) }
-   .init.plt (NOLOAD) : { BYTE(0) }
+   .plt : { BYTE(0) }
+   .init.plt : { BYTE(0) }
 }
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 10/18] arm64: add a workaround for GNU gold with ARM64_MODULE_PLTS

2017-11-15 Thread Sami Tolvanen
CONFIG_CLANG_LTO depends on GNU gold and due to a known bug, the
linker crashes when ARM64_MODULE_PLTS is enabled:

  https://sourceware.org/bugzilla/show_bug.cgi?id=14592

To work around the problem, this change removes NOLOAD from .plt
and .init.plt, which allows us to link modules with ld.gold.

Signed-off-by: Sami Tolvanen 
---
 arch/arm64/kernel/module.lds | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/module.lds b/arch/arm64/kernel/module.lds
index f7c9781a9d48..eacb5c67f61e 100644
--- a/arch/arm64/kernel/module.lds
+++ b/arch/arm64/kernel/module.lds
@@ -1,4 +1,4 @@
 SECTIONS {
-   .plt (NOLOAD) : { BYTE(0) }
-   .init.plt (NOLOAD) : { BYTE(0) }
+   .plt : { BYTE(0) }
+   .init.plt : { BYTE(0) }
 }
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 18/18] arm64: select ARCH_SUPPORTS_LTO_CLANG

2017-11-15 Thread Sami Tolvanen
Allow CONFIG_LTO_CLANG to be enabled for the architecture.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/Kconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 3a70f763e18a..58504327b9f6 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -40,6 +40,7 @@ config ARM64
select ARCH_INLINE_WRITE_UNLOCK_IRQRESTORE if !PREEMPT
select ARCH_USE_CMPXCHG_LOCKREF
select ARCH_USE_QUEUED_RWLOCKS
+   select ARCH_SUPPORTS_LTO_CLANG
select ARCH_SUPPORTS_MEMORY_FAILURE
select ARCH_SUPPORTS_ATOMIC_RMW
select ARCH_SUPPORTS_NUMA_BALANCING
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 18/18] arm64: select ARCH_SUPPORTS_LTO_CLANG

2017-11-15 Thread Sami Tolvanen
Allow CONFIG_LTO_CLANG to be enabled for the architecture.

Signed-off-by: Sami Tolvanen 
---
 arch/arm64/Kconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 3a70f763e18a..58504327b9f6 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -40,6 +40,7 @@ config ARM64
select ARCH_INLINE_WRITE_UNLOCK_IRQRESTORE if !PREEMPT
select ARCH_USE_CMPXCHG_LOCKREF
select ARCH_USE_QUEUED_RWLOCKS
+   select ARCH_SUPPORTS_LTO_CLANG
select ARCH_SUPPORTS_MEMORY_FAILURE
select ARCH_SUPPORTS_ATOMIC_RMW
select ARCH_SUPPORTS_NUMA_BALANCING
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 12/18] kbuild: add support for clang LTO

2017-11-15 Thread Sami Tolvanen
This change adds the configuration option CONFIG_LTO_CLANG, and
build system support for clang's Link Time Optimization (LTO). In
preparation for LTO support for other compilers, potentially common
parts of the changes are gated behind CONFIG_LTO instead.

With -flto, instead of object files, clang produces LLVM bitcode,
which is compiled into a native object at link time, allowing the
final binary to be optimized globally. For more details, see:

  https://llvm.org/docs/LinkTimeOptimization.html

While the kernel normally uses GNU ld for linking, LLVM supports LTO
only with lld or GNU gold linkers. This patch set assumes gold will
be used with the LLVMgold plug-in to perform the LTO link step. Due
to potential incompatibilities with GNU ld, this change also adds
LDFINAL_vmlinux for using a different linker for the vmlinux_link
step, and defaults to using GNU ld.

Assuming LLVMgold.so is in LD_LIBRARY_PATH and CONFIG_LTO_CLANG has
been selected, an LTO kernel can be built simply by running make
CC=clang. Recommended versions are >= 5.0 for clang, and >= 2.27 for
binutils.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 .gitignore   |  2 ++
 Makefile | 54 +++-
 arch/Kconfig | 32 +
 scripts/Makefile.build   | 66 +-
 scripts/Makefile.modpost | 63 -
 scripts/link-vmlinux.sh  | 92 ++--
 6 files changed, 288 insertions(+), 21 deletions(-)

diff --git a/.gitignore b/.gitignore
index 6c119eab5d46..ac236e2bb9b1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,6 +11,7 @@
 #
 .*
 *.a
+*.a.*
 *.bin
 *.bz2
 *.c.[012]*.*
@@ -28,6 +29,7 @@
 *.lzma
 *.lzo
 *.mod.c
+*.modversions
 *.o
 *.o.*
 *.order
diff --git a/Makefile b/Makefile
index f976af9525bf..8141b4c8f1bf 100644
--- a/Makefile
+++ b/Makefile
@@ -350,6 +350,7 @@ include scripts/Kbuild.include
 # Make variables (CC, etc...)
 AS = $(CROSS_COMPILE)as
 LD = $(CROSS_COMPILE)ld
+LDGOLD = $(CROSS_COMPILE)ld.gold
 CC = $(CROSS_COMPILE)gcc
 CPP= $(CC) -E
 AR = $(CROSS_COMPILE)ar
@@ -623,6 +624,15 @@ endif
 # Defaults to vmlinux, but the arch makefile usually adds further targets
 all: vmlinux
 
+# Make toolchain changes before including arch/$(SRCARCH)/Makefile to ensure
+# cc-/ld-* macros return correct values.
+ifdef CONFIG_LTO_CLANG
+# use GNU gold with LLVMgold for LTO linking, and LD for vmlinux_link
+LDFINAL_vmlinux := $(LD)
+LD := $(LDGOLD)
+LDFLAGS_GOLD   += -plugin LLVMgold.so
+endif
+
 # The arch Makefile can set ARCH_{CPP,A,C}FLAGS to override the default
 # values of the respective KBUILD_* variables
 ARCH_CPPFLAGS :=
@@ -789,6 +799,32 @@ KBUILD_CFLAGS  += $(call 
cc-option,-ffunction-sections,)
 KBUILD_CFLAGS  += $(call cc-option,-fdata-sections,)
 endif
 
+ifdef CONFIG_LTO_CLANG
+lto-clang-flags:= -flto -fvisibility=hidden
+
+# allow disabling only clang LTO where needed 
+DISABLE_LTO_CLANG := -fno-lto
+export DISABLE_LTO_CLANG
+
+ifdef CONFIG_MODVERSIONS
+# llvm-dis is used instead of objdump to process LLVM IR files
+LLVM_DIS   := llvm-dis
+export LLVM_DIS
+endif
+endif
+
+ifdef CONFIG_LTO
+lto-flags  := $(lto-clang-flags)
+KBUILD_CFLAGS  += $(lto-flags)
+
+DISABLE_LTO:= $(DISABLE_LTO_CLANG)
+export DISABLE_LTO
+
+# LDFINAL_vmlinux and LDFLAGS_FINAL_vmlinux can be set to override
+# the linker and flags for vmlinux_link.
+export LDFINAL_vmlinux LDFLAGS_FINAL_vmlinux
+endif
+
 # arch Makefile may override CC so keep this after arch Makefile is included
 NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
 CHECKFLAGS += $(NOSTDINC_FLAGS)
@@ -1090,6 +1126,19 @@ prepare-objtool: $(objtool_target)
 # CC_STACKPROTECTOR_STRONG! Why did it build with _REGULAR?!")
 PHONY += prepare-compiler-check
 prepare-compiler-check: FORCE
+# Make sure we're using clang with LTO_CLANG
+ifdef CONFIG_LTO_CLANG
+  ifneq ($(cc-name),clang)
+   @echo Cannot use CONFIG_LTO_CLANG without CC=clang >&2 && exit 1
+  endif
+endif
+# Make sure compiler supports LTO flags
+ifdef lto-flags
+  ifeq ($(call cc-option, $(lto-flags)),)
+   @echo Cannot use CONFIG_LTO: $(lto-flags) not supported by compiler \
+   >&2 && exit 1
+  endif
+endif
 # Make sure compiler supports requested stack protector flag.
 ifdef stackp-name
   ifeq ($(call cc-option, $(stackp-flag)),)
@@ -1564,7 +1613,10 @@ clean: $(clean-dirs)
-o -name modules.builtin -o -name '.tmp_*.o.*' \
-o -name '*.c.[012]*.*' \
-o -name '*.ll' \
-   -o -name '*.gcno' \) -type f -print | xargs rm -f
+   -o -name '*.gcno' \
+   -o -name '*.[oa].objects' \
+   -o -name '*.o.symversions' \
+   -o -name '*.modversions' \) -type f -pri

[PATCH v2 12/18] kbuild: add support for clang LTO

2017-11-15 Thread Sami Tolvanen
This change adds the configuration option CONFIG_LTO_CLANG, and
build system support for clang's Link Time Optimization (LTO). In
preparation for LTO support for other compilers, potentially common
parts of the changes are gated behind CONFIG_LTO instead.

With -flto, instead of object files, clang produces LLVM bitcode,
which is compiled into a native object at link time, allowing the
final binary to be optimized globally. For more details, see:

  https://llvm.org/docs/LinkTimeOptimization.html

While the kernel normally uses GNU ld for linking, LLVM supports LTO
only with lld or GNU gold linkers. This patch set assumes gold will
be used with the LLVMgold plug-in to perform the LTO link step. Due
to potential incompatibilities with GNU ld, this change also adds
LDFINAL_vmlinux for using a different linker for the vmlinux_link
step, and defaults to using GNU ld.

Assuming LLVMgold.so is in LD_LIBRARY_PATH and CONFIG_LTO_CLANG has
been selected, an LTO kernel can be built simply by running make
CC=clang. Recommended versions are >= 5.0 for clang, and >= 2.27 for
binutils.

Signed-off-by: Sami Tolvanen 
---
 .gitignore   |  2 ++
 Makefile | 54 +++-
 arch/Kconfig | 32 +
 scripts/Makefile.build   | 66 +-
 scripts/Makefile.modpost | 63 -
 scripts/link-vmlinux.sh  | 92 ++--
 6 files changed, 288 insertions(+), 21 deletions(-)

diff --git a/.gitignore b/.gitignore
index 6c119eab5d46..ac236e2bb9b1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,6 +11,7 @@
 #
 .*
 *.a
+*.a.*
 *.bin
 *.bz2
 *.c.[012]*.*
@@ -28,6 +29,7 @@
 *.lzma
 *.lzo
 *.mod.c
+*.modversions
 *.o
 *.o.*
 *.order
diff --git a/Makefile b/Makefile
index f976af9525bf..8141b4c8f1bf 100644
--- a/Makefile
+++ b/Makefile
@@ -350,6 +350,7 @@ include scripts/Kbuild.include
 # Make variables (CC, etc...)
 AS = $(CROSS_COMPILE)as
 LD = $(CROSS_COMPILE)ld
+LDGOLD = $(CROSS_COMPILE)ld.gold
 CC = $(CROSS_COMPILE)gcc
 CPP= $(CC) -E
 AR = $(CROSS_COMPILE)ar
@@ -623,6 +624,15 @@ endif
 # Defaults to vmlinux, but the arch makefile usually adds further targets
 all: vmlinux
 
+# Make toolchain changes before including arch/$(SRCARCH)/Makefile to ensure
+# cc-/ld-* macros return correct values.
+ifdef CONFIG_LTO_CLANG
+# use GNU gold with LLVMgold for LTO linking, and LD for vmlinux_link
+LDFINAL_vmlinux := $(LD)
+LD := $(LDGOLD)
+LDFLAGS_GOLD   += -plugin LLVMgold.so
+endif
+
 # The arch Makefile can set ARCH_{CPP,A,C}FLAGS to override the default
 # values of the respective KBUILD_* variables
 ARCH_CPPFLAGS :=
@@ -789,6 +799,32 @@ KBUILD_CFLAGS  += $(call 
cc-option,-ffunction-sections,)
 KBUILD_CFLAGS  += $(call cc-option,-fdata-sections,)
 endif
 
+ifdef CONFIG_LTO_CLANG
+lto-clang-flags:= -flto -fvisibility=hidden
+
+# allow disabling only clang LTO where needed 
+DISABLE_LTO_CLANG := -fno-lto
+export DISABLE_LTO_CLANG
+
+ifdef CONFIG_MODVERSIONS
+# llvm-dis is used instead of objdump to process LLVM IR files
+LLVM_DIS   := llvm-dis
+export LLVM_DIS
+endif
+endif
+
+ifdef CONFIG_LTO
+lto-flags  := $(lto-clang-flags)
+KBUILD_CFLAGS  += $(lto-flags)
+
+DISABLE_LTO:= $(DISABLE_LTO_CLANG)
+export DISABLE_LTO
+
+# LDFINAL_vmlinux and LDFLAGS_FINAL_vmlinux can be set to override
+# the linker and flags for vmlinux_link.
+export LDFINAL_vmlinux LDFLAGS_FINAL_vmlinux
+endif
+
 # arch Makefile may override CC so keep this after arch Makefile is included
 NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
 CHECKFLAGS += $(NOSTDINC_FLAGS)
@@ -1090,6 +1126,19 @@ prepare-objtool: $(objtool_target)
 # CC_STACKPROTECTOR_STRONG! Why did it build with _REGULAR?!")
 PHONY += prepare-compiler-check
 prepare-compiler-check: FORCE
+# Make sure we're using clang with LTO_CLANG
+ifdef CONFIG_LTO_CLANG
+  ifneq ($(cc-name),clang)
+   @echo Cannot use CONFIG_LTO_CLANG without CC=clang >&2 && exit 1
+  endif
+endif
+# Make sure compiler supports LTO flags
+ifdef lto-flags
+  ifeq ($(call cc-option, $(lto-flags)),)
+   @echo Cannot use CONFIG_LTO: $(lto-flags) not supported by compiler \
+   >&2 && exit 1
+  endif
+endif
 # Make sure compiler supports requested stack protector flag.
 ifdef stackp-name
   ifeq ($(call cc-option, $(stackp-flag)),)
@@ -1564,7 +1613,10 @@ clean: $(clean-dirs)
-o -name modules.builtin -o -name '.tmp_*.o.*' \
-o -name '*.c.[012]*.*' \
-o -name '*.ll' \
-   -o -name '*.gcno' \) -type f -print | xargs rm -f
+   -o -name '*.gcno' \
+   -o -name '*.[oa].objects' \
+   -o -name '*.o.symversions' \
+   -o -name '*.modversions' \) -type f -print | x

[PATCH v2 14/18] scripts/mod: disable LTO for empty.c

2017-11-15 Thread Sami Tolvanen
With CONFIG_LTO_CLANG, clang generates LLVM IR instead of ELF object
files. As empty.o is used for probing target properties, disable LTO
for it to produce an object file instead.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 scripts/mod/Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/scripts/mod/Makefile b/scripts/mod/Makefile
index 42c5d50f2bcc..e014b2fdd069 100644
--- a/scripts/mod/Makefile
+++ b/scripts/mod/Makefile
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 OBJECT_FILES_NON_STANDARD := y
+CFLAGS_empty.o += $(DISABLE_LTO)
 
 hostprogs-y:= modpost mk_elfconfig
 always := $(hostprogs-y) empty.o
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 14/18] scripts/mod: disable LTO for empty.c

2017-11-15 Thread Sami Tolvanen
With CONFIG_LTO_CLANG, clang generates LLVM IR instead of ELF object
files. As empty.o is used for probing target properties, disable LTO
for it to produce an object file instead.

Signed-off-by: Sami Tolvanen 
---
 scripts/mod/Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/scripts/mod/Makefile b/scripts/mod/Makefile
index 42c5d50f2bcc..e014b2fdd069 100644
--- a/scripts/mod/Makefile
+++ b/scripts/mod/Makefile
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 OBJECT_FILES_NON_STANDARD := y
+CFLAGS_empty.o += $(DISABLE_LTO)
 
 hostprogs-y:= modpost mk_elfconfig
 always := $(hostprogs-y) empty.o
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 13/18] kbuild: fix dynamic ftrace with clang LTO

2017-11-15 Thread Sami Tolvanen
With CONFIG_LTO_CLANG enabled, LLVM IR won't be compiled into object
files until modpost_link. This change postpones calls to recordmcount
until after this step.

In order to exclude ftrace_process_locs from inspection, we add a new
code section .text..ftrace, which we tell recordmcount to ignore, and
a __norecordmcount attribute for moving functions to this section.

Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/Kconfig  |  2 +-
 include/asm-generic/vmlinux.lds.h |  1 +
 include/linux/compiler-clang.h|  7 +++
 include/linux/compiler_types.h|  4 
 kernel/trace/ftrace.c |  6 +++---
 scripts/Makefile.build| 14 +-
 scripts/Makefile.modpost  |  4 
 scripts/link-vmlinux.sh   | 16 
 scripts/recordmcount.c|  3 ++-
 9 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/arch/Kconfig b/arch/Kconfig
index bb5296ecebdd..e0d0084308e2 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -620,7 +620,7 @@ config ARCH_SUPPORTS_LTO_CLANG
 config LTO_CLANG
bool "Use clang Link Time Optimization (LTO)"
depends on ARCH_SUPPORTS_LTO_CLANG
-   depends on !FTRACE_MCOUNT_RECORD
+   depends on !FTRACE_MCOUNT_RECORD || HAVE_C_RECORDMCOUNT
select LTO
select LD_DEAD_CODE_DATA_ELIMINATION
help
diff --git a/include/asm-generic/vmlinux.lds.h 
b/include/asm-generic/vmlinux.lds.h
index ce0244780988..c4c44ea73930 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -459,6 +459,7 @@
 #define TEXT_TEXT  \
ALIGN_FUNCTION();   \
*(.text.hot TEXT_MAIN .text.fixup .text.unlikely)   \
+   *(.text..ftrace)\
*(.text..refcount)  \
*(.ref.text)\
MEM_KEEP(init.text) \
diff --git a/include/linux/compiler-clang.h b/include/linux/compiler-clang.h
index a06583e41f80..62604537ea66 100644
--- a/include/linux/compiler-clang.h
+++ b/include/linux/compiler-clang.h
@@ -16,3 +16,10 @@
  * with any version that can compile the kernel
  */
 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
+
+#ifdef CONFIG_CLANG_LTO
+#ifdef CONFIG_FTRACE_MCOUNT_RECORD
+#define __norecordmcount \
+   __attribute__((__section__(".text..ftrace")))
+#endif
+#endif
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 6b79a9bba9a7..231c413c615a 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -253,6 +253,10 @@ struct ftrace_likely_data {
 # define __nostackprotector
 #endif
 
+#ifndef __norecordmcount
+#define __norecordmcount
+#endif
+
 /*
  * Assume alignment of return value.
  */
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 8319e09e15b9..e117b849f9dc 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -5573,9 +5573,9 @@ static int ftrace_cmp_ips(const void *a, const void *b)
return 0;
 }
 
-static int ftrace_process_locs(struct module *mod,
-  unsigned long *start,
-  unsigned long *end)
+static int __norecordmcount ftrace_process_locs(struct module *mod,
+   unsigned long *start,
+   unsigned long *end)
 {
struct ftrace_page *start_pg;
struct ftrace_page *pg;
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index e8bf5c440612..4d990c7cbe34 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -246,6 +246,12 @@ ifdef BUILD_C_RECORDMCOUNT
 ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
   RECORDMCOUNT_FLAGS = -w
 endif
+
+ifdef CONFIG_LTO_CLANG
+# With LTO, we postpone running recordmcount until after the LTO link step, so
+# let's export the parameters for the link script.
+export RECORDMCOUNT_FLAGS
+else
 # Due to recursion, we must skip empty.o.
 # The empty.o file is created in the make process in order to determine
 # the target endianness and word size. It is made before all other C
@@ -254,17 +260,22 @@ sub_cmd_record_mcount =   
\
if [ $(@) != "scripts/mod/empty.o" ]; then  \
$(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)";   
\
fi;
+endif
+
 recordmcount_source := $(srctree)/scripts/recordmcount.c \
$(srctree)/scripts/recordmcount.h
-else
+else # !BUILD_C_RECORDMCOUNT
 sub_cmd_record_mcount = set -e ; perl $(srctree)/scripts/recordmcount.pl 
"$(ARCH)" \
"$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)&q

[PATCH v2 13/18] kbuild: fix dynamic ftrace with clang LTO

2017-11-15 Thread Sami Tolvanen
With CONFIG_LTO_CLANG enabled, LLVM IR won't be compiled into object
files until modpost_link. This change postpones calls to recordmcount
until after this step.

In order to exclude ftrace_process_locs from inspection, we add a new
code section .text..ftrace, which we tell recordmcount to ignore, and
a __norecordmcount attribute for moving functions to this section.

Signed-off-by: Sami Tolvanen 
---
 arch/Kconfig  |  2 +-
 include/asm-generic/vmlinux.lds.h |  1 +
 include/linux/compiler-clang.h|  7 +++
 include/linux/compiler_types.h|  4 
 kernel/trace/ftrace.c |  6 +++---
 scripts/Makefile.build| 14 +-
 scripts/Makefile.modpost  |  4 
 scripts/link-vmlinux.sh   | 16 
 scripts/recordmcount.c|  3 ++-
 9 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/arch/Kconfig b/arch/Kconfig
index bb5296ecebdd..e0d0084308e2 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -620,7 +620,7 @@ config ARCH_SUPPORTS_LTO_CLANG
 config LTO_CLANG
bool "Use clang Link Time Optimization (LTO)"
depends on ARCH_SUPPORTS_LTO_CLANG
-   depends on !FTRACE_MCOUNT_RECORD
+   depends on !FTRACE_MCOUNT_RECORD || HAVE_C_RECORDMCOUNT
select LTO
select LD_DEAD_CODE_DATA_ELIMINATION
help
diff --git a/include/asm-generic/vmlinux.lds.h 
b/include/asm-generic/vmlinux.lds.h
index ce0244780988..c4c44ea73930 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -459,6 +459,7 @@
 #define TEXT_TEXT  \
ALIGN_FUNCTION();   \
*(.text.hot TEXT_MAIN .text.fixup .text.unlikely)   \
+   *(.text..ftrace)\
*(.text..refcount)  \
*(.ref.text)\
MEM_KEEP(init.text) \
diff --git a/include/linux/compiler-clang.h b/include/linux/compiler-clang.h
index a06583e41f80..62604537ea66 100644
--- a/include/linux/compiler-clang.h
+++ b/include/linux/compiler-clang.h
@@ -16,3 +16,10 @@
  * with any version that can compile the kernel
  */
 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
+
+#ifdef CONFIG_CLANG_LTO
+#ifdef CONFIG_FTRACE_MCOUNT_RECORD
+#define __norecordmcount \
+   __attribute__((__section__(".text..ftrace")))
+#endif
+#endif
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 6b79a9bba9a7..231c413c615a 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -253,6 +253,10 @@ struct ftrace_likely_data {
 # define __nostackprotector
 #endif
 
+#ifndef __norecordmcount
+#define __norecordmcount
+#endif
+
 /*
  * Assume alignment of return value.
  */
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 8319e09e15b9..e117b849f9dc 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -5573,9 +5573,9 @@ static int ftrace_cmp_ips(const void *a, const void *b)
return 0;
 }
 
-static int ftrace_process_locs(struct module *mod,
-  unsigned long *start,
-  unsigned long *end)
+static int __norecordmcount ftrace_process_locs(struct module *mod,
+   unsigned long *start,
+   unsigned long *end)
 {
struct ftrace_page *start_pg;
struct ftrace_page *pg;
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index e8bf5c440612..4d990c7cbe34 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -246,6 +246,12 @@ ifdef BUILD_C_RECORDMCOUNT
 ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
   RECORDMCOUNT_FLAGS = -w
 endif
+
+ifdef CONFIG_LTO_CLANG
+# With LTO, we postpone running recordmcount until after the LTO link step, so
+# let's export the parameters for the link script.
+export RECORDMCOUNT_FLAGS
+else
 # Due to recursion, we must skip empty.o.
 # The empty.o file is created in the make process in order to determine
 # the target endianness and word size. It is made before all other C
@@ -254,17 +260,22 @@ sub_cmd_record_mcount =   
\
if [ $(@) != "scripts/mod/empty.o" ]; then  \
$(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)";   
\
fi;
+endif
+
 recordmcount_source := $(srctree)/scripts/recordmcount.c \
$(srctree)/scripts/recordmcount.h
-else
+else # !BUILD_C_RECORDMCOUNT
 sub_cmd_record_mcount = set -e ; perl $(srctree)/scripts/recordmcount.pl 
"$(ARCH)" \
"$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
"$(

[PATCH v2 16/18] arm64: crypto: disable LTO for aes-ce-cipher.c

2017-11-15 Thread Sami Tolvanen
CONFIG_LTO_CLANG requires the use of clang's integrated assembler, which
doesn't understand the inline assembly in aes-ce-cipher.c. Disable LTO for
the file to work around the issue.

Acked-by: Ard Biesheuvel <ard.biesheu...@linaro.org>
Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 arch/arm64/crypto/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm64/crypto/Makefile b/arch/arm64/crypto/Makefile
index b5edc5918c28..af08508521a3 100644
--- a/arch/arm64/crypto/Makefile
+++ b/arch/arm64/crypto/Makefile
@@ -24,7 +24,7 @@ obj-$(CONFIG_CRYPTO_CRC32_ARM64_CE) += crc32-ce.o
 crc32-ce-y:= crc32-ce-core.o crc32-ce-glue.o
 
 obj-$(CONFIG_CRYPTO_AES_ARM64_CE) += aes-ce-cipher.o
-CFLAGS_aes-ce-cipher.o += -march=armv8-a+crypto
+CFLAGS_aes-ce-cipher.o += -march=armv8-a+crypto $(DISABLE_LTO_CLANG)
 
 obj-$(CONFIG_CRYPTO_AES_ARM64_CE_CCM) += aes-ce-ccm.o
 aes-ce-ccm-y := aes-ce-ccm-glue.o aes-ce-ccm-core.o
-- 
2.15.0.448.gf294e3d99a-goog



[PATCH v2 16/18] arm64: crypto: disable LTO for aes-ce-cipher.c

2017-11-15 Thread Sami Tolvanen
CONFIG_LTO_CLANG requires the use of clang's integrated assembler, which
doesn't understand the inline assembly in aes-ce-cipher.c. Disable LTO for
the file to work around the issue.

Acked-by: Ard Biesheuvel 
Signed-off-by: Sami Tolvanen 
---
 arch/arm64/crypto/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm64/crypto/Makefile b/arch/arm64/crypto/Makefile
index b5edc5918c28..af08508521a3 100644
--- a/arch/arm64/crypto/Makefile
+++ b/arch/arm64/crypto/Makefile
@@ -24,7 +24,7 @@ obj-$(CONFIG_CRYPTO_CRC32_ARM64_CE) += crc32-ce.o
 crc32-ce-y:= crc32-ce-core.o crc32-ce-glue.o
 
 obj-$(CONFIG_CRYPTO_AES_ARM64_CE) += aes-ce-cipher.o
-CFLAGS_aes-ce-cipher.o += -march=armv8-a+crypto
+CFLAGS_aes-ce-cipher.o += -march=armv8-a+crypto $(DISABLE_LTO_CLANG)
 
 obj-$(CONFIG_CRYPTO_AES_ARM64_CE_CCM) += aes-ce-ccm.o
 aes-ce-ccm-y := aes-ce-ccm-glue.o aes-ce-ccm-core.o
-- 
2.15.0.448.gf294e3d99a-goog



<    3   4   5   6   7   8   9   10   >