Skip to contentSkip to conversation

Compiler Attributes

PATCH Superseded Miguel Ojeda 5 participants 22 messages Sep 8, 2018 – Sep 10, 2018

Subsystems (5)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover Compiler Attributes

The Compiler Attributes series is an effort to disentangle the include/linux/compiler*.h headers and bring them up to date.

The main idea behind the series is to use feature checking macros (i.e. __has_attribute) instead of compiler version checks (e.g. GCC_VERSION), which are compiler-agnostic (so they can be shared, reducing the size of compiler-specific headers) and version-agnostic.

Other related improvements have been performed in the headers as well, which on top of the use of __has_attribute it has amounted to a significant simplification of these headers (e.g. GCC_VERSION is now only guarding 4 non-attribute macros).

This series should also help the efforts to support compiling the kernel with clang and icc. A fair amount of documentation and comments have also been added, clarified or removed; and the headers are now more readable, which should help kernel developers in general.

The series was triggered due to the move to gcc >= 4.6. In turn, this series has also triggered Sparse to gain the ability to recognize __has_attribute on its own.

You can also fetch it from:

https://github.com/ojeda/linux/tree/compiler-attributes-v4

Enjoy!

Cheers,
Miguel

Cc Jonathan Corbet <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Linus Torvalds <[email protected]>

v3 -> v4

This time it is an important fix I found while doing randconfigs, plus the documentation sentence that Nick suggested and the MAINTAINERS file.

Compile-tested for a while on (x86_64, gcc-7.3) on a few randconfs.

New:

* Add MAINTAINERS entry for the new file (compiler_attributes.h) so that we try to maintain that one as clean as possible. (if someone else wants to join, feel free!)

Modified:

* Fix inline macro

__always_inline cannot be used in the inline macro, because people marking a function as __always_inline would expand to inline which in turn would expand back to __always_inline (under some configs), which would not be expanded again. Added a comment about this in the inline macro.

Also added another comment about __always_inline in compiler_attributes.h to note that users expect to not have to write inline (as well as the attribute). We cannot simply remove "inline" there (which would solve the problem described above) because using the attribute is not enough for gcc. A function marked as always_inline seems to require to be marked as inline itself so that the attribute is applied:

"warning: always_inline function might not be inlinable [-Wattributes]"

From the gcc docs:

"For functions declared inline, this attribute inlines the function
independent of any restrictions that otherwise apply to inlining."

See https://godbolt.org/z/LpzUPj for an example.

From reviews:

* Add sentence about feature-detection in Docs (Nick)

Miguel Ojeda (13):
Compiler Attributes: remove unused attributes
Compiler Attributes: always use the extra-underscores syntax Compiler Attributes: remove unneeded tests
Compiler Attributes: homogenize __must_be_array Compiler Attributes: naked was fixed in gcc 4.6 Compiler Attributes: naked can be shared
Compiler Attributes: remove unneeded sparse (__CHECKER__) tests Compiler Attributes: add missing SPDX ID in compiler_types.h Compiler Attributes: use feature checks instead of version checks Compiler Attributes: KENTRY used twice the "used" attribute Compiler Attributes: remove uses of __attribute__ from compiler.h Compiler Attributes: add Doc/process/programming-language.rst Compiler Attributes: Add MAINTAINERS entry

 Documentation/process/index.rst               |   1 +
 .../process/programming-language.rst          |  45 ++++
 MAINTAINERS                                   |   5 +
 include/linux/compiler-clang.h                |   5 -
 include/linux/compiler-gcc.h                  |  84 +-----
 include/linux/compiler-intel.h                |   9 -
 include/linux/compiler.h                      |  19 +-
 include/linux/compiler_attributes.h           | 244 ++++++++++++++++++
 include/linux/compiler_types.h                | 105 ++------
 9 files changed, 329 insertions(+), 188 deletions(-)
 create mode 100644 Documentation/process/programming-language.rst
 create mode 100644 include/linux/compiler_attributes.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH v4 1/13 Compiler Attributes: remove unused attributes

__optimize and __deprecate_for_modules are unused in the whole kernel tree. Simply drop them.

Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler-gcc.h   | 2 --
 include/linux/compiler.h       | 4 ----
 include/linux/compiler_types.h | 1 -
 3 files changed, 7 deletions(-)
include/linux/compiler-gcc.h +0 -2
index 763bbad1e258..0a2d06677d83 100644
@@ -95,8 +95,6 @@
 
 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
 
-#define __optimize(level)	__attribute__((__optimize__(level)))
-
 #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
 
 #ifndef __CHECKER__
include/linux/compiler.h +0 -4
index 681d866efb1e..7c0157d50964 100644
@@ -301,10 +301,6 @@ static inline void *offset_to_ptr(const int *off)
 
 #endif /* __ASSEMBLY__ */
 
-#ifndef __optimize
-# define __optimize(level)
-#endif
-
 /* Compile time object size, -1 for unknown */
 #ifndef __compiletime_object_size
 # define __compiletime_object_size(obj) -1
