Pull compiler attribute updates from Miguel Ojeda: "This 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 a few 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. Finally, the __nonstring variable attribute series has been also applied on top; plus two related patches from Nick Desaulniers for unreachable() that came a bit afterwards" * tag 'compiler-attributes-for-linus-4.20-rc1' of https://github.com/ojeda/linux: compiler-gcc: remove comment about gcc 4.5 from unreachable() compiler.h: update definition of unreachable() Compiler Attributes: ext4: remove local __nonstring definition Compiler Attributes: auxdisplay: panel: use __nonstring Compiler Attributes: enable -Wstringop-truncation on W=1 (gcc >= 8) Compiler Attributes: add support for __nonstring (gcc >= 8) Compiler Attributes: add MAINTAINERS entry Compiler Attributes: add Doc/process/programming-language.rst Compiler Attributes: remove uses of __attribute__ from compiler.h Compiler Attributes: KENTRY used twice the "used" attribute Compiler Attributes: use feature checks instead of version checks Compiler Attributes: add missing SPDX ID in compiler_types.h Compiler Attributes: remove unneeded sparse (__CHECKER__) tests Compiler Attributes: homogenize __must_be_array Compiler Attributes: remove unneeded tests Compiler Attributes: always use the extra-underscores syntax Compiler Attributes: remove unused attributes
Unified · Split
| @@ -25,6 +25,7 @@ Below are the essential guides that every developer should read. · 24 unmodified lines | ||
| 25 | 25 | code-of-conduct-interpretation |
| 26 | 26 | development-process |
| 27 | 27 | submitting-patches |
| 28 | + programming-language | |
| 28 | 29 | coding-style |
| 29 | 30 | maintainer-pgp-guide |
| 30 | 31 | email-clients |
| @@ -0,0 +1,45 @@ | ||
| 1 | +.. _programming_language: | |
| 2 | + | |
| 3 | +Programming Language | |
| 4 | +==================== | |
| 5 | + | |
| 6 | +The kernel is written in the C programming language [c-language]_. | |
| 7 | +More precisely, the kernel is typically compiled with ``gcc`` [gcc]_ | |
| 8 | +under ``-std=gnu89`` [gcc-c-dialect-options]_: the GNU dialect of ISO C90 | |
| 9 | +(including some C99 features). | |
| 10 | + | |
| 11 | +This dialect contains many extensions to the language [gnu-extensions]_, | |
| 12 | +and many of them are used within the kernel as a matter of course. | |
| 13 | + | |
| 14 | +There is some support for compiling the kernel with ``clang`` [clang]_ | |
| 15 | +and ``icc`` [icc]_ for several of the architectures, although at the time | |
| 16 | +of writing it is not completed, requiring third-party patches. | |
| 17 | + | |
| 18 | +Attributes | |
| 19 | +---------- | |
| 20 | + | |
| 21 | +One of the common extensions used throughout the kernel are attributes | |
| 22 | +[gcc-attribute-syntax]_. Attributes allow to introduce | |
| 23 | +implementation-defined semantics to language entities (like variables, | |
| 24 | +functions or types) without having to make significant syntactic changes | |
| 25 | +to the language (e.g. adding a new keyword) [n2049]_. | |
| 26 | + | |
| 27 | +In some cases, attributes are optional (i.e. a compiler not supporting them | |
| 28 | +should still produce proper code, even if it is slower or does not perform | |
| 29 | +as many compile-time checks/diagnostics). | |
| 30 | + | |
| 31 | +The kernel defines pseudo-keywords (e.g. ``__pure``) instead of using | |
| 32 | +directly the GNU attribute syntax (e.g. ``__attribute__((__pure__))``) | |
| 33 | +in order to feature detect which ones can be used and/or to shorten the code. | |
| 34 | + | |
| 35 | +Please refer to ``include/linux/compiler_attributes.h`` for more information. | |
| 36 | + | |
| 37 | +.. [c-language] http://www.open-std.org/jtc1/sc22/wg14/www/standards | |
| 38 | +.. [gcc] https://gcc.gnu.org | |
| 39 | +.. [clang] https://clang.llvm.org | |
| 40 | +.. [icc] https://software.intel.com/en-us/c-compilers | |
| 41 | +.. [gcc-c-dialect-options] https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html | |
| 42 | +.. [gnu-extensions] https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html | |
| 43 | +.. [gcc-attribute-syntax] https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html | |
| 44 | +.. [n2049] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2049.pdf | |
| 45 | + | |
| @@ -3737,6 +3737,11 @@ L: platform-driver-x86@vger.kernel.org · 3736 unmodified lines | ||
| 3737 | 3737 | S: Maintained |
| 3738 | 3738 | F: drivers/platform/x86/compal-laptop.c |
| 3739 | 3739 | |
| 3740 | +COMPILER ATTRIBUTES | |
| 3741 | +M: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> | |
| 3742 | +S: Maintained | |
| 3743 | +F: include/linux/compiler_attributes.h | |
| 3744 | + | |
| 3740 | 3745 | CONEXANT ACCESSRUNNER USB DRIVER |
| 3741 | 3746 | L: accessrunner-general@lists.sourceforge.net |
| 3742 | 3747 | W: http://accessrunner.sourceforge.net/ |
| @@ -155,10 +155,9 @@ struct logical_input { · 154 unmodified lines | ||
| 155 | 155 | int release_data; |
| 156 | 156 | } std; |
| 157 | 157 | struct { /* valid when type == INPUT_TYPE_KBD */ |
| 158 | − /* strings can be non null-terminated */ | |
| 159 | − char press_str[sizeof(void *) + sizeof(int)]; | |
| 160 | − char repeat_str[sizeof(void *) + sizeof(int)]; | |
| 161 | − char release_str[sizeof(void *) + sizeof(int)]; | |
| 158 | + char press_str[sizeof(void *) + sizeof(int)] __nonstring; | |
| 159 | + char repeat_str[sizeof(void *) + sizeof(int)] __nonstring; | |
| 160 | + char release_str[sizeof(void *) + sizeof(int)] __nonstring; | |
| 162 | 161 | } kbd; |
| 163 | 162 | } u; |
| 164 | 163 | }; |
| @@ -45,15 +45,6 @@ · 44 unmodified lines | ||
| 45 | 45 | |
| 46 | 46 | #include <linux/compiler.h> |
| 47 | 47 | |
| 48 | −/* Until this gets included into linux/compiler-gcc.h */ | |
| 49 | −#ifndef __nonstring | |
| 50 | −#if defined(GCC_VERSION) && (GCC_VERSION >= 80000) | |
| 51 | −#define __nonstring __attribute__((nonstring)) | |
| 52 | −#else | |
| 53 | −#define __nonstring | |
| 54 | −#endif | |
| 55 | −#endif | |
| 56 | − | |
| 57 | 48 | /* |
| 58 | 49 | * The fourth extended filesystem constants/structures |
| 59 | 50 | */ |
| @@ -21,8 +21,6 @@ · 20 unmodified lines | ||
| 21 | 21 | #define __SANITIZE_ADDRESS__ |
| 22 | 22 | #endif |
| 23 | 23 | |
| 24 | −#define __no_sanitize_address __attribute__((no_sanitize("address"))) | |
| 25 | − | |
| 26 | 24 | /* |
| 27 | 25 | * Not all versions of clang implement the the type-generic versions |
| 28 | 26 | * of the builtin overflow checkers. Fortunately, clang implements |
| @@ -41,6 +39,3 @@ · 12 unmodified lines | ||
| 41 | 39 | * compilers, like ICC. |
| 42 | 40 | */ |
| 43 | 41 | #define barrier() __asm__ __volatile__("" : : : "memory") |
| 44 | −#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) | |
| 45 | −#define __assume_aligned(a, ...) \ | |
| 46 | − __attribute__((__assume_aligned__(a, ## __VA_ARGS__))) | |
| @@ -68,31 +68,20 @@ · 67 unmodified lines | ||
| 68 | 68 | */ |
| 69 | 69 | #define uninitialized_var(x) x = x |
| 70 | 70 | |
| 71 | −#ifdef __CHECKER__ | |
| 72 | −#define __must_be_array(a) 0 | |
| 73 | −#else | |
| 74 | −/* &a[0] degrades to a pointer: a different type from an array */ | |
| 75 | −#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) | |
| 76 | −#endif | |
| 77 | − | |
| 78 | 71 | #ifdef RETPOLINE |
| 79 | −#define __noretpoline __attribute__((indirect_branch("keep"))) | |
| 72 | +#define __noretpoline __attribute__((__indirect_branch__("keep"))) | |
| 80 | 73 | #endif |
| 81 | 74 | |
| 82 | 75 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__) |
| 83 | 76 | |
| 84 | −#define __optimize(level) __attribute__((__optimize__(level))) | |
| 85 | − | |
| 86 | 77 | #define __compiletime_object_size(obj) __builtin_object_size(obj, 0) |
| 87 | 78 | |
| 88 | −#ifndef __CHECKER__ | |
| 89 | −#define __compiletime_warning(message) __attribute__((warning(message))) | |
| 90 | −#define __compiletime_error(message) __attribute__((error(message))) | |
| 79 | +#define __compiletime_warning(message) __attribute__((__warning__(message))) | |
| 80 | +#define __compiletime_error(message) __attribute__((__error__(message))) | |
| 91 | 81 | |
| 92 | −#ifdef LATENT_ENTROPY_PLUGIN | |
| 82 | +#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__) | |
| 93 | 83 | #define __latent_entropy __attribute__((latent_entropy)) |
| 94 | 84 | #endif |
| 95 | −#endif /* __CHECKER__ */ | |
| 96 | 85 | |
| 97 | 86 | /* |
| 98 | 87 | * calling noreturn functions, __builtin_unreachable() and __builtin_trap() |
| @@ -107,10 +96,6 @@ · 8 unmodified lines | ||
| 107 | 96 | * Mark a position in code as unreachable. This can be used to |
| 108 | 97 | * suppress control flow warnings after asm blocks that transfer |
| 109 | 98 | * control elsewhere. |
| 110 | − * | |
| 111 | − * Early snapshots of gcc 4.5 don't support this and we can't detect | |
| 112 | − * this in the preprocessor, but we can live with this because they're | |
| 113 | − * unreleased. Really, we need to have autoconf for the kernel. | |
| 114 | 99 | */ |
| 115 | 100 | #define unreachable() \ |
| 116 | 101 | do { \ |
| @@ -119,9 +104,6 @@ · 2 unmodified lines | ||
| 119 | 104 | __builtin_unreachable(); \ |
| 120 | 105 | } while (0) |
| 121 | 106 | |
| 122 | −/* Mark a function definition as prohibited from being cloned. */ | |
| 123 | −#define __noclone __attribute__((__noclone__, __optimize__("no-tracer"))) | |
| 124 | − | |
| 125 | 107 | #if defined(RANDSTRUCT_PLUGIN) && !defined(__CHECKER__) |
| 126 | 108 | #define __randomize_layout __attribute__((randomize_layout)) |
| 127 | 109 | #define __no_randomize_layout __attribute__((no_randomize_layout)) |
| @@ -130,32 +112,6 @@ · 2 unmodified lines | ||
| 130 | 112 | #define randomized_struct_fields_end } __randomize_layout; |
| 131 | 113 | #endif |
| 132 | 114 | |
| 133 | −/* | |
| 134 | − * When used with Link Time Optimization, gcc can optimize away C functions or | |
| 135 | − * variables which are referenced only from assembly code. __visible tells the | |
| 136 | − * optimizer that something else uses this function or variable, thus preventing | |
| 137 | − * this. | |
| 138 | − */ | |
| 139 | −#define __visible __attribute__((externally_visible)) | |
| 140 | − | |
| 141 | −/* gcc version specific checks */ | |
| 142 | − | |
| 143 | −#if GCC_VERSION >= 40900 && !defined(__CHECKER__) | |
| 144 | −/* | |
| 145 | − * __assume_aligned(n, k): Tell the optimizer that the returned | |
| 146 | − * pointer can be assumed to be k modulo n. The second argument is | |
| 147 | − * optional (default 0), so we use a variadic macro to make the | |
| 148 | − * shorthand. | |
| 149 | − * | |
| 150 | − * Beware: Do not apply this to functions which may return | |
| 151 | − * ERR_PTRs. Also, it is probably unwise to apply it to functions | |
| 152 | − * returning extra information in the low bits (but in that case the | |
| 153 | − * compiler should see some alignment anyway, when the return value is | |
| 154 | − * massaged by 'flags = ptr & 3; ptr &= ~3;'). | |
| 155 | − */ | |
| 156 | −#define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__))) | |
| 157 | −#endif | |
| 158 | − | |
| 159 | 115 | /* |
| 160 | 116 | * GCC 'asm goto' miscompiles certain code sequences: |
| 161 | 117 | * |
| @@ -187,39 +143,22 @@ · 25 unmodified lines | ||
| 187 | 143 | #define KASAN_ABI_VERSION 3 |
| 188 | 144 | #endif |
| 189 | 145 | |
| 190 | −#if GCC_VERSION >= 40902 | |
| 191 | 146 | /* |
| 192 | − * Tell the compiler that address safety instrumentation (KASAN) | |
| 193 | − * should not be applied to that function. | |
| 194 | − * Conflicts with inlining: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368 | |
| 147 | + * Because __no_sanitize_address conflicts with inlining: | |
| 148 | + * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368 | |
| 149 | + * we do one or the other. | |
| 195 | 150 | */ |
| 196 | −#define __no_sanitize_address __attribute__((no_sanitize_address)) | |
| 197 | 151 | #ifdef CONFIG_KASAN |
| 198 | 152 | #define __no_sanitize_address_or_inline \ |
| 199 | 153 | __no_sanitize_address __maybe_unused notrace |
| 200 | 154 | #else |
| 201 | 155 | #define __no_sanitize_address_or_inline inline |
| 202 | 156 | #endif |
| 203 | −#endif | |
| 204 | 157 | |
| 205 | 158 | #if GCC_VERSION >= 50100 |
| 206 | −/* | |
| 207 | − * Mark structures as requiring designated initializers. | |
| 208 | − * https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html | |
| 209 | − */ | |
| 210 | −#define __designated_init __attribute__((designated_init)) | |
| 211 | 159 | #define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1 |
| 212 | 160 | #endif |
| 213 | 161 | |
| 214 | −#if !defined(__noclone) | |
| 215 | −#define __noclone /* not needed */ | |
| 216 | −#endif | |
| 217 | − | |
| 218 | −#if !defined(__no_sanitize_address) | |
| 219 | −#define __no_sanitize_address | |
| 220 | −#define __no_sanitize_address_or_inline inline | |
| 221 | −#endif | |
| 222 | − | |
| 223 | 162 | /* |
| 224 | 163 | * Turn individual warnings and errors on and off locally, depending |
| 225 | 164 | * on version. |
| @@ -29,17 +29,8 @@ · 28 unmodified lines | ||
| 29 | 29 | */ |
| 30 | 30 | #define OPTIMIZER_HIDE_VAR(var) barrier() |
| 31 | 31 | |
| 32 | −/* Intel ECC compiler doesn't support __builtin_types_compatible_p() */ | |
| 33 | −#define __must_be_array(a) 0 | |
| 34 | − | |
| 35 | 32 | #endif |
| 36 | 33 | |
| 37 | 34 | /* icc has this, but it's called _bswap16 */ |
| 38 | 35 | #define __HAVE_BUILTIN_BSWAP16__ |
| 39 | 36 | #define __builtin_bswap16 _bswap16 |
| 40 | − | |
| 41 | −/* The following are for compatibility with GCC, from compiler-gcc.h, | |
| 42 | − * and may be redefined here because they should not be shared with other | |
| 43 | − * compilers, like clang. | |
| 44 | − */ | |
| 45 | −#define __visible __attribute__((externally_visible)) | |
| @@ -23,8 +23,8 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, · 22 unmodified lines | ||
| 23 | 23 | #define __branch_check__(x, expect, is_constant) ({ \ |
| 24 | 24 | long ______r; \ |
| 25 | 25 | static struct ftrace_likely_data \ |
| 26 | − __attribute__((__aligned__(4))) \ | |
| 27 | − __attribute__((section("_ftrace_annotated_branch"))) \ | |
| 26 | + __aligned(4) \ | |
| 27 | + __section("_ftrace_annotated_branch") \ | |
| 28 | 28 | ______f = { \ |
| 29 | 29 | .data.func = __func__, \ |
| 30 | 30 | .data.file = __FILE__, \ |
| @@ -59,8 +59,8 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, · 28 unmodified lines | ||
| 59 | 59 | ({ \ |
| 60 | 60 | int ______r; \ |
| 61 | 61 | static struct ftrace_branch_data \ |
| 62 | − __attribute__((__aligned__(4))) \ | |
| 63 | − __attribute__((section("_ftrace_branch"))) \ | |
| 62 | + __aligned(4) \ | |
| 63 | + __section("_ftrace_branch") \ | |
| 64 | 64 | ______f = { \ |
| 65 | 65 | .func = __func__, \ |
| 66 | 66 | .file = __FILE__, \ |
| @@ -115,7 +115,10 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, · 48 unmodified lines | ||
| 115 | 115 | # define ASM_UNREACHABLE |
| 116 | 116 | #endif |
| 117 | 117 | #ifndef unreachable |
| 118 | −# define unreachable() do { annotate_reachable(); do { } while (1); } while (0) | |
| 118 | +# define unreachable() do { \ | |
| 119 | + annotate_unreachable(); \ | |
| 120 | + __builtin_unreachable(); \ | |
| 121 | +} while (0) | |
| 119 | 122 | #endif |
| 120 | 123 | |
| 121 | 124 | /* |
| @@ -137,7 +140,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, · 15 unmodified lines | ||
| 137 | 140 | extern typeof(sym) sym; \ |
| 138 | 141 | static const unsigned long __kentry_##sym \ |
| 139 | 142 | __used \ |
| 140 | − __attribute__((section("___kentry" "+" #sym ), used)) \ | |
| 143 | + __section("___kentry" "+" #sym ) \ | |
| 141 | 144 | = (unsigned long)&sym; |
| 142 | 145 | #endif |
| 143 | 146 | |
| @@ -278,7 +281,7 @@ unsigned long read_word_at_a_time(const void *addr) · 134 unmodified lines | ||
| 278 | 281 | * visible to the compiler. |
| 279 | 282 | */ |
| 280 | 283 | #define __ADDRESSABLE(sym) \ |
| 281 | − static void * __attribute__((section(".discard.addressable"), used)) \ | |
| 284 | + static void * __section(".discard.addressable") __used \ | |
| 282 | 285 | __PASTE(__addressable_##sym, __LINE__) = (void *)&sym; |
| 283 | 286 | |
| 284 | 287 | /** |
| @@ -331,10 +334,6 @@ static inline void *offset_to_ptr(const int *off) · 46 unmodified lines | ||
| 331 | 334 | #endif /* __KERNEL__ */ |
| 332 | 335 | #endif /* __ASSEMBLY__ */ |
| 333 | 336 | |
| 334 | −#ifndef __optimize | |
| 335 | −# define __optimize(level) | |
| 336 | −#endif | |
| 337 | − | |
| 338 | 337 | /* Compile time object size, -1 for unknown */ |
| 339 | 338 | #ifndef __compiletime_object_size |
| 340 | 339 | # define __compiletime_object_size(obj) -1 |
| @@ -376,4 +375,7 @@ static inline void *offset_to_ptr(const int *off) · 35 unmodified lines | ||
| 376 | 375 | compiletime_assert(__native_word(t), \ |
| 377 | 376 | "Need native word sized stores/loads for atomicity.") |
| 378 | 377 | |
| 378 | +/* &a[0] degrades to a pointer: a different type from an array */ | |
| 379 | +#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) | |
| 380 | + | |
| 379 | 381 | #endif /* __LINUX_COMPILER_H */ |
| @@ -0,0 +1,258 @@ | ||
| 1 | +/* SPDX-License-Identifier: GPL-2.0 */ | |
| 2 | +#ifndef __LINUX_COMPILER_ATTRIBUTES_H | |
| 3 | +#define __LINUX_COMPILER_ATTRIBUTES_H | |
| 4 | + | |
| 5 | +/* | |
| 6 | + * The attributes in this file are unconditionally defined and they directly | |
| 7 | + * map to compiler attribute(s) -- except those that are optional. | |
| 8 | + * | |
| 9 | + * Any other "attributes" (i.e. those that depend on a configuration option, | |
| 10 | + * on a compiler, on an architecture, on plugins, on other attributes...) | |
| 11 | + * should be defined elsewhere (e.g. compiler_types.h or compiler-*.h). | |
| 12 | + * | |
| 13 | + * This file is meant to be sorted (by actual attribute name, | |
| 14 | + * not by #define identifier). Use the __attribute__((__name__)) syntax | |
| 15 | + * (i.e. with underscores) to avoid future collisions with other macros. | |
| 16 | + * If an attribute is optional, state the reason in the comment. | |
| 17 | + */ | |
| 18 | + | |
| 19 | +/* | |
| 20 | + * To check for optional attributes, we use __has_attribute, which is supported | |
| 21 | + * on gcc >= 5, clang >= 2.9 and icc >= 17. In the meantime, to support | |
| 22 | + * 4.6 <= gcc < 5, we implement __has_attribute by hand. | |
| 23 | + * | |
| 24 | + * sparse does not support __has_attribute (yet) and defines __GNUC_MINOR__ | |
| 25 | + * depending on the compiler used to build it; however, these attributes have | |
| 26 | + * no semantic effects for sparse, so it does not matter. Also note that, | |
| 27 | + * in order to avoid sparse's warnings, even the unsupported ones must be | |
| 28 | + * defined to 0. | |
| 29 | + */ | |
| 30 | +#ifndef __has_attribute | |
| 31 | +# define __has_attribute(x) __GCC4_has_attribute_##x | |
| 32 | +# define __GCC4_has_attribute___assume_aligned__ (__GNUC_MINOR__ >= 9) | |
| 33 | +# define __GCC4_has_attribute___designated_init__ 0 | |
| 34 | +# define __GCC4_has_attribute___externally_visible__ 1 | |
| 35 | +# define __GCC4_has_attribute___noclone__ 1 | |
| 36 | +# define __GCC4_has_attribute___optimize__ 1 | |
| 37 | +# define __GCC4_has_attribute___nonstring__ 0 | |
| 38 | +# define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8) | |
| 39 | +#endif | |
| 40 | + | |
| 41 | +/* | |
| 42 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute | |
| 43 | + */ | |
| 44 | +#define __alias(symbol) __attribute__((__alias__(#symbol))) | |
| 45 | + | |
| 46 | +/* | |
| 47 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-aligned-function-attribute | |
| 48 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-aligned-type-attribute | |
| 49 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-aligned-variable-attribute | |
| 50 | + */ | |
| 51 | +#define __aligned(x) __attribute__((__aligned__(x))) | |
| 52 | +#define __aligned_largest __attribute__((__aligned__)) | |
| 53 | + | |
| 54 | +/* | |
| 55 | + * Note: users of __always_inline currently do not write "inline" themselves, | |
| 56 | + * which seems to be required by gcc to apply the attribute according | |
| 57 | + * to its docs (and also "warning: always_inline function might not be | |
| 58 | + * inlinable [-Wattributes]" is emitted). | |
| 59 | + * | |
| 60 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-always_005finline-function-attribute | |
| 61 | + * clang: mentioned | |
| 62 | + */ | |
| 63 | +#define __always_inline inline __attribute__((__always_inline__)) | |
| 64 | + | |
| 65 | +/* | |
| 66 | + * The second argument is optional (default 0), so we use a variadic macro | |
| 67 | + * to make the shorthand. | |
| 68 | + * | |
| 69 | + * Beware: Do not apply this to functions which may return | |
| 70 | + * ERR_PTRs. Also, it is probably unwise to apply it to functions | |
| 71 | + * returning extra information in the low bits (but in that case the | |
| 72 | + * compiler should see some alignment anyway, when the return value is | |
| 73 | + * massaged by 'flags = ptr & 3; ptr &= ~3;'). | |
| 74 | + * | |
| 75 | + * Optional: only supported since gcc >= 4.9 | |
| 76 | + * Optional: not supported by icc | |
| 77 | + * | |
| 78 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-assume_005faligned-function-attribute | |
| 79 | + * clang: https://clang.llvm.org/docs/AttributeReference.html#assume-aligned | |
| 80 | + */ | |
| 81 | +#if __has_attribute(__assume_aligned__) | |
| 82 | +# define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__))) | |
| 83 | +#else | |
| 84 | +# define __assume_aligned(a, ...) | |
| 85 | +#endif | |
| 86 | + | |
| 87 | +/* | |
| 88 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-cold-function-attribute | |
| 89 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute | |
| 90 | + */ | |
| 91 | +#define __cold __attribute__((__cold__)) | |
| 92 | + | |
| 93 | +/* | |
| 94 | + * Note the long name. | |
| 95 | + * | |
| 96 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute | |
| 97 | + */ | |
| 98 | +#define __attribute_const__ __attribute__((__const__)) | |
| 99 | + | |
| 100 | +/* | |
| 101 | + * Don't. Just don't. See commit 771c035372a0 ("deprecate the '__deprecated' | |
| 102 | + * attribute warnings entirely and for good") for more information. | |
| 103 | + * | |
| 104 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute | |
| 105 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-deprecated-type-attribute | |
| 106 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-deprecated-variable-attribute | |
| 107 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html#index-deprecated-enumerator-attribute | |
| 108 | + * clang: https://clang.llvm.org/docs/AttributeReference.html#deprecated | |
| 109 | + */ | |
| 110 | +#define __deprecated | |
| 111 | + | |
| 112 | +/* | |
| 113 | + * Optional: only supported since gcc >= 5.1 | |
| 114 | + * Optional: not supported by clang | |
| 115 | + * Optional: not supported by icc | |
| 116 | + * | |
| 117 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-designated_005finit-type-attribute | |
| 118 | + */ | |
| 119 | +#if __has_attribute(__designated_init__) | |
| 120 | +# define __designated_init __attribute__((__designated_init__)) | |
| 121 | +#else | |
| 122 | +# define __designated_init | |
| 123 | +#endif | |
| 124 | + | |
| 125 | +/* | |
| 126 | + * Optional: not supported by clang | |
| 127 | + * | |
| 128 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-externally_005fvisible-function-attribute | |
| 129 | + */ | |
| 130 | +#if __has_attribute(__externally_visible__) | |
| 131 | +# define __visible __attribute__((__externally_visible__)) | |
| 132 | +#else | |
| 133 | +# define __visible | |
| 134 | +#endif | |
| 135 | + | |
| 136 | +/* | |
| 137 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-format-function-attribute | |
| 138 | + * clang: https://clang.llvm.org/docs/AttributeReference.html#format | |
| 139 | + */ | |
| 140 | +#define __printf(a, b) __attribute__((__format__(printf, a, b))) | |
| 141 | +#define __scanf(a, b) __attribute__((__format__(scanf, a, b))) | |
| 142 | + | |
| 143 | +/* | |
| 144 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-gnu_005finline-function-attribute | |
| 145 | + * clang: https://clang.llvm.org/docs/AttributeReference.html#gnu-inline | |
| 146 | + */ | |
| 147 | +#define __gnu_inline __attribute__((__gnu_inline__)) | |
| 148 | + | |
| 149 | +/* | |
| 150 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute | |
| 151 | + */ | |
| 152 | +#define __malloc __attribute__((__malloc__)) | |
| 153 | + | |
| 154 | +/* | |
| 155 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-mode-type-attribute | |
| 156 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-mode-variable-attribute | |
| 157 | + */ | |
| 158 | +#define __mode(x) __attribute__((__mode__(x))) | |
| 159 | + | |
| 160 | +/* | |
| 161 | + * Optional: not supported by clang | |
| 162 | + * Note: icc does not recognize gcc's no-tracer | |
| 163 | + * | |
| 164 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noclone-function-attribute | |
| 165 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-optimize-function-attribute | |
| 166 | + */ | |
| 167 | +#if __has_attribute(__noclone__) | |
| 168 | +# if __has_attribute(__optimize__) | |
| 169 | +# define __noclone __attribute__((__noclone__, __optimize__("no-tracer"))) | |
| 170 | +# else | |
| 171 | +# define __noclone __attribute__((__noclone__)) | |
| 172 | +# endif | |
| 173 | +#else | |
| 174 | +# define __noclone | |
| 175 | +#endif | |
| 176 | + | |
| 177 | +/* | |
| 178 | + * Note the missing underscores. | |
| 179 | + * | |
| 180 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute | |
| 181 | + * clang: mentioned | |
| 182 | + */ | |
| 183 | +#define noinline __attribute__((__noinline__)) | |
| 184 | + | |
| 185 | +/* | |
| 186 | + * Optional: only supported since gcc >= 8 | |
| 187 | + * Optional: not supported by clang | |
| 188 | + * Optional: not supported by icc | |
| 189 | + * | |
| 190 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute | |
| 191 | + */ | |
| 192 | +#if __has_attribute(__nonstring__) | |
| 193 | +# define __nonstring __attribute__((__nonstring__)) | |
| 194 | +#else | |
| 195 | +# define __nonstring | |
| 196 | +#endif | |
| 197 | + | |
| 198 | +/* | |
| 199 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute | |
| 200 | + * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn | |
| 201 | + * clang: https://clang.llvm.org/docs/AttributeReference.html#id1 | |
| 202 | + */ | |
| 203 | +#define __noreturn __attribute__((__noreturn__)) | |
| 204 | + | |
| 205 | +/* | |
| 206 | + * Optional: only supported since gcc >= 4.8 | |
| 207 | + * Optional: not supported by icc | |
| 208 | + * | |
| 209 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fsanitize_005faddress-function-attribute | |
| 210 | + * clang: https://clang.llvm.org/docs/AttributeReference.html#no-sanitize-address-no-address-safety-analysis | |
| 211 | + */ | |
| 212 | +#if __has_attribute(__no_sanitize_address__) | |
| 213 | +# define __no_sanitize_address __attribute__((__no_sanitize_address__)) | |
| 214 | +#else | |
| 215 | +# define __no_sanitize_address | |
| 216 | +#endif | |
| 217 | + | |
| 218 | +/* | |
| 219 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-packed-type-attribute | |
| 220 | + * clang: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute | |
| 221 | + */ | |
| 222 | +#define __packed __attribute__((__packed__)) | |
| 223 | + | |
| 224 | +/* | |
| 225 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute | |
| 226 | + */ | |
| 227 | +#define __pure __attribute__((__pure__)) | |
| 228 | + | |
| 229 | +/* | |
| 230 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-section-function-attribute | |
| 231 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-section-variable-attribute | |
| 232 | + * clang: https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate | |
| 233 | + */ | |
| 234 | +#define __section(S) __attribute__((__section__(#S))) | |
| 235 | + | |
| 236 | +/* | |
| 237 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute | |
| 238 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute | |
| 239 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-unused-variable-attribute | |
| 240 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-unused-label-attribute | |
| 241 | + * clang: https://clang.llvm.org/docs/AttributeReference.html#maybe-unused-unused | |
| 242 | + */ | |
| 243 | +#define __always_unused __attribute__((__unused__)) | |
| 244 | +#define __maybe_unused __attribute__((__unused__)) | |
| 245 | + | |
| 246 | +/* | |
| 247 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-used-function-attribute | |
| 248 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-used-variable-attribute | |
| 249 | + */ | |
| 250 | +#define __used __attribute__((__used__)) | |
| 251 | + | |
| 252 | +/* | |
| 253 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute | |
| 254 | + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute | |
| 255 | + */ | |
| 256 | +#define __weak __attribute__((__weak__)) | |
| 257 | + | |
| 258 | +#endif /* __LINUX_COMPILER_ATTRIBUTES_H */ | |
| @@ -1,3 +1,4 @@ | ||
| 1 | +/* SPDX-License-Identifier: GPL-2.0 */ | |
| 1 | 2 | #ifndef __LINUX_COMPILER_TYPES_H |
| 2 | 3 | #define __LINUX_COMPILER_TYPES_H |
| 3 | 4 | |
| @@ -54,6 +55,9 @@ extern void __chk_io_ptr(const volatile void __iomem *); · 50 unmodified lines | ||
| 54 | 55 | |
| 55 | 56 | #ifdef __KERNEL__ |
| 56 | 57 | |
| 58 | +/* Attributes */ | |
| 59 | +#include <linux/compiler_attributes.h> | |
| 60 | + | |
| 57 | 61 | /* Compiler specific macros. */ |
| 58 | 62 | #ifdef __clang__ |
| 59 | 63 | #include <linux/compiler-clang.h> |
| @@ -78,12 +82,6 @@ extern void __chk_io_ptr(const volatile void __iomem *); · 18 unmodified lines | ||
| 78 | 82 | #include <asm/compiler.h> |
| 79 | 83 | #endif |
| 80 | 84 | |
| 81 | −/* | |
| 82 | − * Generic compiler-independent macros required for kernel | |
| 83 | − * build go below this comment. Actual compiler/compiler version | |
| 84 | − * specific implementations come from the above header files | |
| 85 | − */ | |
| 86 | − | |
| 87 | 85 | struct ftrace_branch_data { |
| 88 | 86 | const char *func; |
| 89 | 87 | const char *file; |
| @@ -106,10 +104,6 @@ struct ftrace_likely_data { · 16 unmodified lines | ||
| 106 | 104 | unsigned long constant; |
| 107 | 105 | }; |
| 108 | 106 | |
| 109 | −/* Don't. Just don't. */ | |
| 110 | −#define __deprecated | |
| 111 | −#define __deprecated_for_modules | |
| 112 | − | |
| 113 | 107 | #endif /* __KERNEL__ */ |
| 114 | 108 | |
| 115 | 109 | #endif /* __ASSEMBLY__ */ |
| @@ -119,10 +113,6 @@ struct ftrace_likely_data { · 3 unmodified lines | ||
| 119 | 113 | * compilers. We don't consider that to be an error, so set them to nothing. |
| 120 | 114 | * For example, some of them are for compiler specific plugins. |
| 121 | 115 | */ |
| 122 | −#ifndef __designated_init | |
| 123 | −# define __designated_init | |
| 124 | −#endif | |
| 125 | − | |
| 126 | 116 | #ifndef __latent_entropy |
| 127 | 117 | # define __latent_entropy |
| 128 | 118 | #endif |
| @@ -140,17 +130,6 @@ struct ftrace_likely_data { · 11 unmodified lines | ||
| 140 | 130 | # define randomized_struct_fields_end |
| 141 | 131 | #endif |
| 142 | 132 | |
| 143 | −#ifndef __visible | |
| 144 | −#define __visible | |
| 145 | −#endif | |
| 146 | − | |
| 147 | −/* | |
| 148 | − * Assume alignment of return value. | |
| 149 | − */ | |
| 150 | −#ifndef __assume_aligned | |
| 151 | −#define __assume_aligned(a, ...) | |
| 152 | −#endif | |
| 153 | − | |
| 154 | 133 | /* Are two types/vars the same type (ignoring qualifiers)? */ |
| 155 | 134 | #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) |
| 156 | 135 | |
| @@ -159,14 +138,6 @@ struct ftrace_likely_data { · 2 unmodified lines | ||
| 159 | 138 | (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \ |
| 160 | 139 | sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long)) |
| 161 | 140 | |
| 162 | −#ifndef __attribute_const__ | |
| 163 | −#define __attribute_const__ __attribute__((__const__)) | |
| 164 | −#endif | |
| 165 | − | |
| 166 | −#ifndef __noclone | |
| 167 | −#define __noclone | |
| 168 | −#endif | |
| 169 | − | |
| 170 | 141 | /* Helpers for emitting diagnostics in pragmas. */ |
| 171 | 142 | #ifndef __diag |
| 172 | 143 | #define __diag(string) |
| @@ -186,43 +157,16 @@ struct ftrace_likely_data { · 13 unmodified lines | ||
| 186 | 157 | #define __diag_error(compiler, version, option, comment) \ |
| 187 | 158 | __diag_ ## compiler(version, error, option) |
| 188 | 159 | |
| 189 | −/* | |
| 190 | − * From the GCC manual: | |
| 191 | − * | |
| 192 | − * Many functions have no effects except the return value and their | |
| 193 | − * return value depends only on the parameters and/or global | |
| 194 | − * variables. Such a function can be subject to common subexpression | |
| 195 | − * elimination and loop optimization just as an arithmetic operator | |
| 196 | − * would be. | |
| 197 | − * [...] | |
| 198 | − */ | |
| 199 | −#define __pure __attribute__((pure)) | |
| 200 | −#define __aligned(x) __attribute__((aligned(x))) | |
| 201 | −#define __printf(a, b) __attribute__((format(printf, a, b))) | |
| 202 | −#define __scanf(a, b) __attribute__((format(scanf, a, b))) | |
| 203 | −#define __maybe_unused __attribute__((unused)) | |
| 204 | −#define __always_unused __attribute__((unused)) | |
| 205 | −#define __mode(x) __attribute__((mode(x))) | |
| 206 | −#define __malloc __attribute__((__malloc__)) | |
| 207 | −#define __used __attribute__((__used__)) | |
| 208 | −#define __noreturn __attribute__((noreturn)) | |
| 209 | −#define __packed __attribute__((packed)) | |
| 210 | −#define __weak __attribute__((weak)) | |
| 211 | −#define __alias(symbol) __attribute__((alias(#symbol))) | |
| 212 | −#define __cold __attribute__((cold)) | |
| 213 | −#define __section(S) __attribute__((__section__(#S))) | |
| 214 | − | |
| 215 | − | |
| 216 | 160 | #ifdef CONFIG_ENABLE_MUST_CHECK |
| 217 | −#define __must_check __attribute__((warn_unused_result)) | |
| 161 | +#define __must_check __attribute__((__warn_unused_result__)) | |
| 218 | 162 | #else |
| 219 | 163 | #define __must_check |
| 220 | 164 | #endif |
| 221 | 165 | |
| 222 | −#if defined(CC_USING_HOTPATCH) && !defined(__CHECKER__) | |
| 166 | +#if defined(CC_USING_HOTPATCH) | |
| 223 | 167 | #define notrace __attribute__((hotpatch(0, 0))) |
| 224 | 168 | #else |
| 225 | −#define notrace __attribute__((no_instrument_function)) | |
| 169 | +#define notrace __attribute__((__no_instrument_function__)) | |
| 226 | 170 | #endif |
| 227 | 171 | |
| 228 | 172 | /* |
| @@ -231,22 +175,10 @@ struct ftrace_likely_data { · 2 unmodified lines | ||
| 231 | 175 | * stack and frame pointer being set up and there is no chance to |
| 232 | 176 | * restore the lr register to the value before mcount was called. |
| 233 | 177 | */ |
| 234 | −#define __naked __attribute__((naked)) notrace | |
| 178 | +#define __naked __attribute__((__naked__)) notrace | |
| 235 | 179 | |
| 236 | 180 | #define __compiler_offsetof(a, b) __builtin_offsetof(a, b) |
| 237 | 181 | |
| 238 | −/* | |
| 239 | − * Feature detection for gnu_inline (gnu89 extern inline semantics). Either | |
| 240 | − * __GNUC_STDC_INLINE__ is defined (not using gnu89 extern inline semantics, | |
| 241 | − * and we opt in to the gnu89 semantics), or __GNUC_STDC_INLINE__ is not | |
| 242 | − * defined so the gnu89 semantics are the default. | |
| 243 | − */ | |
| 244 | −#ifdef __GNUC_STDC_INLINE__ | |
| 245 | −# define __gnu_inline __attribute__((gnu_inline)) | |
| 246 | −#else | |
| 247 | −# define __gnu_inline | |
| 248 | −#endif | |
| 249 | − | |
| 250 | 182 | /* |
| 251 | 183 | * Force always-inline if the user requests it so via the .config. |
| 252 | 184 | * GCC does not warn about unused static inline functions for |
| @@ -258,22 +190,20 @@ struct ftrace_likely_data { · 5 unmodified lines | ||
| 258 | 190 | * semantics rather than c99. This prevents multiple symbol definition errors |
| 259 | 191 | * of extern inline functions at link time. |
| 260 | 192 | * A lot of inline functions can cause havoc with function tracing. |
| 193 | + * Do not use __always_inline here, since currently it expands to inline again | |
| 194 | + * (which would break users of __always_inline). | |
| 261 | 195 | */ |
| 262 | 196 | #if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) || \ |
| 263 | 197 | !defined(CONFIG_OPTIMIZE_INLINING) |
| 264 | −#define inline \ | |
| 265 | − inline __attribute__((always_inline, unused)) notrace __gnu_inline | |
| 198 | +#define inline inline __attribute__((__always_inline__)) __gnu_inline \ | |
| 199 | + __maybe_unused notrace | |
| 266 | 200 | #else |
| 267 | −#define inline inline __attribute__((unused)) notrace __gnu_inline | |
| 201 | +#define inline inline __gnu_inline \ | |
| 202 | + __maybe_unused notrace | |
| 268 | 203 | #endif |
| 269 | 204 | |
| 270 | 205 | #define __inline__ inline |
| 271 | −#define __inline inline | |
| 272 | −#define noinline __attribute__((noinline)) | |
| 273 | − | |
| 274 | −#ifndef __always_inline | |
| 275 | −#define __always_inline inline __attribute__((always_inline)) | |
| 276 | −#endif | |
| 206 | +#define __inline inline | |
| 277 | 207 | |
| 278 | 208 | /* |
| 279 | 209 | * Rather then using noinline to prevent stack consumption, use |
| @@ -29,6 +29,7 @@ warning-1 += $(call cc-option, -Wmissing-include-dirs) · 28 unmodified lines | ||
| 29 | 29 | warning-1 += $(call cc-option, -Wunused-but-set-variable) |
| 30 | 30 | warning-1 += $(call cc-option, -Wunused-const-variable) |
| 31 | 31 | warning-1 += $(call cc-option, -Wpacked-not-aligned) |
| 32 | +warning-1 += $(call cc-option, -Wstringop-truncation) | |
| 32 | 33 | warning-1 += $(call cc-disable-warning, missing-field-initializers) |
| 33 | 34 | warning-1 += $(call cc-disable-warning, sign-compare) |
| 34 | 35 | |