Skip to content

Commit e468f5c

Browse files
committed
Merge tag 'compiler-attributes-for-linus-4.20-rc1' of https://github.com/ojeda/linux
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
2 parents baa888d + 1ff2fea commit e468f5c

12 files changed

Lines changed: 348 additions & 191 deletions

File tree

‎Documentation/process/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Below are the essential guides that every developer should read.
2525
code-of-conduct-interpretation
2626
development-process
2727
submitting-patches
28+
programming-language
2829
coding-style
2930
maintainer-pgp-guide
3031
email-clients
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

‎MAINTAINERS‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3737,6 +3737,11 @@ L: platform-driver-x86@vger.kernel.org
37373737
S: Maintained
37383738
F: drivers/platform/x86/compal-laptop.c
37393739

3740+
COMPILER ATTRIBUTES
3741+
M: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
3742+
S: Maintained
3743+
F: include/linux/compiler_attributes.h
3744+
37403745
CONEXANT ACCESSRUNNER USB DRIVER
37413746
L: accessrunner-general@lists.sourceforge.net
37423747
W: http://accessrunner.sourceforge.net/

‎drivers/auxdisplay/panel.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,9 @@ struct logical_input {
155155
int release_data;
156156
} std;
157157
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;
162161
} kbd;
163162
} u;
164163
};

‎fs/ext4/ext4.h‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,6 @@
4545

4646
#include <linux/compiler.h>
4747

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-
5748
/*
5849
* The fourth extended filesystem constants/structures
5950
*/

‎include/linux/compiler-clang.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#define __SANITIZE_ADDRESS__
2222
#endif
2323

24-
#define __no_sanitize_address __attribute__((no_sanitize("address")))
25-
2624
/*
2725
* Not all versions of clang implement the the type-generic versions
2826
* of the builtin overflow checkers. Fortunately, clang implements
@@ -41,6 +39,3 @@
4139
* compilers, like ICC.
4240
*/
4341
#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__)))

‎include/linux/compiler-gcc.h‎

Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,20 @@
6868
*/
6969
#define uninitialized_var(x) x = x
7070

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-
7871
#ifdef RETPOLINE
79-
#define __noretpoline __attribute__((indirect_branch("keep")))
72+
#define __noretpoline __attribute__((__indirect_branch__("keep")))
8073
#endif
8174

8275
#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
8376

84-
#define __optimize(level) __attribute__((__optimize__(level)))
85-
8677
#define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
8778

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)))
9181

92-
#ifdef LATENT_ENTROPY_PLUGIN
82+
#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
9383
#define __latent_entropy __attribute__((latent_entropy))
9484
#endif
95-
#endif /* __CHECKER__ */
9685

9786
/*
9887
* calling noreturn functions, __builtin_unreachable() and __builtin_trap()
@@ -107,10 +96,6 @@
10796
* Mark a position in code as unreachable. This can be used to
10897
* suppress control flow warnings after asm blocks that transfer
10998
* 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.
11499
*/
115100
#define unreachable() \
116101
do { \
@@ -119,9 +104,6 @@
119104
__builtin_unreachable(); \
120105
} while (0)
121106

122-
/* Mark a function definition as prohibited from being cloned. */
123-
#define __noclone __attribute__((__noclone__, __optimize__("no-tracer")))
124-
125107
#if defined(RANDSTRUCT_PLUGIN) && !defined(__CHECKER__)
126108
#define __randomize_layout __attribute__((randomize_layout))
127109
#define __no_randomize_layout __attribute__((no_randomize_layout))
@@ -130,32 +112,6 @@
130112
#define randomized_struct_fields_end } __randomize_layout;
131113
#endif
132114

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-
159115
/*
160116
* GCC 'asm goto' miscompiles certain code sequences:
161117
*
@@ -187,39 +143,22 @@
187143
#define KASAN_ABI_VERSION 3
188144
#endif
189145

190-
#if GCC_VERSION >= 40902
191146
/*
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.
195150
*/
196-
#define __no_sanitize_address __attribute__((no_sanitize_address))
197151
#ifdef CONFIG_KASAN
198152
#define __no_sanitize_address_or_inline \
199153
__no_sanitize_address __maybe_unused notrace
200154
#else
201155
#define __no_sanitize_address_or_inline inline
202156
#endif
203-
#endif
204157

205158
#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))
211159
#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
212160
#endif
213161

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-
223162
/*
224163
* Turn individual warnings and errors on and off locally, depending
225164
* on version.

‎include/linux/compiler-intel.h‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,8 @@
2929
*/
3030
#define OPTIMIZER_HIDE_VAR(var) barrier()
3131

32-
/* Intel ECC compiler doesn't support __builtin_types_compatible_p() */
33-
#define __must_be_array(a) 0
34-
3532
#endif
3633

3734
/* icc has this, but it's called _bswap16 */
3835
#define __HAVE_BUILTIN_BSWAP16__
3936
#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))

‎include/linux/compiler.h‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
2323
#define __branch_check__(x, expect, is_constant) ({ \
2424
long ______r; \
2525
static struct ftrace_likely_data \
26-
__attribute__((__aligned__(4))) \
27-
__attribute__((section("_ftrace_annotated_branch"))) \
26+
__aligned(4) \
27+
__section("_ftrace_annotated_branch") \
2828
______f = { \
2929
.data.func = __func__, \
3030
.data.file = __FILE__, \
@@ -59,8 +59,8 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
5959
({ \
6060
int ______r; \
6161
static struct ftrace_branch_data \
62-
__attribute__((__aligned__(4))) \
63-
__attribute__((section("_ftrace_branch"))) \
62+
__aligned(4) \
63+
__section("_ftrace_branch") \
6464
______f = { \
6565
.func = __func__, \
6666
.file = __FILE__, \
@@ -115,7 +115,10 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
115115
# define ASM_UNREACHABLE
116116
#endif
117117
#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)
119122
#endif
120123

121124
/*
@@ -137,7 +140,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
137140
extern typeof(sym) sym; \
138141
static const unsigned long __kentry_##sym \
139142
__used \
140-
__attribute__((section("___kentry" "+" #sym ), used)) \
143+
__section("___kentry" "+" #sym ) \
141144
= (unsigned long)&sym;
142145
#endif
143146

@@ -278,7 +281,7 @@ unsigned long read_word_at_a_time(const void *addr)
278281
* visible to the compiler.
279282
*/
280283
#define __ADDRESSABLE(sym) \
281-
static void * __attribute__((section(".discard.addressable"), used)) \
284+
static void * __section(".discard.addressable") __used \
282285
__PASTE(__addressable_##sym, __LINE__) = (void *)&sym;
283286

284287
/**
@@ -331,10 +334,6 @@ static inline void *offset_to_ptr(const int *off)
331334
#endif /* __KERNEL__ */
332335
#endif /* __ASSEMBLY__ */
333336

334-
#ifndef __optimize
335-
# define __optimize(level)
336-
#endif
337-
338337
/* Compile time object size, -1 for unknown */
339338
#ifndef __compiletime_object_size
340339
# define __compiletime_object_size(obj) -1
@@ -376,4 +375,7 @@ static inline void *offset_to_ptr(const int *off)
376375
compiletime_assert(__native_word(t), \
377376
"Need native word sized stores/loads for atomicity.")
378377

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+
379381
#endif /* __LINUX_COMPILER_H */

0 commit comments

Comments
 (0)