include/linux/compiler_types.h +0 -1
index 3525c179698c..b6534292ea33 100644
@@ -108,7 +108,6 @@ struct ftrace_likely_data {
 
 /* Don't. Just don't. */
 #define __deprecated
-#define __deprecated_for_modules
 
 #endif /* __KERNEL__ */
 
Signature · 1 line
2.17.1
#2 lore raw

PATCH v4 2/13 Compiler Attributes: always use the extra-underscores syntax

In reply to Miguel Ojeda

The attribute syntax optionally allows to surround attribute names with "__" in order to avoid collisions with macros of the same name (see https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html).

This homogenizes all attributes to use the syntax with underscores. While there are currently only a handful of cases of some TUs defining macros like "error" which may collide with the attributes, this should prevent futures surprises.

This has been done only for "standard" attributes supported by the major compilers. In other words, those of third-party tools (e.g. sparse, plugins...) have not been changed for the moment.

Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler-clang.h |  2 +-
 include/linux/compiler-gcc.h   | 14 ++++++------
 include/linux/compiler-intel.h |  2 +-
 include/linux/compiler.h       |  8 +++----
 include/linux/compiler_types.h | 40 +++++++++++++++++-----------------
 5 files changed, 33 insertions(+), 33 deletions(-)
include/linux/compiler-clang.h +1 -1
index b1ce500fe8b3..d11cad61ba5c 100644
@@ -21,7 +21,7 @@
 #define __SANITIZE_ADDRESS__
 #endif
 
-#define __no_sanitize_address __attribute__((no_sanitize("address")))
+#define __no_sanitize_address __attribute__((__no_sanitize__("address")))
 
 /*
  * Not all versions of clang implement the the type-generic versions
include/linux/compiler-gcc.h +7 -7
index 0a2d06677d83..371b6fa2d074 100644
@@ -76,7 +76,7 @@
 #endif
 
 #ifdef RETPOLINE
-#define __noretpoline __attribute__((indirect_branch("keep")))
+#define __noretpoline __attribute__((__indirect_branch__("keep")))
 #endif
 
 /*
@@ -91,15 +91,15 @@
  * GCC 4.[56] currently fail to enforce this, so we must do so ourselves.
  * See GCC PR44290.
  */
-#define __naked		__attribute__((naked)) noinline __noclone notrace
+#define __naked		__attribute__((__naked__)) noinline __noclone notrace
 
 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
 
 #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
 
 #ifndef __CHECKER__
-#define __compiletime_warning(message) __attribute__((warning(message)))
-#define __compiletime_error(message) __attribute__((error(message)))
+#define __compiletime_warning(message) __attribute__((__warning__(message)))
+#define __compiletime_error(message) __attribute__((__error__(message)))
 
 #ifdef LATENT_ENTROPY_PLUGIN
 #define __latent_entropy __attribute__((latent_entropy))
@@ -148,7 +148,7 @@
  * optimizer that something else uses this function or variable, thus preventing
  * this.
  */
-#define __visible	__attribute__((externally_visible))
+#define __visible	__attribute__((__externally_visible__))
 
 /* gcc version specific checks */
 
@@ -205,7 +205,7 @@
  * should not be applied to that function.
  * Conflicts with inlining: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
  */
-#define __no_sanitize_address __attribute__((no_sanitize_address))
+#define __no_sanitize_address __attribute__((__no_sanitize_address__))
 #endif
 
 #if GCC_VERSION >= 50100
@@ -213,7 +213,7 @@
  * Mark structures as requiring designated initializers.
  * https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
  */
-#define __designated_init __attribute__((designated_init))
+#define __designated_init __attribute__((__designated_init__))
 #define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
 #endif
 
include/linux/compiler-intel.h +1 -1
index 4c7f9befa9f6..fef8bb3e53ef 100644
@@ -42,4 +42,4 @@
  * and may be redefined here because they should not be shared with other
  * compilers, like clang.
  */
-#define __visible	__attribute__((externally_visible))
+#define __visible	__attribute__((__externally_visible__))
include/linux/compiler.h +4 -4
index 7c0157d50964..ec4a28bad2c6 100644
@@ -24,7 +24,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 			long ______r;					\
 			static struct ftrace_likely_data		\
 				__attribute__((__aligned__(4)))		\
-				__attribute__((section("_ftrace_annotated_branch"))) \
+				__attribute__((__section__("_ftrace_annotated_branch"))) \
 				______f = {				\
 				.data.func = __func__,			\
 				.data.file = __FILE__,			\
@@ -60,7 +60,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 		int ______r;						\
 		static struct ftrace_branch_data			\
 			__attribute__((__aligned__(4)))			\
-			__attribute__((section("_ftrace_branch")))	\
+			__attribute__((__section__("_ftrace_branch")))	\
 			______f = {					\
 				.func = __func__,			\
 				.file = __FILE__,			\
@@ -146,7 +146,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 	extern typeof(sym) sym;					\
 	static const unsigned long __kentry_##sym		\
 	__used							\
-	__attribute__((section("___kentry" "+" #sym ), used))	\
+	__attribute__((__section__("___kentry" "+" #sym ), used))	\
 	= (unsigned long)&sym;
 #endif
 
@@ -287,7 +287,7 @@ unsigned long read_word_at_a_time(const void *addr)
  * visible to the compiler.
  */
 #define __ADDRESSABLE(sym) \
-	static void * __attribute__((section(".discard.addressable"), used)) \
+	static void * __attribute__((__section__(".discard.addressable"), used)) \
 		__PASTE(__addressable_##sym, __LINE__) = (void *)&sym;
 
 /**
include/linux/compiler_types.h +20 -20
index b6534292ea33..2bc0f94df38e 100644
@@ -195,26 +195,26 @@ struct ftrace_likely_data {
  * would be.
  * [...]
  */
-#define __pure			__attribute__((pure))
-#define __aligned(x)		__attribute__((aligned(x)))
-#define __aligned_largest	__attribute__((aligned))
-#define __printf(a, b)		__attribute__((format(printf, a, b)))
-#define __scanf(a, b)		__attribute__((format(scanf, a, b)))
-#define __maybe_unused		__attribute__((unused))
-#define __always_unused		__attribute__((unused))
-#define __mode(x)		__attribute__((mode(x)))
+#define __pure			__attribute__((__pure__))
+#define __aligned(x)		__attribute__((__aligned__(x)))
+#define __aligned_largest	__attribute__((__aligned__))
+#define __printf(a, b)		__attribute__((__format__(printf, a, b)))
+#define __scanf(a, b)		__attribute__((__format__(scanf, a, b)))
+#define __maybe_unused		__attribute__((__unused__))
+#define __always_unused		__attribute__((__unused__))
+#define __mode(x)		__attribute__((__mode__(x)))
 #define __malloc		__attribute__((__malloc__))
 #define __used			__attribute__((__used__))
-#define __noreturn		__attribute__((noreturn))
-#define __packed		__attribute__((packed))
-#define __weak			__attribute__((weak))
-#define __alias(symbol)		__attribute__((alias(#symbol)))
-#define __cold			__attribute__((cold))
+#define __noreturn		__attribute__((__noreturn__))
+#define __packed		__attribute__((__packed__))
+#define __weak			__attribute__((__weak__))
+#define __alias(symbol)		__attribute__((__alias__(#symbol)))
+#define __cold			__attribute__((__cold__))
 #define __section(S)		__attribute__((__section__(#S)))
 
 
 #ifdef CONFIG_ENABLE_MUST_CHECK
-#define __must_check		__attribute__((warn_unused_result))
+#define __must_check		__attribute__((__warn_unused_result__))
 #else
 #define __must_check
 #endif
@@ -222,7 +222,7 @@ struct ftrace_likely_data {
 #if defined(CC_USING_HOTPATCH) && !defined(__CHECKER__)
 #define notrace			__attribute__((hotpatch(0, 0)))
 #else
-#define notrace			__attribute__((no_instrument_function))
+#define notrace			__attribute__((__no_instrument_function__))
 #endif
 
 #define __compiler_offsetof(a, b)	__builtin_offsetof(a, b)
@@ -234,7 +234,7 @@ struct ftrace_likely_data {
  * defined so the gnu89 semantics are the default.
  */
 #ifdef __GNUC_STDC_INLINE__
-# define __gnu_inline	__attribute__((gnu_inline))
+# define __gnu_inline	__attribute__((__gnu_inline__))
 #else
 # define __gnu_inline
 #endif
@@ -254,17 +254,17 @@ struct ftrace_likely_data {
 #if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) || \
 	!defined(CONFIG_OPTIMIZE_INLINING)
 #define inline \
-	inline __attribute__((always_inline, unused)) notrace __gnu_inline
+	inline __attribute__((__always_inline__, __unused__)) notrace __gnu_inline
 #else
-#define inline inline	__attribute__((unused)) notrace __gnu_inline
+#define inline inline	__attribute__((__unused__)) notrace __gnu_inline
 #endif
 
 #define __inline__ inline
 #define __inline inline
-#define noinline	__attribute__((noinline))
+#define noinline	__attribute__((__noinline__))
 
 #ifndef __always_inline
-#define __always_inline inline __attribute__((always_inline))
+#define __always_inline inline __attribute__((__always_inline__))
 #endif
 
 /*
Signature · 1 line
2.17.1
#3 lore raw

PATCH v4 3/13 Compiler Attributes: remove unneeded tests

In reply to Miguel Ojeda

Attributes const and always_inline have tests around them which are unneeded, since they are supported by gcc >= 4.6, clang >= 3 and icc >= 13. https://godbolt.org/z/DFPq37

In the case of gnu_inline, we do not need to test for __GNUC_STDC_INLINE__ because, regardless of the current inlining behavior, we can simply always force the old GCC inlining behavior by using the attribute in all cases.

Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler_types.h | 23 +++--------------------
 1 file changed, 3 insertions(+), 20 deletions(-)
include/linux/compiler_types.h +3 -20
index 2bc0f94df38e..83475515bc39 100644
@@ -158,10 +158,6 @@ struct ftrace_likely_data {
 	(sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
 	 sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
 
-#ifndef __attribute_const__
-#define __attribute_const__	__attribute__((__const__))
-#endif
-
 #ifndef __noclone
 #define __noclone
 #endif
@@ -196,6 +192,7 @@ struct ftrace_likely_data {
  * [...]
  */
 #define __pure			__attribute__((__pure__))
+#define __attribute_const__	__attribute__((__const__))
 #define __aligned(x)		__attribute__((__aligned__(x)))
 #define __aligned_largest	__attribute__((__aligned__))
 #define __printf(a, b)		__attribute__((__format__(printf, a, b)))
@@ -211,6 +208,8 @@ struct ftrace_likely_data {
 #define __alias(symbol)		__attribute__((__alias__(#symbol)))
 #define __cold			__attribute__((__cold__))
 #define __section(S)		__attribute__((__section__(#S)))
+#define __always_inline		inline __attribute__((__always_inline__))
+#define __gnu_inline		__attribute__((__gnu_inline__))
 
 
 #ifdef CONFIG_ENABLE_MUST_CHECK
@@ -227,18 +226,6 @@ struct ftrace_likely_data {
 
 #define __compiler_offsetof(a, b)	__builtin_offsetof(a, b)
 
-/*
- * 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.
  * GCC does not warn about unused static inline functions for
@@ -263,10 +250,6 @@ struct ftrace_likely_data {
 #define __inline inline
 #define noinline	__attribute__((__noinline__))
 
-#ifndef __always_inline
-#define __always_inline inline __attribute__((__always_inline__))
-#endif
-
 /*
  * Rather then using noinline to prevent stack consumption, use
  * noinline_for_stack instead.  For documentation reasons.
Signature · 1 line
2.17.1
#4 lore raw

PATCH v4 4/13 Compiler Attributes: homogenize __must_be_array

In reply to Miguel Ojeda

Different definitions of __must_be_array:

* gcc: disabled for __CHECKER__

* clang: same definition as gcc's, but without __CHECKER__

* intel: the comment claims __builtin_types_compatible_p() is unsupported; but icc seems to support it since 13.0.1 (released in 2012). See https://godbolt.org/z/S0l6QQ

Therefore, we can remove all of them and have a single definition in compiler.h

Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler-clang.h | 1 -
 include/linux/compiler-gcc.h   | 7 -------
 include/linux/compiler-intel.h | 3 ---
 include/linux/compiler.h       | 7 +++++++
 4 files changed, 7 insertions(+), 11 deletions(-)
include/linux/compiler-clang.h +0 -1
index d11cad61ba5c..fa9532f8d885 100644
@@ -41,6 +41,5 @@
  * compilers, like ICC.
  */
 #define barrier() __asm__ __volatile__("" : : : "memory")
-#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
 #define __assume_aligned(a, ...)	\
 	__attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
include/linux/compiler-gcc.h +0 -7
index 371b6fa2d074..76f4907ef707 100644
@@ -68,13 +68,6 @@
  */
 #define uninitialized_var(x) x = x
 
-#ifdef __CHECKER__
-#define __must_be_array(a)	0
-#else
-/* &a[0] degrades to a pointer: a different type from an array */
-#define __must_be_array(a)	BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
-#endif
-
 #ifdef RETPOLINE
 #define __noretpoline __attribute__((__indirect_branch__("keep")))
 #endif
include/linux/compiler-intel.h +0 -3
index fef8bb3e53ef..6004b4588bd4 100644
@@ -29,9 +29,6 @@
  */
 #define OPTIMIZER_HIDE_VAR(var) barrier()
 
-/* Intel ECC compiler doesn't support __builtin_types_compatible_p() */
-#define __must_be_array(a) 0
-
 #endif
 
 /* icc has this, but it's called _bswap16 */
include/linux/compiler.h +7 -0
index ec4a28bad2c6..165b1d5683ed 100644
@@ -357,4 +357,11 @@ static inline void *offset_to_ptr(const int *off)
 	compiletime_assert(__native_word(t),				\
 		"Need native word sized stores/loads for atomicity.")
 
+#ifdef __CHECKER__
+#define __must_be_array(a)	0
+#else
+/* &a[0] degrades to a pointer: a different type from an array */
+#define __must_be_array(a)	BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
+#endif
+
 #endif /* __LINUX_COMPILER_H */
Signature · 1 line
2.17.1
#5 lore raw

PATCH v4 5/13 Compiler Attributes: naked was fixed in gcc 4.6

In reply to Miguel Ojeda

Commit 9c695203a7dd ("compiler-gcc.h: gcc-4.5 needs noclone and noinline on __naked functions") added noinline and noclone as a workaround for a gcc 4.5 bug, which was resolved in 4.6.0.

Since now the minimum gcc supported version is 4.6, we can clean it up.

See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44290
and https://godbolt.org/z/h6NMIL

Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler-gcc.h | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
include/linux/compiler-gcc.h +1 -7
index 76f4907ef707..4cd5e9264bce 100644
@@ -77,14 +77,8 @@
  * to trace naked functions because then mcount is called without
  * stack and frame pointer being set up and there is no chance to
  * restore the lr register to the value before mcount was called.
- *
- * The asm() bodies of naked functions often depend on standard calling
- * conventions, therefore they must be noinline and noclone.
- *
- * GCC 4.[56] currently fail to enforce this, so we must do so ourselves.
- * See GCC PR44290.
  */
-#define __naked		__attribute__((__naked__)) noinline __noclone notrace
+#define __naked		__attribute__((__naked__)) notrace
 
 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
 
Signature · 1 line
2.17.1
#6 lore raw

Re: [PATCH v4 05/13] Compiler Attributes: naked was fixed in gcc 4.6

Hi,

Quoted 35 lines · Miguel Ojeda

Commit 9c695203a7dd ("compiler-gcc.h: gcc-4.5 needs noclone and noinline on __naked functions") added noinline and noclone as a workaround for a gcc 4.5 bug, which was resolved in 4.6.0.

Since now the minimum gcc supported version is 4.6, we can clean it up.

See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44290
and https://godbolt.org/z/h6NMIL

Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler-gcc.h | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
include/linux/compiler-gcc.h +0 -0
index 76f4907ef707..4cd5e9264bce 100644

include/linux/compiler-gcc.h line 79

Quoted 3 lines
include/linux/compiler-gcc.h +0 -0
@@ -77,14 +77,8 @@
  * to trace naked functions because then mcount is called without
  * stack and frame pointer being set up and there is no chance to
  * restore the lr register to the value before mcount was called.

I think this is also no longer required since GCC 4.6 and newer only have the new mcount interface:

https://lore.kernel.org/lkml/[email protected]/T/#u

That said, I am not sure whether it is a good idea to enable tracing for naked functions even with the mcount interface.

--
Stefan

Quoted 11 lines
include/linux/compiler-gcc.h +1 -7
- *
- * The asm() bodies of naked functions often depend on standard calling
- * conventions, therefore they must be noinline and noclone.
- *
- * GCC 4.[56] currently fail to enforce this, so we must do so ourselves.
- * See GCC PR44290.
  */
-#define __naked		__attribute__((__naked__)) noinline __noclone notrace
+#define __naked		__attribute__((__naked__)) notrace
 
 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
#7 lore raw

PATCH v4 6/13 Compiler Attributes: naked can be shared

In reply to Miguel Ojeda

The naked attribute is supported by at least gcc >= 4.6 (for ARM, which is the only current user), gcc >= 8 (for x86), clang >= 3.1 and icc >= 13. See https://godbolt.org/z/350Dyc

Therefore, move it out of compiler-gcc.h so that the definition is shared by all compilers.

Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler-gcc.h   | 8 --------
 include/linux/compiler_types.h | 8 ++++++++
 2 files changed, 8 insertions(+), 8 deletions(-)
include/linux/compiler-gcc.h +0 -8
index 4cd5e9264bce..3b32bbfa5a49 100644
@@ -72,14 +72,6 @@
 #define __noretpoline __attribute__((__indirect_branch__("keep")))
 #endif
 
-/*
- * it doesn't make sense on ARM (currently the only user of __naked)
- * to trace naked functions because then mcount is called without
- * stack and frame pointer being set up and there is no chance to
- * restore the lr register to the value before mcount was called.
- */
-#define __naked		__attribute__((__naked__)) notrace
-
 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
 
 #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
include/linux/compiler_types.h +8 -0
index 83475515bc39..5ff9cda893f4 100644
@@ -224,6 +224,14 @@ struct ftrace_likely_data {
 #define notrace			__attribute__((__no_instrument_function__))
 #endif
 
+/*
+ * it doesn't make sense on ARM (currently the only user of __naked)
+ * to trace naked functions because then mcount is called without
+ * stack and frame pointer being set up and there is no chance to
+ * restore the lr register to the value before mcount was called.
+ */
+#define __naked			__attribute__((__naked__)) notrace
+
 #define __compiler_offsetof(a, b)	__builtin_offsetof(a, b)
 
 /*
Signature · 1 line
2.17.1
#8 lore raw

Re: [PATCH v4 06/13] Compiler Attributes: naked can be shared

Quoted 7 lines · Miguel Ojeda

The naked attribute is supported by at least gcc >= 4.6 (for ARM, which is the only current user), gcc >= 8 (for x86), clang >= 3.1 and icc >= 13. See https://godbolt.org/z/350Dyc

Therefore, move it out of compiler-gcc.h so that the definition is shared by all compilers.

This actually fixes Clang support for ARM32, it fixes: 815f0ddb346c ("include/linux/compiler*.h: make compiler-*.h mutually exclusive").

So this should go in v4.19. Is this patch set (patches 5/6 in particular) planned for v4.19? If not, we probably have to split out this fix...

--
Stefan

Quoted 59 lines
Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler-gcc.h   | 8 --------
 include/linux/compiler_types.h | 8 ++++++++
 2 files changed, 8 insertions(+), 8 deletions(-)
include/linux/compiler-gcc.h +0 -8
index 4cd5e9264bce..3b32bbfa5a49 100644
@@ -72,14 +72,6 @@
 #define __noretpoline __attribute__((__indirect_branch__("keep")))
 #endif
 
-/*
- * it doesn't make sense on ARM (currently the only user of __naked)
- * to trace naked functions because then mcount is called without
- * stack and frame pointer being set up and there is no chance to
- * restore the lr register to the value before mcount was called.
- */
-#define __naked		__attribute__((__naked__)) notrace
-
 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
 
 #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
include/linux/compiler_types.h +8 -0
index 83475515bc39..5ff9cda893f4 100644
@@ -224,6 +224,14 @@ struct ftrace_likely_data {
 #define notrace			__attribute__((__no_instrument_function__))
 #endif
 
+/*
+ * it doesn't make sense on ARM (currently the only user of __naked)
+ * to trace naked functions because then mcount is called without
+ * stack and frame pointer being set up and there is no chance to
+ * restore the lr register to the value before mcount was called.
+ */
+#define __naked			__attribute__((__naked__)) notrace
+
 #define __compiler_offsetof(a, b)	__builtin_offsetof(a, b)
 
 /*
#9 lore raw

PATCH v4 7/13 Compiler Attributes: remove unneeded sparse (__CHECKER__) tests

In reply to Miguel Ojeda

Sparse knows about a few more attributes now, so we can remove the __CHECKER__ conditions from them (which, in turn, allow us to move some of them later on to compiler_attributes.h).

* assume_aligned: since sparse's commit ffc860b ("sparse:
ignore __assume_aligned__ attribute"), included in 0.5.1

* error: since sparse's commit 0a04210 ("sparse: Add 'error' to ignored attributes"), included in 0.5.0

* hotpatch: since sparse's commit 6043210 ("sparse/parse.c:
ignore hotpatch attribute"), included in 0.5.1

* warning: since sparse's commit 977365d ("Avoid "attribute 'warning': unknown attribute" warning"), included in 0.4.2

On top of that, __must_be_array does not need it either because:

* Even ancient versions of sparse do not have a problem

* BUILD_BUG_ON_ZERO() is currently disabled for __CHECKER__

Cc Rasmus Villemoes <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler-gcc.h   | 6 ++----
 include/linux/compiler.h       | 4 ----
 include/linux/compiler_types.h | 2 +-
 3 files changed, 3 insertions(+), 9 deletions(-)
include/linux/compiler-gcc.h +2 -4
index 3b32bbfa5a49..1ca6a51cfaa9 100644
@@ -76,14 +76,12 @@
 
 #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
 
-#ifndef __CHECKER__
 #define __compiletime_warning(message) __attribute__((__warning__(message)))
 #define __compiletime_error(message) __attribute__((__error__(message)))
 
-#ifdef LATENT_ENTROPY_PLUGIN
+#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
 #define __latent_entropy __attribute__((latent_entropy))
 #endif
-#endif /* __CHECKER__ */
 
 /*
  * calling noreturn functions, __builtin_unreachable() and __builtin_trap()
@@ -131,7 +129,7 @@
 
 /* gcc version specific checks */
 
-#if GCC_VERSION >= 40900 && !defined(__CHECKER__)
+#if GCC_VERSION >= 40900
 /*
  * __assume_aligned(n, k): Tell the optimizer that the returned
  * pointer can be assumed to be k modulo n. The second argument is
include/linux/compiler.h +0 -4
index 165b1d5683ed..4030a2940d6b 100644
@@ -357,11 +357,7 @@ static inline void *offset_to_ptr(const int *off)
 	compiletime_assert(__native_word(t),				\
 		"Need native word sized stores/loads for atomicity.")
 
-#ifdef __CHECKER__
-#define __must_be_array(a)	0
-#else
 /* &a[0] degrades to a pointer: a different type from an array */
 #define __must_be_array(a)	BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
-#endif
 
 #endif /* __LINUX_COMPILER_H */
include/linux/compiler_types.h +1 -1
index 5ff9cda893f4..a3eceb3ad1b3 100644
@@ -218,7 +218,7 @@ struct ftrace_likely_data {
 #define __must_check
 #endif
 
-#if defined(CC_USING_HOTPATCH) && !defined(__CHECKER__)
+#if defined(CC_USING_HOTPATCH)
 #define notrace			__attribute__((hotpatch(0, 0)))
 #else
 #define notrace			__attribute__((__no_instrument_function__))
Signature · 1 line
2.17.1
#10 lore raw

PATCH v4 8/13 Compiler Attributes: add missing SPDX ID in compiler_types.h

In reply to Miguel Ojeda
Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler_types.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/compiler_types.h +1 -0
index a3eceb3ad1b3..ed7c0e4a180e 100644
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0 */
 #ifndef __LINUX_COMPILER_TYPES_H
 #define __LINUX_COMPILER_TYPES_H
 
Signature · 1 line
2.17.1
#11 lore raw

PATCH v4 9/13 Compiler Attributes: use feature checks instead of version checks

In reply to Miguel Ojeda

Instead of using version checks per-compiler to define (or not) each attribute, use __has_attribute to test for them, following the cleanup started with commit 815f0ddb346c ("include/linux/compiler*.h: make compiler-*.h mutually exclusive"), which is supported on gcc >= 5, clang >= 2.9 and icc >= 17. In the meantime, to support 4.6 <= gcc < 5, we implement __has_attribute by hand.

All the attributes that can be unconditionally defined and directly map to compiler attribute(s) (even if optional) have been moved to a new file include/linux/compiler_attributes.h

In an effort to make the file as regular as possible, comments stating the purpose of attributes have been removed. Instead, links to the compiler docs have been added (i.e. to gcc and, if available, to clang as well). In addition, they have been sorted.

Finally, if an attribute is optional (i.e. if it is guarded by __has_attribute), the reason has been stated for future reference.

Cc Rasmus Villemoes <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler-clang.h      |   4 -
 include/linux/compiler-gcc.h        |  51 ------
 include/linux/compiler-intel.h      |   6 -
 include/linux/compiler_attributes.h | 244 ++++++++++++++++++++++++++++
 include/linux/compiler_types.h      |  74 ++-------
 5 files changed, 254 insertions(+), 125 deletions(-)
 create mode 100644 include/linux/compiler_attributes.h
include/linux/compiler-clang.h +0 -4
index fa9532f8d885..3e7dafb3ea80 100644
@@ -21,8 +21,6 @@
 #define __SANITIZE_ADDRESS__
 #endif
 
-#define __no_sanitize_address __attribute__((__no_sanitize__("address")))
-
 /*
  * Not all versions of clang implement the the type-generic versions
  * of the builtin overflow checkers. Fortunately, clang implements
@@ -41,5 +39,3 @@
  * compilers, like ICC.
  */
 #define barrier() __asm__ __volatile__("" : : : "memory")
-#define __assume_aligned(a, ...)	\
-	__attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
include/linux/compiler-gcc.h +0 -51

73 lines not on this page · raw message

include/linux/compiler-intel.h +0 -6
index 6004b4588bd4..517bd14e1222 100644
@@ -34,9 +34,3 @@
 /* icc has this, but it's called _bswap16 */
 #define __HAVE_BUILTIN_BSWAP16__
 #define __builtin_bswap16 _bswap16
-
-/* The following are for compatibility with GCC, from compiler-gcc.h,
- * and may be redefined here because they should not be shared with other
- * compilers, like clang.
- */
-#define __visible	__attribute__((__externally_visible__))
include/linux/compiler_attributes.h new file +244 -0

244 lines not on this page · raw message

include/linux/compiler_types.h +10 -64

129 lines not on this page · raw message

Signature · 1 line
2.17.1
#12 lore raw

PATCH v4 10/13 Compiler Attributes: KENTRY used twice the "used" attribute

In reply to Miguel Ojeda
Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -1
index 4030a2940d6b..17ee9165ca51 100644
@@ -146,7 +146,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 	extern typeof(sym) sym;					\
 	static const unsigned long __kentry_##sym		\
 	__used							\
-	__attribute__((__section__("___kentry" "+" #sym ), used))	\
+	__attribute__((__section__("___kentry" "+" #sym )))	\
 	= (unsigned long)&sym;
 #endif
 
Signature · 1 line
2.17.1
#13 lore raw

PATCH v4 11/13 Compiler Attributes: remove uses of __attribute__ from compiler.h

In reply to Miguel Ojeda
Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 include/linux/compiler.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
include/linux/compiler.h +6 -6
index 17ee9165ca51..b5fb034fa6fa 100644
@@ -23,8 +23,8 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 #define __branch_check__(x, expect, is_constant) ({			\
 			long ______r;					\
 			static struct ftrace_likely_data		\
-				__attribute__((__aligned__(4)))		\
-				__attribute__((__section__("_ftrace_annotated_branch"))) \
+				__aligned(4)				\
+				__section("_ftrace_annotated_branch")	\
 				______f = {				\
 				.data.func = __func__,			\
 				.data.file = __FILE__,			\
@@ -59,8 +59,8 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 	({								\
 		int ______r;						\
 		static struct ftrace_branch_data			\
-			__attribute__((__aligned__(4)))			\
-			__attribute__((__section__("_ftrace_branch")))	\
+			__aligned(4)					\
+			__section("_ftrace_branch")			\
 			______f = {					\
 				.func = __func__,			\
 				.file = __FILE__,			\
@@ -146,7 +146,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 	extern typeof(sym) sym;					\
 	static const unsigned long __kentry_##sym		\
 	__used							\
-	__attribute__((__section__("___kentry" "+" #sym )))	\
+	__section("___kentry" "+" #sym )			\
 	= (unsigned long)&sym;
 #endif
 
@@ -287,7 +287,7 @@ unsigned long read_word_at_a_time(const void *addr)
  * visible to the compiler.
  */
 #define __ADDRESSABLE(sym) \
-	static void * __attribute__((__section__(".discard.addressable"), used)) \
+	static void * __section(".discard.addressable") __used \
 		__PASTE(__addressable_##sym, __LINE__) = (void *)&sym;
 
 /**
Signature · 1 line
2.17.1
#14 lore raw

PATCH v4 12/13 Compiler Attributes: add Doc/process/programming-language.rst

In reply to Miguel Ojeda
Cc Jonathan Corbet <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 Documentation/process/index.rst               |  1 +
 .../process/programming-language.rst          | 45 +++++++++++++++++++
 2 files changed, 46 insertions(+)
 create mode 100644 Documentation/process/programming-language.rst
Documentation/process/index.rst +1 -0
index 37bd0628b6ee..c56f24a22d2a 100644
@@ -23,6 +23,7 @@ Below are the essential guides that every developer should read.
    code-of-conflict
    development-process
    submitting-patches
+   programming-language
    coding-style
    maintainer-pgp-guide
    email-clients
Documentation/process/programming-language.rst new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer
#15 lore raw

Re: [PATCH v4 12/13] Compiler Attributes: add Doc/process/programming-language.rst

Quoted 5 lines · On Sat, 8 Sep 2018 23:24:58 +0200 Miguel Ojeda…
 Documentation/process/index.rst               |  1 +
 .../process/programming-language.rst          | 45 +++++++++++++++++++
 2 files changed, 46 insertions(+)
 create mode 100644 Documentation/process/programming-language.rst

So I have some overall thoughts on the documentation; my apologies for not getting to this until you got to v4...

1) I think the document is mistitled. It's not really about the language that the kernel used, it's about compiler attributes. So I would make both the name of the document and it introduction reflect that.

2) This is an ideal opportunity to document what all of those attributes actually mean. I would guess that is the information many developers will come here looking for, and they'll go away frustrated. The ideal thing to do, IMO, would be do say what each attribute means (rather than just which compilers support it) in a DOC section in the new compiler_attributes.h header, then use RST directives to pull all that information into this document.

Thanks,

jon

#16 lore raw

Re: [PATCH v4 12/13] Compiler Attributes: add Doc/process/programming-language.rst

Hi Jonathan,

Quoted 10 lines · Jonathan Corbet
Quoted 5 lines · On Sat, 8 Sep 2018 23:24:58 +0200 Miguel Ojeda…
 Documentation/process/index.rst               |  1 +
 .../process/programming-language.rst          | 45 +++++++++++++++++++
 2 files changed, 46 insertions(+)
 create mode 100644 Documentation/process/programming-language.rst

So I have some overall thoughts on the documentation; my apologies for not getting to this until you got to v4...

No problem at all! :-)

Quoted 4 lines

1) I think the document is mistitled. It's not really about the language that the kernel used, it's about compiler attributes. So I would make both the name of the document and it introduction reflect that.

The idea here is to create a document explaining the programming language that the kernel uses (which we hadn't), taking the chance to describe the attributes (which in a way are extensions to the C language); in the future expanding it with other (non-attribute) extensions that we use. Of course, that is not done, but I thought starting it was a nice idea (meant for people coming from a C background that do not know the "dialect" used in Linux).

Also other kind of very commonly used macros and functions used throughout the kernel could be summarized there as a summary for newcomers (e.g. stuff like printk/pr_*, likely/unlikely, panic, EXPORT_SYMBOL, BUG_ON/WARN_ON...), without writing full documentation there (but pointing to where they are described, either in the Docs or a source code if not).

What do you think?

Quoted 3 lines

2) This is an ideal opportunity to document what all of those attributes actually mean. I would guess that is the information many developers

I thought about that, but the issue is that compilers already describe
them: in the case of GCC, all of them are documented; some are in clang; icc is not well documented --- see the previous threads about this), so we would be duplicating that information (and it will become outdated as compilers are released and improve the documentation). This is the reason why the series removes most of the copy-pasted documentation from gcc and instead supplies a link for each attribute compiler_attributes.h to gcc & clang online docs.

Quoted 5 lines

will come here looking for, and they'll go away frustrated. The ideal thing to do, IMO, would be do say what each attribute means (rather than just which compilers support it) in a DOC section in the new compiler_attributes.h header, then use RST directives to pull all that information into this document.

Initially my idea was to provide a table with the name and the links to each compiler docs; so that it could be easily used. However, when thinking about it, it seems that most people consulting such file would come from the actual source code, so they would have to move from there to the Doc/ file; so I did it the other way around (IIRC Nick also mentioned his preference for keeping them in the source code).

Now, the idea of keeping them in the header but pulling them to the RST can be a good idea but I was unsure how to do it best. I took a look at how other RST files did it (the pull), but I thought (maybe wrongly) that I wouldn't be able to create a nice table (i.e. instead it would be a long list of attributes, which most people will not use -- and in my idea of a document describing all the extensions, it would be taking most of the space), so I discarded it. I am not sure about the future goals for the Docs/, so excuse me if I have the completely wrong idea, but in a way, in the current state, I see the kernel docs as articles/chapters/book on high-level concepts about the kernel, rather than a technical reference (i.e. the source code with a better formatting).

Thanks a lot for reviewing!

Cheers,
Miguel

#17 lore raw

PATCH v4 13/13 Compiler Attributes: Add MAINTAINERS entry

In reply to Miguel Ojeda
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 MAINTAINERS | 5 +++++
 1 file changed, 5 insertions(+)
MAINTAINERS +5 -0
index 9ad052aeac39..a7ef76d694d4 100644
@@ -3721,6 +3721,11 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/platform/x86/compal-laptop.c
 
+COMPILER ATTRIBUTES
+M:	Miguel Ojeda <[email protected]>
+S:	Maintained
+F:	include/linux/compiler_attributes.h
+
 CONEXANT ACCESSRUNNER USB DRIVER
 L:	[email protected]
 W:	http://accessrunner.sourceforge.net/
Signature · 1 line
2.17.1
Reviewed-by on the v4 series
#18 lore raw
In reply to Miguel Ojeda
Quoted 22 lines · Miguel Ojeda

The Compiler Attributes series is an effort to disentangle the include/linux/compiler*.h headers and bring them up to date.

The main idea behind the series is to use feature checking macros (i.e. __has_attribute) instead of compiler version checks (e.g. GCC_VERSION), which are compiler-agnostic (so they can be shared, reducing the size of compiler-specific headers) and version-agnostic.

Other related improvements have been performed in the headers as well, which on top of the use of __has_attribute it has amounted to a significant simplification of these headers (e.g. GCC_VERSION is now only guarding 4 non-attribute macros).

This series should also help the efforts to support compiling the kernel with clang and icc. A fair amount of documentation and comments have also been added, clarified or removed; and the headers are now more readable, which should help kernel developers in general.

The series was triggered due to the move to gcc >= 4.6. In turn, this series has also triggered Sparse to gain the ability to recognize __has_attribute on its own.

Quoted 5 lines

You can also fetch it from:

https://github.com/ojeda/linux/tree/compiler-attributes-v4

Enjoy!

It's a nice and welcomed cleanup! Everything look good to me, so feel free to add my

Cheers,
-- Luc

#19 lore raw

Hi Luc,

Quoted 25 lines · Luc Van Oostenryck
Quoted 24 lines · Miguel Ojeda

The Compiler Attributes series is an effort to disentangle the include/linux/compiler*.h headers and bring them up to date.

The main idea behind the series is to use feature checking macros (i.e. __has_attribute) instead of compiler version checks (e.g. GCC_VERSION), which are compiler-agnostic (so they can be shared, reducing the size of compiler-specific headers) and version-agnostic.

Other related improvements have been performed in the headers as well, which on top of the use of __has_attribute it has amounted to a significant simplification of these headers (e.g. GCC_VERSION is now only guarding 4 non-attribute macros).

This series should also help the efforts to support compiling the kernel with clang and icc. A fair amount of documentation and comments have also been added, clarified or removed; and the headers are now more readable, which should help kernel developers in general.

The series was triggered due to the move to gcc >= 4.6. In turn, this series has also triggered Sparse to gain the ability to recognize __has_attribute on its own.

You can also fetch it from:

Quoted 7 lines
Quoted 3 lines

https://github.com/ojeda/linux/tree/compiler-attributes-v4

Enjoy!

It's a nice and welcomed cleanup! Everything look good to me, so feel free to add my

Thanks a lot for the review! :-)

Cheers,
Miguel

#20 lore raw
In reply to Miguel Ojeda

Hi all,

Quoted 4 lines · Miguel Ojeda

You can also fetch it from:

https://github.com/ojeda/linux/tree/compiler-attributes-v4

In case it helps, I have also rebased it onto next-20180907 at:

https://github.com/ojeda/linux/tree/compiler-attributes-for-next

The only conflict was the removal of __aligned_largest in f3569fd613f6 ("crypto: shash - Remove VLA usage in unaligned hashing").

(Luc's Reviewed-by is not included in that version)

Cheers,
Miguel

reviewer Reviewed-by on the v4 series
#21 lore raw
In reply to Miguel Ojeda
Quoted 122 lines · Miguel Ojeda

The Compiler Attributes series is an effort to disentangle the include/linux/compiler*.h headers and bring them up to date.

The main idea behind the series is to use feature checking macros (i.e. __has_attribute) instead of compiler version checks (e.g. GCC_VERSION), which are compiler-agnostic (so they can be shared, reducing the size of compiler-specific headers) and version-agnostic.

Other related improvements have been performed in the headers as well, which on top of the use of __has_attribute it has amounted to a significant simplification of these headers (e.g. GCC_VERSION is now only guarding 4 non-attribute macros).

This series should also help the efforts to support compiling the kernel with clang and icc. A fair amount of documentation and comments have also been added, clarified or removed; and the headers are now more readable, which should help kernel developers in general.

The series was triggered due to the move to gcc >= 4.6. In turn, this series has also triggered Sparse to gain the ability to recognize __has_attribute on its own.

You can also fetch it from:

https://github.com/ojeda/linux/tree/compiler-attributes-v4

Enjoy!

Cheers,
Miguel

Cc Jonathan Corbet <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Luc Van Oostenryck <[email protected]>
Cc Eli Friedman <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Joe Perches <[email protected]>
Cc Dominique Martinet <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Linus Torvalds <[email protected]>

v3 -> v4

This time it is an important fix I found while doing randconfigs, plus the documentation sentence that Nick suggested and the MAINTAINERS file.

Compile-tested for a while on (x86_64, gcc-7.3) on a few randconfs.

New:

* Add MAINTAINERS entry for the new file (compiler_attributes.h) so that we try to maintain that one as clean as possible. (if someone else wants to join, feel free!)

Modified:

* Fix inline macro

__always_inline cannot be used in the inline macro, because people marking a function as __always_inline would expand to inline which in turn would expand back to __always_inline (under some configs), which would not be expanded again. Added a comment about this in the inline macro.

Also added another comment about __always_inline in compiler_attributes.h to note that users expect to not have to write inline (as well as the attribute). We cannot simply remove "inline" there (which would solve the problem described above) because using the attribute is not enough for gcc. A function marked as always_inline seems to require to be marked as inline itself so that the attribute is applied:

"warning: always_inline function might not be inlinable [-Wattributes]"

From the gcc docs:

"For functions declared inline, this attribute inlines the function
independent of any restrictions that otherwise apply to inlining."

See https://godbolt.org/z/LpzUPj for an example.

From reviews:

* Add sentence about feature-detection in Docs (Nick)

Miguel Ojeda (13):
Compiler Attributes: remove unused attributes
Compiler Attributes: always use the extra-underscores syntax Compiler Attributes: remove unneeded tests
Compiler Attributes: homogenize __must_be_array Compiler Attributes: naked was fixed in gcc 4.6 Compiler Attributes: naked can be shared
Compiler Attributes: remove unneeded sparse (__CHECKER__) tests Compiler Attributes: add missing SPDX ID in compiler_types.h Compiler Attributes: use feature checks instead of version checks Compiler Attributes: KENTRY used twice the "used" attribute Compiler Attributes: remove uses of __attribute__ from compiler.h Compiler Attributes: add Doc/process/programming-language.rst Compiler Attributes: Add MAINTAINERS entry

 Documentation/process/index.rst               |   1 +
 .../process/programming-language.rst          |  45 ++++
 MAINTAINERS                                   |   5 +
 include/linux/compiler-clang.h                |   5 -
 include/linux/compiler-gcc.h                  |  84 +-----
 include/linux/compiler-intel.h                |   9 -
 include/linux/compiler.h                      |  19 +-
 include/linux/compiler_attributes.h           | 244 ++++++++++++++++++
 include/linux/compiler_types.h                | 105 ++------
 9 files changed, 329 insertions(+), 188 deletions(-)
 create mode 100644 Documentation/process/programming-language.rst
 create mode 100644 include/linux/compiler_attributes.h
Quoted 3 lines

--
2.17.1

For the ones I didn't already review:

Signature · 2 lines
Thanks,
~Nick Desaulniers