[tip:x86/urgent] compiler-gcc.h: Add __attribute__((gnu_inline)) to all inline declarations

From: tip-bot for Nick Desaulniers
Date: Tue Jul 03 2018 - 11:59:46 EST


Commit-ID: d03db2bc26f0e4a6849ad649a09c9c73fccdc656
Gitweb: https://git.kernel.org/tip/d03db2bc26f0e4a6849ad649a09c9c73fccdc656
Author: Nick Desaulniers <ndesaulniers@xxxxxxxxxx>
AuthorDate: Thu, 21 Jun 2018 09:23:22 -0700
Committer: Ingo Molnar <mingo@xxxxxxxxxx>
CommitDate: Tue, 3 Jul 2018 10:56:27 +0200

compiler-gcc.h: Add __attribute__((gnu_inline)) to all inline declarations

Functions marked extern inline do not emit an externally visible
function when the gnu89 C standard is used. Some KBUILD Makefiles
overwrite KBUILD_CFLAGS. This is an issue for GCC 5.1+ users as without
an explicit C standard specified, the default is gnu11. Since c99, the
semantics of extern inline have changed such that an externally visible
function is always emitted. This can lead to multiple definition errors
of extern inline functions at link time of compilation units whose build
files have removed an explicit C standard compiler flag for users of GCC
5.1+ or Clang.

Suggested-by: Arnd Bergmann <arnd@xxxxxxxx>
Suggested-by: H. Peter Anvin <hpa@xxxxxxxxx>
Suggested-by: Joe Perches <joe@xxxxxxxxxxx>
Signed-off-by: Nick Desaulniers <ndesaulniers@xxxxxxxxxx>
Acked-by: Juergen Gross <jgross@xxxxxxxx>
Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: acme@xxxxxxxxxx
Cc: akataria@xxxxxxxxxx
Cc: akpm@xxxxxxxxxxxxxxxxxxxx
Cc: andrea.parri@xxxxxxxxxxxxxxxxxxxx
Cc: ard.biesheuvel@xxxxxxxxxx
Cc: aryabinin@xxxxxxxxxxxxx
Cc: astrachan@xxxxxxxxxx
Cc: boris.ostrovsky@xxxxxxxxxx
Cc: brijesh.singh@xxxxxxx
Cc: caoj.fnst@xxxxxxxxxxxxxx
Cc: geert@xxxxxxxxxxxxxx
Cc: ghackmann@xxxxxxxxxx
Cc: gregkh@xxxxxxxxxxxxxxxxxxx
Cc: jan.kiszka@xxxxxxxxxxx
Cc: jarkko.sakkinen@xxxxxxxxxxxxxxx
Cc: jpoimboe@xxxxxxxxxx
Cc: keescook@xxxxxxxxxx
Cc: kirill.shutemov@xxxxxxxxxxxxxxx
Cc: kstewart@xxxxxxxxxxxxxxxxxxx
Cc: linux-efi@xxxxxxxxxxxxxxx
Cc: linux-kbuild@xxxxxxxxxxxxxxx
Cc: manojgupta@xxxxxxxxxx
Cc: mawilcox@xxxxxxxxxxxxx
Cc: michal.lkml@xxxxxxxxxxx
Cc: mjg59@xxxxxxxxxx
Cc: mka@xxxxxxxxxxxx
Cc: pombredanne@xxxxxxxx
Cc: rientjes@xxxxxxxxxx
Cc: rostedt@xxxxxxxxxxx
Cc: sedat.dilek@xxxxxxxxx
Cc: thomas.lendacky@xxxxxxx
Cc: tstellar@xxxxxxxxxx
Cc: tweek@xxxxxxxxxx
Cc: virtualization@xxxxxxxxxxxxxxxxxxxxxxxxxx
Cc: will.deacon@xxxxxxx
Cc: yamada.masahiro@xxxxxxxxxxxxx
Link: http://lkml.kernel.org/r/20180621162324.36656-2-ndesaulniers@xxxxxxxxxx
Signed-off-by: Ingo Molnar <mingo@xxxxxxxxxx>
---
include/linux/compiler-gcc.h | 29 ++++++++++++++++++++++-------
1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
index fd282c7d3e5e..573f5a7d42d4 100644
--- a/include/linux/compiler-gcc.h
+++ b/include/linux/compiler-gcc.h
@@ -65,6 +65,18 @@
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
#endif

+/*
+ * Feature detection for gnu_inline (gnu89 extern inline semantics). Either
+ * __GNUC_STDC_INLINE__ is defined (not using gnu89 extern inline semantics,
+ * and we opt in to the gnu89 semantics), or __GNUC_STDC_INLINE__ is not
+ * defined so the gnu89 semantics are the default.
+ */
+#ifdef __GNUC_STDC_INLINE__
+# define __gnu_inline __attribute__((gnu_inline))
+#else
+# define __gnu_inline
+#endif
+
/*
* Force always-inline if the user requests it so via the .config,
* or if gcc is too old.
@@ -72,19 +84,22 @@
* -Wunused-function. This turns out to avoid the need for complex #ifdef
* directives. Suppress the warning in clang as well by using "unused"
* function attribute, which is redundant but not harmful for gcc.
+ * Prefer gnu_inline, so that extern inline functions do not emit an
+ * externally visible function. This makes extern inline behave as per gnu89
+ * semantics rather than c99. This prevents multiple symbol definition errors
+ * of extern inline functions at link time.
+ * A lot of inline functions can cause havoc with function tracing.
*/
#if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) || \
!defined(CONFIG_OPTIMIZE_INLINING) || (__GNUC__ < 4)
-#define inline inline __attribute__((always_inline,unused)) notrace
-#define __inline__ __inline__ __attribute__((always_inline,unused)) notrace
-#define __inline __inline __attribute__((always_inline,unused)) notrace
+#define inline \
+ inline __attribute__((always_inline, unused)) notrace __gnu_inline
#else
-/* A lot of inline functions can cause havoc with function tracing */
-#define inline inline __attribute__((unused)) notrace
-#define __inline__ __inline__ __attribute__((unused)) notrace
-#define __inline __inline __attribute__((unused)) notrace
+#define inline inline __attribute__((unused)) notrace __gnu_inline
#endif

+#define __inline__ inline
+#define __inline inline
#define __always_inline inline __attribute__((always_inline))
#define noinline __attribute__((noinline))