forked from mirrors/linux
		
	 e468f5c06b
			
		
	
	
		e468f5c06b
		
	
	
	
	
		
			
			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. -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEPjU5OPd5QIZ9jqqOGXyLc2htIW0FAlvNpywACgkQGXyLc2ht IW1aiQ/+P8SJOa3GkiH37/nrIbk/wgMNytbs+gxE5YPaU1DP74Mn1prJ4XhQQic9 /mt8GnitZwzEHWdsGEUk+ZQwnIa7ZEAmpecbAF206AMRbNxa14T5YwBx4bqWFjZp sP4zPTHt3JCKL8TM+z26o152UbF2kc4WSxHjEjSFaqEnR2E5D0MwFeGPzc8fgWmS pNyn3CidzB0TS1UF008YXhiJO6HIhFNPyhPawlhwbbdsdlhZ4u0JmwfqP4EvjRFM kyzdQ9CDe+AgTTD9Y8HhtoUClaa7SJzFWNzpKIJMWt8jpKWYZQ/+WtwKg2cf+v3M uwktcs3RI1dYrjcITLz4VJ0oVaRFnyGgXvMP4yqWQx429hqnd09WXhMioXQ1htoI H0vpPIAPsK+dqVA9sP3JzMq4h6+dE7P364lkbThbVpYAGKZ52qaLt9ixT1mw1Q9f a683ji6o02IVOGUNZ/3KAb5MqdhewNEDdZILZYRfm4AL1Em3WW9QVtIosHPviLgc 16VjA02wKdxIcg+1LZMTNhfybztnSCf7SuQurpH1zEqFDGzrXwB7nYFplEY7DrrD cqhOA1fMQa++oQR+D40QDoY2ybqPOyvJG7z17pvtt+6jXep4yy2a3Bxf+ClK0nto 5yT7v9ikXJr84FOkk7OvktLlAWvcykvAdfvDepBZhpqhuX82tHY= =Y8WB -----END PGP SIGNATURE----- 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
		
			
				
	
	
		
			381 lines
		
	
	
	
		
			11 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			381 lines
		
	
	
	
		
			11 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /* SPDX-License-Identifier: GPL-2.0 */
 | |
| #ifndef __LINUX_COMPILER_H
 | |
| #define __LINUX_COMPILER_H
 | |
| 
 | |
| #include <linux/compiler_types.h>
 | |
| 
 | |
| #ifndef __ASSEMBLY__
 | |
| 
 | |
| #ifdef __KERNEL__
 | |
| 
 | |
| /*
 | |
|  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
 | |
|  * to disable branch tracing on a per file basis.
 | |
|  */
 | |
| #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
 | |
|     && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
 | |
| void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 | |
| 			  int expect, int is_constant);
 | |
| 
 | |
| #define likely_notrace(x)	__builtin_expect(!!(x), 1)
 | |
| #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
 | |
| 
 | |
| #define __branch_check__(x, expect, is_constant) ({			\
 | |
| 			long ______r;					\
 | |
| 			static struct ftrace_likely_data		\
 | |
| 				__aligned(4)				\
 | |
| 				__section("_ftrace_annotated_branch")	\
 | |
| 				______f = {				\
 | |
| 				.data.func = __func__,			\
 | |
| 				.data.file = __FILE__,			\
 | |
| 				.data.line = __LINE__,			\
 | |
| 			};						\
 | |
| 			______r = __builtin_expect(!!(x), expect);	\
 | |
| 			ftrace_likely_update(&______f, ______r,		\
 | |
| 					     expect, is_constant);	\
 | |
| 			______r;					\
 | |
| 		})
 | |
| 
 | |
| /*
 | |
|  * Using __builtin_constant_p(x) to ignore cases where the return
 | |
|  * value is always the same.  This idea is taken from a similar patch
 | |
|  * written by Daniel Walker.
 | |
|  */
 | |
| # ifndef likely
 | |
| #  define likely(x)	(__branch_check__(x, 1, __builtin_constant_p(x)))
 | |
| # endif
 | |
| # ifndef unlikely
 | |
| #  define unlikely(x)	(__branch_check__(x, 0, __builtin_constant_p(x)))
 | |
| # endif
 | |
| 
 | |
| #ifdef CONFIG_PROFILE_ALL_BRANCHES
 | |
| /*
 | |
|  * "Define 'is'", Bill Clinton
 | |
|  * "Define 'if'", Steven Rostedt
 | |
|  */
 | |
| #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
 | |
| #define __trace_if(cond) \
 | |
