Compiler Attributes
Subsystems (9)
v5 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.
Finally, the nonstring variable attribute series has been applied on top of this one.
You can also fetch it from:
https://github.com/ojeda/linux/tree/compiler-attributes-v5
Enjoy!
Cheers,
Miguel
v4 -> v5
* Removed the 2 __naked patches, since Greg just applied them to master; and rebased on top of that. The only change in the range-diff is in "Compiler Attributes: always use the extra-underscores syntax" due to the move of __naked to compiler_types.h.
* Applied the nonstring series on top of this one (last 4 patches). Rationale:
- the nonstring series would anyway be modified again by this one (so it has been rebased after the general cleanup).
- ext4's "local" nonstring was merged
- easier to manage (and easier to see the end result)
The first 2 patches of the last 4 add the warning back at W=1 and add the __nonstring attribute. The other two serve as an example of usage and as a cleanup.
* Cc lists removed from each commit; using a single list in the cover letter; and a new, revised list of Cc (wider audience).
Miguel Ojeda (15):
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: 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
Compiler Attributes: add support for __nonstring (gcc >= 8) Compiler Attributes: enable -Wstringop-truncation on W=1 (gcc >= 8) Compiler Attributes: auxdisplay: panel: use __nonstring Compiler Attributes: ext4: remove local __nonstring definition
Documentation/process/index.rst | 1 + .../process/programming-language.rst | 45 +++ MAINTAINERS | 5 + drivers/auxdisplay/panel.c | 7 +- fs/ext4/ext4.h | 9 - include/linux/compiler-clang.h | 5 - include/linux/compiler-gcc.h | 70 +---- include/linux/compiler-intel.h | 9 - include/linux/compiler.h | 19 +- include/linux/compiler_attributes.h | 258 ++++++++++++++++++ include/linux/compiler_types.h | 101 +------ scripts/Makefile.extrawarn | 1 + 12 files changed, 341 insertions(+), 189 deletions(-) create mode 100644 Documentation/process/programming-language.rst create mode 100644 include/linux/compiler_attributes.h
Signature · 1 line
2.17.1
PATCH v5 1/15 Compiler Attributes: remove unused attributes
__optimize and __deprecate_for_modules are unused in the whole kernel tree. Simply drop them.
--- 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
@@ -81,8 +81,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
@@ -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
@@ -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
PATCH v5 2/15 Compiler Attributes: always use the extra-underscores syntax
In reply to Miguel OjedaThe 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.
--- include/linux/compiler-clang.h | 2 +- include/linux/compiler-gcc.h | 12 +++++----- include/linux/compiler-intel.h | 2 +- include/linux/compiler.h | 8 +++---- include/linux/compiler_types.h | 42 +++++++++++++++++----------------- 5 files changed, 33 insertions(+), 33 deletions(-)
include/linux/compiler-clang.h +1 -1
@@ -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 +6 -6
@@ -76,7 +76,7 @@#endif #ifdef RETPOLINE -#define __noretpoline __attribute__((indirect_branch("keep"))) +#define __noretpoline __attribute__((__indirect_branch__("keep")))#endif #define __UNIQUE_ID (prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
@@ -84,8 +84,8 @@#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))
@@ -134,7 +134,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 */
@@ -191,7 +191,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
@@ -199,7 +199,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
@@ -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
@@ -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 +21 -21
85 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v5 3/15 Compiler Attributes: remove unneeded tests
In reply to Miguel OjedaAttributes 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.
--- include/linux/compiler_types.h | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-)
include/linux/compiler_types.h +3 -20
@@ -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
@@ -235,18 +234,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
@@ -271,10 +258,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
PATCH v5 4/15 Compiler Attributes: homogenize __must_be_array
In reply to Miguel OjedaDifferent 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
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH v5 5/15 Compiler Attributes: remove unneeded sparse (__CHECKER__) tests
In reply to Miguel OjedaSparse 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__
--- 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
@@ -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
@@ -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
@@ -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
PATCH v5 6/15 Compiler Attributes: add missing SPDX ID in compiler_types.h
In reply to Miguel Ojeda--- include/linux/compiler_types.h | 1 + 1 file changed, 1 insertion(+)
include/linux/compiler_types.h +1 -0
@@ -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
PATCH v5 7/15 Compiler Attributes: use feature checks instead of version checks
In reply to Miguel OjedaInstead 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.
--- 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
@@ -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
@@ -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
PATCH v5 8/15 Compiler Attributes: KENTRY used twice the "used" attribute
In reply to Miguel Ojeda--- include/linux/compiler.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -1
@@ -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
PATCH v5 9/15 Compiler Attributes: remove uses of __attribute__ from compiler.h
In reply to Miguel Ojeda--- include/linux/compiler.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
include/linux/compiler.h +6 -6
@@ -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
PATCH v5 10/15 Compiler Attributes: add Doc/process/programming-language.rst
In reply to 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
Documentation/process/index.rst +1 -0
@@ -23,6 +23,7 @@ Below are the essential guides that every developer should read. code-of-conduct 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
PATCH v5 11/15 Compiler Attributes: add MAINTAINERS entry
In reply to Miguel Ojeda--- MAINTAINERS | 5 +++++ 1 file changed, 5 insertions(+)
MAINTAINERS +5 -0
@@ -3722,6 +3722,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
PATCH v5 12/15 Compiler Attributes: add support for __nonstring (gcc >= 8)
In reply to Miguel OjedaQuoted 1 line
From the GCC manual:
nonstring
The nonstring variable attribute specifies that an object or member declaration with type array of char, signed char, or unsigned char, or pointer to such a type is intended to store character arrays that do not necessarily contain a terminating NUL. This is useful in detecting uses of such arrays or pointers with functions that expect NUL-terminated strings, and to avoid warnings when such an array or pointer is used as an argument to a bounded string manipulation function such as strncpy.
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
This attribute can be used for documentation purposes (i.e. replacing comments), but it is most helpful when the following warnings are enabled:
-Wstringop-overflow
Warn for calls to string manipulation functions such as memcpy and strcpy that are determined to overflow the destination buffer.
[...]
-Wstringop-truncation
Warn for calls to bounded string manipulation functions such as strncat, strncpy, and stpncpy that may either truncate the copied string or leave the destination unchanged.
[...]
In situations where a character array is intended to store a sequence of bytes with no terminating NUL such an array may be annotated with attribute nonstring to avoid this warning. Such arrays, however, are not suitable arguments to functions that expect NUL-terminated strings. To help detect accidental misuses of such arrays GCC issues warnings unless it can prove that the use is safe.
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
--- include/linux/compiler_attributes.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/linux/compiler_attributes.h +14 -0
@@ -34,6 +34,7 @@# define __GCC4_has_attribute___externally_visible__ 1# define __GCC4_has_attribute___noclone__ 1# define __GCC4_has_attribute___optimize__ 1 +# define __GCC4_has_attribute___nonstring__ 0# define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)#endif
@@ -181,6 +182,19 @@*/ #define noinline __attribute__((__noinline__)) +/* +* Optional: only supported since gcc >= 8 +* Optional: not supported by clang +* Optional: not supported by icc +* +* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute +*/ +#if __has_attribute (__nonstring__) +# define __nonstring __attribute__((__nonstring__)) +#else +# define __nonstring +#endif +/* * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn
Signature · 1 line
2.17.1
Re: [PATCH v5 12/15] Compiler Attributes: add support for __nonstring (gcc >= 8)
Quoted 80 lines · Miguel Ojeda
From the GCC manual:
nonstring
The nonstring variable attribute specifies that an object or member declaration with type array of char, signed char, or unsigned char, or pointer to such a type is intended to store character arrays that do not necessarily contain a terminating NUL. This is useful in detecting uses of such arrays or pointers with functions that expect NUL-terminated strings, and to avoid warnings when such an array or pointer is used as an argument to a bounded string manipulation function such as strncpy.
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
This attribute can be used for documentation purposes (i.e. replacing comments), but it is most helpful when the following warnings are enabled:
-Wstringop-overflow
Warn for calls to string manipulation functions such as memcpy and strcpy that are determined to overflow the destination buffer.
[...]
-Wstringop-truncation
Warn for calls to bounded string manipulation functions such as strncat, strncpy, and stpncpy that may either truncate the copied string or leave the destination unchanged.
[...]
In situations where a character array is intended to store a sequence of bytes with no terminating NUL such an array may be annotated with attribute nonstring to avoid this warning. Such arrays, however, are not suitable arguments to functions that expect NUL-terminated strings. To help detect accidental misuses of such arrays GCC issues warnings unless it can prove that the use is safe.
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
Signed-off-by Miguel Ojeda <[email protected]>--- include/linux/compiler_attributes.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)include/linux/compiler_attributes.h +14
-1@@ -34,6 +34,7 @@# define __GCC4_has_attribute___externally_visible__ 1# define __GCC4_has_attribute___noclone__ 1# define __GCC4_has_attribute___optimize__ 1 +# define __GCC4_has_attribute___nonstring__ 0# define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)#endif @@ -181,6 +182,19 @@*/ #define noinline __attribute__((__noinline__)) +/* +* Optional: only supported since gcc >= 8 +* Optional: not supported by clang +* Optional: not supported by icc +* +* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute +*/ +#if __has_attribute (__nonstring__) +# define __nonstring __attribute__((__nonstring__)) +#else +# define __nonstring +#endif +/* * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn --
Quoted 2 lines
2.17.1
Signature · 2 lines
Thanks, ~Nick Desaulniers
Re: [PATCH v5 12/15] Compiler Attributes: add support for __nonstring (gcc >= 8)
In reply to Miguel OjedaQuoted 39 lines · Miguel Ojeda
From the GCC manual:
nonstring
The nonstring variable attribute specifies that an object or member declaration with type array of char, signed char, or unsigned char, or pointer to such a type is intended to store character arrays that do not necessarily contain a terminating NUL. This is useful in detecting uses of such arrays or pointers with functions that expect NUL-terminated strings, and to avoid warnings when such an array or pointer is used as an argument to a bounded string manipulation function such as strncpy.
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
This attribute can be used for documentation purposes (i.e. replacing comments), but it is most helpful when the following warnings are enabled:
-Wstringop-overflow
Warn for calls to string manipulation functions such as memcpy and strcpy that are determined to overflow the destination buffer.
[...]
-Wstringop-truncation
Warn for calls to bounded string manipulation functions such as strncat, strncpy, and stpncpy that may either truncate the copied string or leave the destination unchanged.
[...]
In situations where a character array is intended to store a sequence of bytes with no terminating NUL such an array may be annotated with attribute nonstring to avoid this warning. Such arrays, however, are not suitable arguments to functions that expect NUL-terminated strings. To help detect accidental misuses of such arrays GCC issues warnings unless it can prove that the use is safe.
Quoted 3 lines
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
Signed-off-by Miguel Ojeda <[email protected]>
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Re: [PATCH v5 12/15] Compiler Attributes: add support for __nonstring (gcc >= 8)
PATCH v5 13/15 Compiler Attributes: enable -Wstringop-truncation on W=1 (gcc >= 8)
In reply to Miguel OjedaCommit 217c3e019675 ("disable stringop truncation warnings for now") disabled -Wstringop-truncation since it was too noisy.
Having __nonstring available allows us to let GCC know that a string is not meant to be NUL-terminated, which helps suppressing some -Wstringop-truncation warnings.
Note that using __nonstring actually triggers other warnings (-Wstringop-overflow, which is on by default) which may be real problems. Therefore, cleaning up -Wstringop-truncation warnings also buys us the ability to uncover further potential problems.
To encourage the use of __nonstring, we put the warning back at W=1. In the future, if we end up with a fairly warning-free tree, we might want to enable it by default.
--- scripts/Makefile.extrawarn | 1 + 1 file changed, 1 insertion(+)
scripts/Makefile.extrawarn +1 -0
@@ -29,6 +29,7 @@ warning-1 += $(call cc-option, -Wmissing-include-dirs) warning-1 += $(call cc-option, -Wunused-but-set-variable) warning-1 += $(call cc-option, -Wunused-const-variable) warning-1 += $(call cc-option, -Wpacked-not-aligned) +warning-1 += $(call cc-option, -Wstringop-truncation) warning-1 += $(call cc-disable-warning, missing-field-initializers) warning-1 += $(call cc-disable-warning, sign-compare)
Signature · 1 line
2.17.1
Re: [PATCH v5 13/15] Compiler Attributes: enable -Wstringop-truncation on W=1 (gcc >= 8)
Quoted 35 lines · Miguel Ojeda
Commit
217c3e019675("disable stringop truncation warnings for now") disabled -Wstringop-truncation since it was too noisy.Having
__nonstringavailable allows us to let GCC know that a string is not meant to be NUL-terminated, which helps suppressing some -Wstringop-truncation warnings.Note that using
__nonstringactually triggers other warnings (-Wstringop-overflow, which is on by default) which may be real problems. Therefore, cleaning up -Wstringop-truncation warnings also buys us the ability to uncover further potential problems.To encourage the use of
__nonstring, we put the warning back at W=1. In the future, if we end up with a fairly warning-free tree, we might want to enable it by default.Signed-off-by Miguel Ojeda <[email protected]>--- scripts/Makefile.extrawarn | 1 + 1 file changed, 1 insertion(+)scripts/Makefile.extrawarn +1
-1@@ -29,6 +29,7 @@ warning-1 += $(call cc-option, -Wmissing-include-dirs) warning-1 += $(call cc-option, -Wunused-but-set-variable) warning-1 += $(call cc-option, -Wunused-const-variable) warning-1 += $(call cc-option, -Wpacked-not-aligned) +warning-1 += $(call cc-option, -Wstringop-truncation) warning-1 += $(call cc-disable-warning, missing-field-initializers) warning-1 += $(call cc-disable-warning, sign-compare) --
Quoted 2 lines
2.17.1
I was sad to see these go in the first place.
Signature · 2 lines
Thanks, ~Nick Desaulniers
Re: [PATCH v5 13/15] Compiler Attributes: enable -Wstringop-truncation on W=1 (gcc >= 8)
In reply to Miguel OjedaQuoted 12 lines · Miguel Ojeda
Commit
217c3e019675("disable stringop truncation warnings for now") disabled -Wstringop-truncation since it was too noisy.Having
__nonstringavailable allows us to let GCC know that a string is not meant to be NUL-terminated, which helps suppressing some -Wstringop-truncation warnings.Note that using
__nonstringactually triggers other warnings (-Wstringop-overflow, which is on by default) which may be real problems. Therefore, cleaning up -Wstringop-truncation warnings also buys us the ability to uncover further potential problems.
Quoted 5 lines
To encourage the use of
__nonstring, we put the warning back at W=1. In the future, if we end up with a fairly warning-free tree, we might want to enable it by default.Signed-off-by Miguel Ojeda <[email protected]>
Yessss. :)
-Kees
Quoted 19 lines
--- scripts/Makefile.extrawarn | 1 + 1 file changed, 1 insertion(+)scripts/Makefile.extrawarn +1
-1@@ -29,6 +29,7 @@ warning-1 += $(call cc-option, -Wmissing-include-dirs) warning-1 += $(call cc-option, -Wunused-but-set-variable) warning-1 += $(call cc-option, -Wunused-const-variable) warning-1 += $(call cc-option, -Wpacked-not-aligned) +warning-1 += $(call cc-option, -Wstringop-truncation) warning-1 += $(call cc-disable-warning, missing-field-initializers) warning-1 += $(call cc-disable-warning, sign-compare) --2.17.1
Signature · 2 lines
Kees Cook Pixel Security
Re: [PATCH v5 13/15] Compiler Attributes: enable -Wstringop-truncation on W=1 (gcc >= 8)
Quoted 4 lines · Kees Cook
Yessss. :)
Reviewed-by Kees Cook <[email protected]>
Done! Thanks! :)
Cheers,
Miguel
PATCH v5 14/15 Compiler Attributes: auxdisplay: panel: use __nonstring
In reply to Miguel OjedaLet gcc know these arrays are not meant to be NUL-terminated by annotating them with the new __nonstring variable attribute; and remove the comment since it conveys the same information.
--- drivers/auxdisplay/panel.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/auxdisplay/panel.c +3 -4
@@ -155,10 +155,9 @@ struct logical_input {int release_data; } std;struct {/* valid when type == INPUT_TYPE_KBD */ -/* strings can be non null-terminated */ -char press_str[sizeof (void *) +sizeof (int )]; -char repeat_str[sizeof (void *) +sizeof (int )]; -char release_str[sizeof (void *) +sizeof (int )]; + char press_str[sizeof(void *) + sizeof(int)] __nonstring; + char repeat_str[sizeof(void *) + sizeof(int)] __nonstring; + char release_str[sizeof(void *) + sizeof(int)] __nonstring; } kbd; } u; };
Signature · 1 line
2.17.1
Re: [PATCH v5 14/15] Compiler Attributes: auxdisplay: panel: use __nonstring
Quoted 29 lines · Miguel Ojeda
Let gcc know these arrays are not meant to be NUL-terminated by annotating them with the new
__nonstringvariable attribute; and remove the comment since it conveys the same information.Signed-off-by Miguel Ojeda <[email protected]>--- drivers/auxdisplay/panel.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)drivers/auxdisplay/panel.c +3
-5@@ -155,10 +155,9 @@ struct logical_input {int release_data; } std;struct {/* valid when type == INPUT_TYPE_KBD */ -/* strings can be non null-terminated */ -char press_str[sizeof (void *) +sizeof (int )]; -char repeat_str[sizeof (void *) +sizeof (int )]; -char release_str[sizeof (void *) +sizeof (int )]; + char press_str[sizeof(void *) + sizeof(int)] __nonstring; + char repeat_str[sizeof(void *) + sizeof(int)] __nonstring; + char release_str[sizeof(void *) + sizeof(int)] __nonstring; } kbd; } u; }; --
Quoted 2 lines
2.17.1
Signature · 2 lines
Thanks, ~Nick Desaulniers
PATCH v5 15/15 Compiler Attributes: ext4: remove local __nonstring definition
In reply to Miguel OjedaCommit 072ebb3bffe6 ("ext4: add nonstring annotations to ext4.h") introduced a local definition of __nonstring to suppress some false positives in gcc 8's -Wstringop-truncation.
Since now we support __nonstring for everyone, remove it.
--- fs/ext4/ext4.h | 9 --------- 1 file changed, 9 deletions(-)
fs/ext4/ext4.h +0 -9
@@ -45,15 +45,6 @@#include <linux/compiler.h> -/* Until this gets included into linux/compiler-gcc.h */ -#ifndef __nonstring -#if defined(GCC_VERSION ) && (GCC_VERSION >=80000 ) -#define __nonstring __attribute__((nonstring)) -#else -#define __nonstring -#endif -#endif -/* * The fourth extended filesystem constants/structures */
Signature · 1 line
2.17.1
Re: [PATCH v5 15/15] Compiler Attributes: ext4: remove local __nonstring definition
Quoted 6 lines · Miguel Ojeda
Commit
072ebb3bffe6("ext4: add nonstring annotations toext4.h") introduced a local definition of__nonstringto suppress some false positives in gcc 8's -Wstringop-truncation.Since now we support
__nonstringfor everyone, remove it.
... from a patch in the current series (Note to other reviewers).
Quoted 27 lines
Signed-off-by Miguel Ojeda <[email protected]>--- fs/ext4/ext4.h | 9 --------- 1 file changed, 9 deletions(-)fs/ext4/ext4.h +0
-10@@ -45,15 +45,6 @@#include <linux/compiler.h> -/* Until this gets included into linux/compiler-gcc.h */ -#ifndef __nonstring -#if defined(GCC_VERSION ) && (GCC_VERSION >=80000 ) -#define __nonstring __attribute__((nonstring)) -#else -#define __nonstring -#endif -#endif -/* * The fourth extended filesystem constants/structures */ --
Quoted 2 lines
2.17.1
Signature · 2 lines
Thanks, ~Nick Desaulniers
Nick Desaulniers
reviewerQuoted 26 lines · Miguel Ojeda
The Compiler Attributes series is an effort to disentangle the
include/linux/compiler*.hheaders 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_attributeit has amounted to a significant simplification of these headers (e.g.GCC_VERSIONis 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_attributeon its own.Finally, the nonstring variable attribute series has been applied on top of this one.
Quoted 8 lines
You can also fetch it from:
https://github.com/ojeda/linux/tree/compiler-attributes-v5
Enjoy!
Cheers,
Miguel
Thanks for this series. I've reviewed the rest of the patches in the series that didn't already have my reviewed by tags on them. They look good to go to me.
Quoted 99 lines
Cc Andreas Dilger <[email protected]>Cc Masahiro Yamada <[email protected]>Cc Michal Marek <[email protected]>Cc Steven Rostedt <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Cc Olof Johansson <[email protected]>Cc Konstantin Ryabitsev <[email protected]>Cc David S. Miller <[email protected]>Cc Andrey Ryabinin <[email protected]>Cc Kees Cook <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc Paul Lawrence <[email protected]>Cc Sandipan Das <[email protected]>Cc Andrey Konovalov <[email protected]>Cc David Woodhouse <[email protected]>Cc Will Deacon <[email protected]>Cc Philippe Ombredanne <[email protected]>Cc Paul Burton <[email protected]>Cc David Rientjes <[email protected]>Cc Willy Tarreau <[email protected]>Cc Martin Sebor <[email protected]>Cc Christopher Li <[email protected]>Cc Jonathan Corbet <[email protected]>Cc Theodore Ts'o <[email protected]>Cc Geert Uytterhoeven <[email protected]>Cc Rasmus Villemoes <[email protected]>Cc Joe Perches <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Dominique Martinet <[email protected]>Cc Stefan Agner <[email protected]>Cc Luc Van Oostenryck <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Andrew Morton <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Linus Torvalds <[email protected]>v4 -> v5
* Removed the 2
__nakedpatches, since Greg just applied them to master; and rebased on top of that. The only change in the range-diff is in "Compiler Attributes: always use the extra-underscores syntax" due to the move of__nakedtocompiler_types.h.* Applied the nonstring series on top of this one (last 4 patches). Rationale:
- the nonstring series would anyway be modified again by this one (so it has been rebased after the general cleanup).
- ext4's "local" nonstring was merged
- easier to manage (and easier to see the end result)The first 2 patches of the last 4 add the warning back at W=1 and add the
__nonstringattribute. The other two serve as an example of usage and as a cleanup.* Cc lists removed from each commit; using a single list in the cover letter; and a new, revised list of Cc (wider audience).
Miguel Ojeda (15):
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: remove unneeded sparse (__CHECKER__) tests Compiler Attributes: add missing SPDX ID incompiler_types.hCompiler Attributes: use feature checks instead of version checks Compiler Attributes: KENTRY used twice the "used" attribute Compiler Attributes: remove uses of__attribute__fromcompiler.hCompiler Attributes: addDoc/process/programming-language.rstCompiler Attributes: addMAINTAINERSentry
Compiler Attributes: add support for__nonstring(gcc >= 8) Compiler Attributes: enable -Wstringop-truncation on W=1 (gcc >= 8) Compiler Attributes: auxdisplay: panel: use__nonstringCompiler Attributes: ext4: remove local__nonstringdefinitionDocumentation/process/index.rst | 1 + .../process/programming-language.rst | 45 +++ MAINTAINERS | 5 + drivers/auxdisplay/panel.c | 7 +- fs/ext4/ext4.h | 9 - include/linux/compiler-clang.h | 5 - include/linux/compiler-gcc.h | 70 +---- include/linux/compiler-intel.h | 9 - include/linux/compiler.h | 19 +- include/linux/compiler_attributes.h | 258 ++++++++++++++++++ include/linux/compiler_types.h | 101 +------ scripts/Makefile.extrawarn | 1 + 12 files changed, 341 insertions(+), 189 deletions(-) create mode 100644 Documentation/process/programming-language.rst create mode 100644 include/linux/compiler_attributes.h--
2.17.1
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 18 lines · Miguel Ojeda
The Compiler Attributes series is an effort to disentangle the
include/linux/compiler*.hheaders 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_attributeit has amounted to a significant simplification of these headers (e.g.GCC_VERSIONis 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.
Quoted 6 lines
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_attributeon its own.Finally, the nonstring variable attribute series has been applied on top of this one.
Hi Miguel,
Feel free to add my
on patches 11-15 (I think the others ones already have it).
-- Luc
Quoted 24 lines · Luc Van Oostenryck
Quoted 22 lines · Miguel Ojeda
The Compiler Attributes series is an effort to disentangle the
include/linux/compiler*.hheaders 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_attributeit has amounted to a significant simplification of these headers (e.g.GCC_VERSIONis 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_attributeon its own.
Quoted 8 lines
Quoted 2 lines
Finally, the nonstring variable attribute series has been applied on top of this one.
Hi Miguel,
Feel free to add myReviewed-by Luc Van Oostenryck <[email protected]>on patches 11-15 (I think the others ones already have it).
Done for both Nick and you!
Cheers,
Miguel