| 	if (__builtin_constant_p(!!(cond)) ? !!(cond) :			\
 | |
| 	({								\
 | |
| 		int ______r;						\
 | |
| 		static struct ftrace_branch_data			\
 | |
| 			__aligned(4)					\
 | |
| 			__section("_ftrace_branch")			\
 | |
| 			______f = {					\
 | |
| 				.func = __func__,			\
 | |
| 				.file = __FILE__,			\
 | |
| 				.line = __LINE__,			\
 | |
| 			};						\
 | |
| 		______r = !!(cond);					\
 | |
| 		______f.miss_hit[______r]++;					\
 | |
| 		______r;						\
 | |
| 	}))
 | |
| #endif /* CONFIG_PROFILE_ALL_BRANCHES */
 | |
| 
 | |
| #else
 | |
| # define likely(x)	__builtin_expect(!!(x), 1)
 | |
| # define unlikely(x)	__builtin_expect(!!(x), 0)
 | |
| #endif
 | |
| 
 | |
| /* Optimization barrier */
 | |
| #ifndef barrier
 | |
| # define barrier() __memory_barrier()
 | |
| #endif
 | |
| 
 | |
| #ifndef barrier_data
 | |
| # define barrier_data(ptr) barrier()
 | |
| #endif
 | |
| 
 | |
| /* workaround for GCC PR82365 if needed */
 | |
| #ifndef barrier_before_unreachable
 | |
| # define barrier_before_unreachable() do { } while (0)
 | |
| #endif
 | |
| 
 | |
| /* Unreachable code */
 | |
| #ifdef CONFIG_STACK_VALIDATION
 | |
| /*
 | |
|  * These macros help objtool understand GCC code flow for unreachable code.
 | |
|  * The __COUNTER__ based labels are a hack to make each instance of the macros
 | |
|  * unique, to convince GCC not to merge duplicate inline asm statements.
 | |
|  */
 | |
| #define annotate_reachable() ({						\
 | |
| 	asm volatile("ANNOTATE_REACHABLE counter=%c0"			\
 | |
| 		     : : "i" (__COUNTER__));				\
 | |
| })
 | |
| #define annotate_unreachable() ({					\
 | |
| 	asm volatile("ANNOTATE_UNREACHABLE counter=%c0"			\
 | |
| 		     : : "i" (__COUNTER__));				\
 | |
| })
 | |
| #else
 | |
| #define annotate_reachable()
 | |
| #define annotate_unreachable()
 | |
| #endif
 | |
| 
 | |
| #ifndef ASM_UNREACHABLE
 | |
| # define ASM_UNREACHABLE
 | |
| #endif
 | |
| #ifndef unreachable
 | |
| # define unreachable() do {		\
 | |
| 	annotate_unreachable();		\
 | |
| 	__builtin_unreachable();	\
 | |
| } while (0)
 | |
| #endif
 | |
| 
 | |
| /*
 | |
|  * KENTRY - kernel entry point
 | |
|  * This can be used to annotate symbols (functions or data) that are used
 | |
|  * without their linker symbol being referenced explicitly. For example,
 | |
|  * interrupt vector handlers, or functions in the kernel image that are found
 | |
|  * programatically.
 | |
|  *
 | |
|  * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those
 | |
|  * are handled in their own way (with KEEP() in linker scripts).
 | |
|  *
 | |
|  * KENTRY can be avoided if the symbols in question are marked as KEEP() in the
 | |
|  * linker script. For example an architecture could KEEP() its entire
 | |
|  * boot/exception vector code rather than annotate each function and data.
 | |
|  */
 | |
| #ifndef KENTRY
 | |
| # define KENTRY(sym)						\
 | |
| 	extern typeof(sym) sym;					\
 | |
| 	static const unsigned long __kentry_##sym		\
 | |
| 	__used							\
 | |
| 	__section("___kentry" "+" #sym )			\
 | |
| 	= (unsigned long)&sym;
 | |
| #endif
 | |
| 
 | |
| #ifndef RELOC_HIDE
 | |
| # define RELOC_HIDE(ptr, off)					\
 | |
|   ({ unsigned long __ptr;					\
 | |
|      __ptr = (unsigned long) (ptr);				\
 | |
|     (typeof(ptr)) (__ptr + (off)); })
 | |
| #endif
 | |
| 
 | |
| #ifndef OPTIMIZER_HIDE_VAR
 | |
| #define OPTIMIZER_HIDE_VAR(var) barrier()
 | |
| #endif
 | |
| 
 | |
| /* Not-quite-unique ID. */
 | |
| #ifndef __UNIQUE_ID
 | |
| # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
 | |
| #endif
 | |
| 
 | |
| #include <uapi/linux/types.h>
 | |
| 
 | |
| #define __READ_ONCE_SIZE						\
 | |
| ({									\
 | |
| 	switch (size) {							\
 | |
| 	case 1: *(__u8 *)res = *(volatile __u8 *)p; break;		\
 | |
| 	case 2: *(__u16 *)res = *(volatile __u16 *)p; break;		\
 | |
| 	case 4: *(__u32 *)res = *(volatile __u32 *)p; break;		\
 | |
| 	case 8: *(__u64 *)res = *(volatile __u64 *)p; break;		\
 | |
| 	default:							\
 | |
| 		barrier();						\
 | |
| 		__builtin_memcpy((void *)res, (const void *)p, size);	\
 | |
| 		barrier();						\
 | |
| 	}								\
 | |
| })
 | |
| 
 | |
| static __always_inline
 | |
| void __read_once_size(const volatile void *p, void *res, int size)
 | |
| {
 | |
| 	__READ_ONCE_SIZE;
 | |
| }
 | |
| 
 | |
| #ifdef CONFIG_KASAN
 | |
| /*
 | |
|  * We can't declare function 'inline' because __no_sanitize_address confilcts
 | |
|  * with inlining. Attempt to inline it may cause a build failure.
 | |
|  * 	https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
 | |
|  * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
 | |
|  */
 | |
| # define __no_kasan_or_inline __no_sanitize_address __maybe_unused
 | |
| #else
 | |
| # define __no_kasan_or_inline __always_inline
 | |
| #endif
 | |
| 
 | |
| static __no_kasan_or_inline
 | |
| void __read_once_size_nocheck(const volatile void *p, void *res, int size)
 | |
| {
 | |
| 	__READ_ONCE_SIZE;
 | |
| }
 | |
| 
 | |
| static __always_inline void __write_once_size(volatile void *p, void *res, int size)
 | |
| {
 | |
| 	switch (size) {
 | |
| 	case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
 | |
| 	case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
 | |
| 	case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
 | |
| 	case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
 | |
| 	default:
 | |
| 		barrier();
 | |
| 		__builtin_memcpy((void *)p, (const void *)res, size);
 | |
| 		barrier();
 | |
| 	}
 | |
| }
 | |
| 
 | |
| /*
 | |
|  * Prevent the compiler from merging or refetching reads or writes. The
 | |
|  * compiler is also forbidden from reordering successive instances of
 | |
|  * READ_ONCE and WRITE_ONCE, but only when the compiler is aware of some
 | |
|  * particular ordering. One way to make the compiler aware of ordering is to
 | |
|  * put the two invocations of READ_ONCE or WRITE_ONCE in different C
 | |
|  * statements.
 | |
|  *
 | |
|  * These two macros will also work on aggregate data types like structs or
 | |
|  * unions. If the size of the accessed data type exceeds the word size of
 | |
|  * the machine (e.g., 32 bits or 64 bits) READ_ONCE() and WRITE_ONCE() will
 | |
|  * fall back to memcpy(). There's at least two memcpy()s: one for the
 | |
|  * __builtin_memcpy() and then one for the macro doing the copy of variable
 | |
|  * - '__u' allocated on the stack.
 | |
|  *
 | |
|  * Their two major use cases are: (1) Mediating communication between
 | |
|  * process-level code and irq/NMI handlers, all running on the same CPU,
 | |
|  * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
 | |
|  * mutilate accesses that either do not require ordering or that interact
 | |
|  * with an explicit memory barrier or atomic instruction that provides the
 | |
|  * required ordering.
 | |
|  */
 | |
| #include <asm/barrier.h>
 | |
| #include <linux/kasan-checks.h>
 | |
| 
 | |
| #define __READ_ONCE(x, check)						\
 | |
| ({									\
 | |
| 	union { typeof(x) __val; char __c[1]; } __u;			\
 | |
| 	if (check)							\
 | |
| 		__read_once_size(&(x), __u.__c, sizeof(x));		\
 | |
| 	else								\
 | |
| 		__read_once_size_nocheck(&(x), __u.__c, sizeof(x));	\
 | |
| 	smp_read_barrier_depends(); /* Enforce dependency ordering from x */ \
 | |
| 	__u.__val;							\
 | |
| })
 | |
| #define READ_ONCE(x) __READ_ONCE(x, 1)
 | |
| 
 | |
| /*
 | |
|  * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
 | |
|  * to hide memory access from KASAN.
 | |
|  */
 | |
| #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
 | |
| 
 | |
| static __no_kasan_or_inline
 | |
| unsigned long read_word_at_a_time(const void *addr)
 | |
| {
 | |
| 	kasan_check_read(addr, 1);
 | |
| 	return *(unsigned long *)addr;
 | |
| }
 | |
| 
 | |
| #define WRITE_ONCE(x, val) \
 | |
| ({							\
 | |
| 	union { typeof(x) __val; char __c[1]; } __u =	\
 | |
| 		{ .__val = (__force typeof(x)) (val) }; \
 | |
| 	__write_once_size(&(x), __u.__c, sizeof(x));	\
 | |
| 	__u.__val;					\
 | |
| })
 | |
| 
 | |
| #endif /* __KERNEL__ */
 | |
| 
 | |
| /*
 | |
|  * Force the compiler to emit 'sym' as a symbol, so that we can reference
 | |
|  * it from inline assembler. Necessary in case 'sym' could be inlined
 | |
|  * otherwise, or eliminated entirely due to lack of references that are
 | |
|  * visible to the compiler.
 | |
|  */
 | |
| #define __ADDRESSABLE(sym) \
 | |
| 	static void * __section(".discard.addressable") __used \
 | |
| 		__PASTE(__addressable_##sym, __LINE__) = (void *)&sym;
 | |
| 
 | |
| /**
 | |
|  * offset_to_ptr - convert a relative memory offset to an absolute pointer
 | |
|  * @off:	the address of the 32-bit offset value
 | |
|  */
 | |
| static inline void *offset_to_ptr(const int *off)
 | |
| {
 | |
| 	return (void *)((unsigned long)off + *off);
 | |
| }
 | |
| 
 | |
| #else /* __ASSEMBLY__ */
 | |
| 
 | |
| #ifdef __KERNEL__
 | |
| #ifndef LINKER_SCRIPT
 | |
| 
 | |
| #ifdef CONFIG_STACK_VALIDATION
 | |
| .macro ANNOTATE_UNREACHABLE counter:req
 | |
| \counter:
 | |
| 	.pushsection .discard.unreachable
 | |
| 	.long \counter\()b -.
 | |
| 	.popsection
 | |
| .endm
 | |
| 
 | |
| .macro ANNOTATE_REACHABLE counter:req
 | |
| \counter:
 | |
| 	.pushsection .discard.reachable
 | |
| 	.long \counter\()b -.
 | |
| 	.popsection
 | |
| .endm
 | |
| 
 | |
| .macro ASM_UNREACHABLE
 | |
| 999:
 | |
| 	.pushsection .discard.unreachable
 | |
| 	.long 999b - .
 | |
| 	.popsection
 | |
| .endm
 | |
| #else /* CONFIG_STACK_VALIDATION */
 | |
| .macro ANNOTATE_UNREACHABLE counter:req
 | |
| .endm
 | |
| 
 | |
| .macro ANNOTATE_REACHABLE counter:req
 | |
| .endm
 | |
| 
 | |
| .macro ASM_UNREACHABLE
 | |
| .endm
 | |
| #endif /* CONFIG_STACK_VALIDATION */
 | |
| 
 | |
| #endif /* LINKER_SCRIPT */
 | |
| #endif /* __KERNEL__ */
 | |
| #endif /* __ASSEMBLY__ */
 | |
| 
 | |
| /* Compile time object size, -1 for unknown */
 | |
| #ifndef __compiletime_object_size
 | |
| # define __compiletime_object_size(obj) -1
 | |
| #endif
 | |
| #ifndef __compiletime_warning
 | |
| # define __compiletime_warning(message)
 | |
| #endif
 | |
| #ifndef __compiletime_error
 | |
| # define __compiletime_error(message)
 | |
| #endif
 | |
| 
 | |
| #ifdef __OPTIMIZE__
 | |
| # define __compiletime_assert(condition, msg, prefix, suffix)		\
 | |
| 	do {								\
 | |
| 		extern void prefix ## suffix(void) __compiletime_error(msg); \
 | |
| 		if (!(condition))					\
 | |
| 			prefix ## suffix();				\
 | |
| 	} while (0)
 | |
| #else
 | |
| # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
 | |
| #endif
 | |
| 
 | |
| #define _compiletime_assert(condition, msg, prefix, suffix) \
 | |
| 	__compiletime_assert(condition, msg, prefix, suffix)
 | |
| 
 | |
| /**
 | |
|  * compiletime_assert - break build and emit msg if condition is false
 | |
|  * @condition: a compile-time constant condition to check
 | |
|  * @msg:       a message to emit if condition is false
 | |
|  *
 | |
|  * In tradition of POSIX assert, this macro will break the build if the
 | |
|  * supplied condition is *false*, emitting the supplied error message if the
 | |
|  * compiler has support to do so.
 | |
|  */
 | |
| #define compiletime_assert(condition, msg) \
 | |
| 	_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
 | |
| 
 | |
| #define compiletime_assert_atomic_type(t)				\
 | |
| 	compiletime_assert(__native_word(t),				\
 | |
| 		"Need native word sized stores/loads for atomicity.")
 | |
| 
 | |
| /* &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 /* __LINUX_COMPILER_H */
 |