xref: /linux/include/linux/compiler.h (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef __LINUX_COMPILER_H
31da177e4SLinus Torvalds #define __LINUX_COMPILER_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #ifndef __ASSEMBLY__
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #ifdef __CHECKER__
81da177e4SLinus Torvalds # define __user		__attribute__((noderef, address_space(1)))
9e0fdb0e0SRusty Russell # define __kernel	__attribute__((address_space(0)))
101da177e4SLinus Torvalds # define __safe		__attribute__((safe))
111da177e4SLinus Torvalds # define __force	__attribute__((force))
121da177e4SLinus Torvalds # define __nocast	__attribute__((nocast))
131da177e4SLinus Torvalds # define __iomem	__attribute__((noderef, address_space(2)))
148529091eSJosh Triplett # define __must_hold(x)	__attribute__((context(x,1,1)))
15c902e0a0SJosh Triplett # define __acquires(x)	__attribute__((context(x,0,1)))
16c902e0a0SJosh Triplett # define __releases(x)	__attribute__((context(x,1,0)))
17c902e0a0SJosh Triplett # define __acquire(x)	__context__(x,1)
18c902e0a0SJosh Triplett # define __release(x)	__context__(x,-1)
19dcc8e559SJosh Triplett # define __cond_lock(x,c)	((c) ? ({ __acquire(x); 1; }) : 0)
20e0fdb0e0SRusty Russell # define __percpu	__attribute__((noderef, address_space(3)))
21ca5ecddfSPaul E. McKenney # define __rcu		__attribute__((noderef, address_space(4)))
22ad315455SBoqun Feng # define __private	__attribute__((noderef))
23c47ffe3dSAl Viro extern void __chk_user_ptr(const volatile void __user *);
24c47ffe3dSAl Viro extern void __chk_io_ptr(const volatile void __iomem *);
25ad315455SBoqun Feng # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member))
26ad315455SBoqun Feng #else /* __CHECKER__ */
27c61f13eaSKees Cook # ifdef STRUCTLEAK_PLUGIN
28c61f13eaSKees Cook #  define __user __attribute__((user))
29c61f13eaSKees Cook # else
301da177e4SLinus Torvalds #  define __user
31c61f13eaSKees Cook # endif
321da177e4SLinus Torvalds # define __kernel
331da177e4SLinus Torvalds # define __safe
341da177e4SLinus Torvalds # define __force
351da177e4SLinus Torvalds # define __nocast
361da177e4SLinus Torvalds # define __iomem
371da177e4SLinus Torvalds # define __chk_user_ptr(x) (void)0
381da177e4SLinus Torvalds # define __chk_io_ptr(x) (void)0
391da177e4SLinus Torvalds # define __builtin_warning(x, y...) (1)
408529091eSJosh Triplett # define __must_hold(x)
411da177e4SLinus Torvalds # define __acquires(x)
421da177e4SLinus Torvalds # define __releases(x)
431da177e4SLinus Torvalds # define __acquire(x) (void)0
441da177e4SLinus Torvalds # define __release(x) (void)0
45dcc8e559SJosh Triplett # define __cond_lock(x,c) (c)
46e0fdb0e0SRusty Russell # define __percpu
4771d1d5c7SPaul E. McKenney # define __rcu
48ad315455SBoqun Feng # define __private
49ad315455SBoqun Feng # define ACCESS_PRIVATE(p, member) ((p)->member)
50ad315455SBoqun Feng #endif /* __CHECKER__ */
511da177e4SLinus Torvalds 
526f33d587SRusty Russell /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
536f33d587SRusty Russell #define ___PASTE(a,b) a##b
546f33d587SRusty Russell #define __PASTE(a,b) ___PASTE(a,b)
556f33d587SRusty Russell 
561da177e4SLinus Torvalds #ifdef __KERNEL__
571da177e4SLinus Torvalds 
58f153b821SLinus Torvalds #ifdef __GNUC__
59f153b821SLinus Torvalds #include <linux/compiler-gcc.h>
601da177e4SLinus Torvalds #endif
611da177e4SLinus Torvalds 
620c5a69f4SChristian Borntraeger #if defined(CC_USING_HOTPATCH) && !defined(__CHECKER__)
6361f55214SHeiko Carstens #define notrace __attribute__((hotpatch(0,0)))
6461f55214SHeiko Carstens #else
6528614889SSteven Rostedt #define notrace __attribute__((no_instrument_function))
6661f55214SHeiko Carstens #endif
6728614889SSteven Rostedt 
681da177e4SLinus Torvalds /* Intel compiler defines __GNUC__. So we will overwrite implementations
691da177e4SLinus Torvalds  * coming from above header files here
701da177e4SLinus Torvalds  */
711da177e4SLinus Torvalds #ifdef __INTEL_COMPILER
721da177e4SLinus Torvalds # include <linux/compiler-intel.h>
731da177e4SLinus Torvalds #endif
741da177e4SLinus Torvalds 
75565cbdc2SMark Charlebois /* Clang compiler defines __GNUC__. So we will overwrite implementations
76565cbdc2SMark Charlebois  * coming from above header files here
77565cbdc2SMark Charlebois  */
78565cbdc2SMark Charlebois #ifdef __clang__
79565cbdc2SMark Charlebois #include <linux/compiler-clang.h>
80565cbdc2SMark Charlebois #endif
81565cbdc2SMark Charlebois 
821da177e4SLinus Torvalds /*
831da177e4SLinus Torvalds  * Generic compiler-dependent macros required for kernel
841da177e4SLinus Torvalds  * build go below this comment. Actual compiler/compiler version
851da177e4SLinus Torvalds  * specific implementations come from the above header files
861da177e4SLinus Torvalds  */
871da177e4SLinus Torvalds 
882ed84eebSSteven Rostedt struct ftrace_branch_data {
891f0d69a9SSteven Rostedt 	const char *func;
901f0d69a9SSteven Rostedt 	const char *file;
911f0d69a9SSteven Rostedt 	unsigned line;
922bcd521aSSteven Rostedt 	union {
932bcd521aSSteven Rostedt 		struct {
941f0d69a9SSteven Rostedt 			unsigned long correct;
951f0d69a9SSteven Rostedt 			unsigned long incorrect;
961f0d69a9SSteven Rostedt 		};
972bcd521aSSteven Rostedt 		struct {
982bcd521aSSteven Rostedt 			unsigned long miss;
992bcd521aSSteven Rostedt 			unsigned long hit;
1002bcd521aSSteven Rostedt 		};
10197e7e4f3SWitold Baryluk 		unsigned long miss_hit[2];
1022bcd521aSSteven Rostedt 	};
1032bcd521aSSteven Rostedt };
1042ed84eebSSteven Rostedt 
105134e6a03SSteven Rostedt (VMware) struct ftrace_likely_data {
106134e6a03SSteven Rostedt (VMware) 	struct ftrace_branch_data	data;
107134e6a03SSteven Rostedt (VMware) 	unsigned long			constant;
108134e6a03SSteven Rostedt (VMware) };
109134e6a03SSteven Rostedt (VMware) 
1102ed84eebSSteven Rostedt /*
1112ed84eebSSteven Rostedt  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
1122ed84eebSSteven Rostedt  * to disable branch tracing on a per file basis.
1132ed84eebSSteven Rostedt  */
114d9ad8bc0SBart Van Assche #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
115d9ad8bc0SBart Van Assche     && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
116134e6a03SSteven Rostedt (VMware) void ftrace_likely_update(struct ftrace_likely_data *f, int val,
117d45ae1f7SSteven Rostedt (VMware) 			  int expect, int is_constant);
1181f0d69a9SSteven Rostedt 
1191f0d69a9SSteven Rostedt #define likely_notrace(x)	__builtin_expect(!!(x), 1)
1201f0d69a9SSteven Rostedt #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
1211f0d69a9SSteven Rostedt 
122d45ae1f7SSteven Rostedt (VMware) #define __branch_check__(x, expect, is_constant) ({			\
1231f0d69a9SSteven Rostedt 			int ______r;					\
124134e6a03SSteven Rostedt (VMware) 			static struct ftrace_likely_data		\
1251f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
12645b79749SSteven Rostedt 				__attribute__((section("_ftrace_annotated_branch"))) \
1271f0d69a9SSteven Rostedt 				______f = {				\
128134e6a03SSteven Rostedt (VMware) 				.data.func = __func__,			\
129134e6a03SSteven Rostedt (VMware) 				.data.file = __FILE__,			\
130134e6a03SSteven Rostedt (VMware) 				.data.line = __LINE__,			\
1311f0d69a9SSteven Rostedt 			};						\
132d45ae1f7SSteven Rostedt (VMware) 			______r = __builtin_expect(!!(x), expect);	\
133d45ae1f7SSteven Rostedt (VMware) 			ftrace_likely_update(&______f, ______r,		\
134d45ae1f7SSteven Rostedt (VMware) 					     expect, is_constant);	\
1351f0d69a9SSteven Rostedt 			______r;					\
1361f0d69a9SSteven Rostedt 		})
1371f0d69a9SSteven Rostedt 
1381f0d69a9SSteven Rostedt /*
1391f0d69a9SSteven Rostedt  * Using __builtin_constant_p(x) to ignore cases where the return
1401f0d69a9SSteven Rostedt  * value is always the same.  This idea is taken from a similar patch
1411f0d69a9SSteven Rostedt  * written by Daniel Walker.
1421f0d69a9SSteven Rostedt  */
1431f0d69a9SSteven Rostedt # ifndef likely
144d45ae1f7SSteven Rostedt (VMware) #  define likely(x)	(__branch_check__(x, 1, __builtin_constant_p(x)))
1451f0d69a9SSteven Rostedt # endif
1461f0d69a9SSteven Rostedt # ifndef unlikely
147d45ae1f7SSteven Rostedt (VMware) #  define unlikely(x)	(__branch_check__(x, 0, __builtin_constant_p(x)))
1481f0d69a9SSteven Rostedt # endif
1492bcd521aSSteven Rostedt 
1502bcd521aSSteven Rostedt #ifdef CONFIG_PROFILE_ALL_BRANCHES
1512bcd521aSSteven Rostedt /*
1522bcd521aSSteven Rostedt  * "Define 'is'", Bill Clinton
1532bcd521aSSteven Rostedt  * "Define 'if'", Steven Rostedt
1542bcd521aSSteven Rostedt  */
155ab3c9c68SLinus Torvalds #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
156ab3c9c68SLinus Torvalds #define __trace_if(cond) \
157b33c8ff4SArnd Bergmann 	if (__builtin_constant_p(!!(cond)) ? !!(cond) :			\
1582bcd521aSSteven Rostedt 	({								\
1592bcd521aSSteven Rostedt 		int ______r;						\
1602bcd521aSSteven Rostedt 		static struct ftrace_branch_data			\
1612bcd521aSSteven Rostedt 			__attribute__((__aligned__(4)))			\
1622bcd521aSSteven Rostedt 			__attribute__((section("_ftrace_branch")))	\
1632bcd521aSSteven Rostedt 			______f = {					\
1642bcd521aSSteven Rostedt 				.func = __func__,			\
1652bcd521aSSteven Rostedt 				.file = __FILE__,			\
1662bcd521aSSteven Rostedt 				.line = __LINE__,			\
1672bcd521aSSteven Rostedt 			};						\
1682bcd521aSSteven Rostedt 		______r = !!(cond);					\
16997e7e4f3SWitold Baryluk 		______f.miss_hit[______r]++;					\
1702bcd521aSSteven Rostedt 		______r;						\
1712bcd521aSSteven Rostedt 	}))
1722bcd521aSSteven Rostedt #endif /* CONFIG_PROFILE_ALL_BRANCHES */
1732bcd521aSSteven Rostedt 
1741f0d69a9SSteven Rostedt #else
1751da177e4SLinus Torvalds # define likely(x)	__builtin_expect(!!(x), 1)
1761da177e4SLinus Torvalds # define unlikely(x)	__builtin_expect(!!(x), 0)
1771f0d69a9SSteven Rostedt #endif
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds /* Optimization barrier */
1801da177e4SLinus Torvalds #ifndef barrier
1811da177e4SLinus Torvalds # define barrier() __memory_barrier()
1821da177e4SLinus Torvalds #endif
1831da177e4SLinus Torvalds 
1847829fb09SDaniel Borkmann #ifndef barrier_data
1857829fb09SDaniel Borkmann # define barrier_data(ptr) barrier()
1867829fb09SDaniel Borkmann #endif
1877829fb09SDaniel Borkmann 
18838938c87SDavid Daney /* Unreachable code */
189649ea4d5SJosh Poimboeuf #ifdef CONFIG_STACK_VALIDATION
190649ea4d5SJosh Poimboeuf #define annotate_reachable() ({						\
191649ea4d5SJosh Poimboeuf 	asm("%c0:\n\t"							\
192649ea4d5SJosh Poimboeuf 	    ".pushsection .discard.reachable\n\t"			\
193649ea4d5SJosh Poimboeuf 	    ".long %c0b - .\n\t"					\
194649ea4d5SJosh Poimboeuf 	    ".popsection\n\t" : : "i" (__LINE__));			\
195649ea4d5SJosh Poimboeuf })
196649ea4d5SJosh Poimboeuf #define annotate_unreachable() ({					\
197649ea4d5SJosh Poimboeuf 	asm("%c0:\n\t"							\
198649ea4d5SJosh Poimboeuf 	    ".pushsection .discard.unreachable\n\t"			\
199649ea4d5SJosh Poimboeuf 	    ".long %c0b - .\n\t"					\
200649ea4d5SJosh Poimboeuf 	    ".popsection\n\t" : : "i" (__LINE__));			\
201649ea4d5SJosh Poimboeuf })
202649ea4d5SJosh Poimboeuf #define ASM_UNREACHABLE							\
203649ea4d5SJosh Poimboeuf 	"999:\n\t"							\
204649ea4d5SJosh Poimboeuf 	".pushsection .discard.unreachable\n\t"				\
205649ea4d5SJosh Poimboeuf 	".long 999b - .\n\t"						\
206649ea4d5SJosh Poimboeuf 	".popsection\n\t"
207649ea4d5SJosh Poimboeuf #else
208649ea4d5SJosh Poimboeuf #define annotate_reachable()
209649ea4d5SJosh Poimboeuf #define annotate_unreachable()
210649ea4d5SJosh Poimboeuf #endif
211649ea4d5SJosh Poimboeuf 
212aa5d1b81SKees Cook #ifndef ASM_UNREACHABLE
213aa5d1b81SKees Cook # define ASM_UNREACHABLE
214aa5d1b81SKees Cook #endif
21538938c87SDavid Daney #ifndef unreachable
216649ea4d5SJosh Poimboeuf # define unreachable() do { annotate_reachable(); do { } while (1); } while (0)
21738938c87SDavid Daney #endif
21838938c87SDavid Daney 
219b67067f1SNicholas Piggin /*
220b67067f1SNicholas Piggin  * KENTRY - kernel entry point
221b67067f1SNicholas Piggin  * This can be used to annotate symbols (functions or data) that are used
222b67067f1SNicholas Piggin  * without their linker symbol being referenced explicitly. For example,
223b67067f1SNicholas Piggin  * interrupt vector handlers, or functions in the kernel image that are found
224b67067f1SNicholas Piggin  * programatically.
225b67067f1SNicholas Piggin  *
226b67067f1SNicholas Piggin  * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those
227b67067f1SNicholas Piggin  * are handled in their own way (with KEEP() in linker scripts).
228b67067f1SNicholas Piggin  *
229b67067f1SNicholas Piggin  * KENTRY can be avoided if the symbols in question are marked as KEEP() in the
230b67067f1SNicholas Piggin  * linker script. For example an architecture could KEEP() its entire
231b67067f1SNicholas Piggin  * boot/exception vector code rather than annotate each function and data.
232b67067f1SNicholas Piggin  */
233b67067f1SNicholas Piggin #ifndef KENTRY
234b67067f1SNicholas Piggin # define KENTRY(sym)						\
235b67067f1SNicholas Piggin 	extern typeof(sym) sym;					\
236b67067f1SNicholas Piggin 	static const unsigned long __kentry_##sym		\
237b67067f1SNicholas Piggin 	__used							\
238b67067f1SNicholas Piggin 	__attribute__((section("___kentry" "+" #sym ), used))	\
239b67067f1SNicholas Piggin 	= (unsigned long)&sym;
240b67067f1SNicholas Piggin #endif
241b67067f1SNicholas Piggin 
2421da177e4SLinus Torvalds #ifndef RELOC_HIDE
2431da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off)					\
2441da177e4SLinus Torvalds   ({ unsigned long __ptr;					\
2451da177e4SLinus Torvalds      __ptr = (unsigned long) (ptr);				\
2461da177e4SLinus Torvalds     (typeof(ptr)) (__ptr + (off)); })
2471da177e4SLinus Torvalds #endif
2481da177e4SLinus Torvalds 
249fe8c8a12SCesar Eduardo Barros #ifndef OPTIMIZER_HIDE_VAR
250fe8c8a12SCesar Eduardo Barros #define OPTIMIZER_HIDE_VAR(var) barrier()
251fe8c8a12SCesar Eduardo Barros #endif
252fe8c8a12SCesar Eduardo Barros 
2536f33d587SRusty Russell /* Not-quite-unique ID. */
2546f33d587SRusty Russell #ifndef __UNIQUE_ID
2556f33d587SRusty Russell # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
2566f33d587SRusty Russell #endif
2576f33d587SRusty Russell 
258230fa253SChristian Borntraeger #include <uapi/linux/types.h>
259230fa253SChristian Borntraeger 
260d976441fSAndrey Ryabinin #define __READ_ONCE_SIZE						\
261d976441fSAndrey Ryabinin ({									\
262d976441fSAndrey Ryabinin 	switch (size) {							\
263d976441fSAndrey Ryabinin 	case 1: *(__u8 *)res = *(volatile __u8 *)p; break;		\
264d976441fSAndrey Ryabinin 	case 2: *(__u16 *)res = *(volatile __u16 *)p; break;		\
265d976441fSAndrey Ryabinin 	case 4: *(__u32 *)res = *(volatile __u32 *)p; break;		\
266d976441fSAndrey Ryabinin 	case 8: *(__u64 *)res = *(volatile __u64 *)p; break;		\
267d976441fSAndrey Ryabinin 	default:							\
268d976441fSAndrey Ryabinin 		barrier();						\
269d976441fSAndrey Ryabinin 		__builtin_memcpy((void *)res, (const void *)p, size);	\
270d976441fSAndrey Ryabinin 		barrier();						\
271d976441fSAndrey Ryabinin 	}								\
272d976441fSAndrey Ryabinin })
273d976441fSAndrey Ryabinin 
274d976441fSAndrey Ryabinin static __always_inline
275d976441fSAndrey Ryabinin void __read_once_size(const volatile void *p, void *res, int size)
276230fa253SChristian Borntraeger {
277d976441fSAndrey Ryabinin 	__READ_ONCE_SIZE;
278230fa253SChristian Borntraeger }
279d976441fSAndrey Ryabinin 
280d976441fSAndrey Ryabinin #ifdef CONFIG_KASAN
281d976441fSAndrey Ryabinin /*
282d976441fSAndrey Ryabinin  * This function is not 'inline' because __no_sanitize_address confilcts
283d976441fSAndrey Ryabinin  * with inlining. Attempt to inline it may cause a build failure.
284d976441fSAndrey Ryabinin  * 	https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
285d976441fSAndrey Ryabinin  * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
286d976441fSAndrey Ryabinin  */
287d976441fSAndrey Ryabinin static __no_sanitize_address __maybe_unused
288d976441fSAndrey Ryabinin void __read_once_size_nocheck(const volatile void *p, void *res, int size)
289d976441fSAndrey Ryabinin {
290d976441fSAndrey Ryabinin 	__READ_ONCE_SIZE;
291230fa253SChristian Borntraeger }
292d976441fSAndrey Ryabinin #else
293d976441fSAndrey Ryabinin static __always_inline
294d976441fSAndrey Ryabinin void __read_once_size_nocheck(const volatile void *p, void *res, int size)
295d976441fSAndrey Ryabinin {
296d976441fSAndrey Ryabinin 	__READ_ONCE_SIZE;
297d976441fSAndrey Ryabinin }
298d976441fSAndrey Ryabinin #endif
299230fa253SChristian Borntraeger 
30043239cbeSChristian Borntraeger static __always_inline void __write_once_size(volatile void *p, void *res, int size)
301230fa253SChristian Borntraeger {
302230fa253SChristian Borntraeger 	switch (size) {
303230fa253SChristian Borntraeger 	case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
304230fa253SChristian Borntraeger 	case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
305230fa253SChristian Borntraeger 	case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
306230fa253SChristian Borntraeger 	case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
307230fa253SChristian Borntraeger 	default:
308230fa253SChristian Borntraeger 		barrier();
309230fa253SChristian Borntraeger 		__builtin_memcpy((void *)p, (const void *)res, size);
310230fa253SChristian Borntraeger 		barrier();
311230fa253SChristian Borntraeger 	}
312230fa253SChristian Borntraeger }
313230fa253SChristian Borntraeger 
314230fa253SChristian Borntraeger /*
315230fa253SChristian Borntraeger  * Prevent the compiler from merging or refetching reads or writes. The
316230fa253SChristian Borntraeger  * compiler is also forbidden from reordering successive instances of
31743239cbeSChristian Borntraeger  * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
318230fa253SChristian Borntraeger  * compiler is aware of some particular ordering.  One way to make the
319230fa253SChristian Borntraeger  * compiler aware of ordering is to put the two invocations of READ_ONCE,
32043239cbeSChristian Borntraeger  * WRITE_ONCE or ACCESS_ONCE() in different C statements.
321230fa253SChristian Borntraeger  *
322230fa253SChristian Borntraeger  * In contrast to ACCESS_ONCE these two macros will also work on aggregate
323230fa253SChristian Borntraeger  * data types like structs or unions. If the size of the accessed data
324230fa253SChristian Borntraeger  * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
325fed0764fSKonrad Rzeszutek Wilk  * READ_ONCE() and WRITE_ONCE() will fall back to memcpy(). There's at
326fed0764fSKonrad Rzeszutek Wilk  * least two memcpy()s: one for the __builtin_memcpy() and then one for
327fed0764fSKonrad Rzeszutek Wilk  * the macro doing the copy of variable - '__u' allocated on the stack.
328230fa253SChristian Borntraeger  *
329230fa253SChristian Borntraeger  * Their two major use cases are: (1) Mediating communication between
330230fa253SChristian Borntraeger  * process-level code and irq/NMI handlers, all running on the same CPU,
331230fa253SChristian Borntraeger  * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
332230fa253SChristian Borntraeger  * mutilate accesses that either do not require ordering or that interact
333230fa253SChristian Borntraeger  * with an explicit memory barrier or atomic instruction that provides the
334230fa253SChristian Borntraeger  * required ordering.
335230fa253SChristian Borntraeger  */
336230fa253SChristian Borntraeger 
337d976441fSAndrey Ryabinin #define __READ_ONCE(x, check)						\
338d976441fSAndrey Ryabinin ({									\
339d976441fSAndrey Ryabinin 	union { typeof(x) __val; char __c[1]; } __u;			\
340d976441fSAndrey Ryabinin 	if (check)							\
341d976441fSAndrey Ryabinin 		__read_once_size(&(x), __u.__c, sizeof(x));		\
342d976441fSAndrey Ryabinin 	else								\
343d976441fSAndrey Ryabinin 		__read_once_size_nocheck(&(x), __u.__c, sizeof(x));	\
344d976441fSAndrey Ryabinin 	__u.__val;							\
345d976441fSAndrey Ryabinin })
346d976441fSAndrey Ryabinin #define READ_ONCE(x) __READ_ONCE(x, 1)
347d976441fSAndrey Ryabinin 
348d976441fSAndrey Ryabinin /*
349d976441fSAndrey Ryabinin  * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
350d976441fSAndrey Ryabinin  * to hide memory access from KASAN.
351d976441fSAndrey Ryabinin  */
352d976441fSAndrey Ryabinin #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
353230fa253SChristian Borntraeger 
35443239cbeSChristian Borntraeger #define WRITE_ONCE(x, val) \
355ba33034fSChristian Borntraeger ({							\
356ba33034fSChristian Borntraeger 	union { typeof(x) __val; char __c[1]; } __u =	\
357ba33034fSChristian Borntraeger 		{ .__val = (__force typeof(x)) (val) }; \
358ba33034fSChristian Borntraeger 	__write_once_size(&(x), __u.__c, sizeof(x));	\
359ba33034fSChristian Borntraeger 	__u.__val;					\
360ba33034fSChristian Borntraeger })
361230fa253SChristian Borntraeger 
3621da177e4SLinus Torvalds #endif /* __KERNEL__ */
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */
3651da177e4SLinus Torvalds 
3664f79c3ffSDavid Woodhouse #ifdef __KERNEL__
3671da177e4SLinus Torvalds /*
3681da177e4SLinus Torvalds  * Allow us to mark functions as 'deprecated' and have gcc emit a nice
3691da177e4SLinus Torvalds  * warning for each use, in hopes of speeding the functions removal.
3701da177e4SLinus Torvalds  * Usage is:
3711da177e4SLinus Torvalds  * 		int __deprecated foo(void)
3721da177e4SLinus Torvalds  */
3731da177e4SLinus Torvalds #ifndef __deprecated
3741da177e4SLinus Torvalds # define __deprecated		/* unimplemented */
3751da177e4SLinus Torvalds #endif
3761da177e4SLinus Torvalds 
377512345beSPaul E. McKenney #ifdef MODULE
378512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated
379512345beSPaul E. McKenney #else
380512345beSPaul E. McKenney #define __deprecated_for_modules
381512345beSPaul E. McKenney #endif
382512345beSPaul E. McKenney 
3831da177e4SLinus Torvalds #ifndef __must_check
3841da177e4SLinus Torvalds #define __must_check
3851da177e4SLinus Torvalds #endif
3861da177e4SLinus Torvalds 
387cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK
388cebc04baSAndrew Morton #undef __must_check
389cebc04baSAndrew Morton #define __must_check
390cebc04baSAndrew Morton #endif
391de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED
392de488443SJeff Garzik #undef __deprecated
393de488443SJeff Garzik #undef __deprecated_for_modules
394de488443SJeff Garzik #define __deprecated
395de488443SJeff Garzik #define __deprecated_for_modules
396de488443SJeff Garzik #endif
397cebc04baSAndrew Morton 
398d64e85d3SRasmus Villemoes #ifndef __malloc
399d64e85d3SRasmus Villemoes #define __malloc
400d64e85d3SRasmus Villemoes #endif
401d64e85d3SRasmus Villemoes 
4021da177e4SLinus Torvalds /*
4031da177e4SLinus Torvalds  * Allow us to avoid 'defined but not used' warnings on functions and data,
4041da177e4SLinus Torvalds  * as well as force them to be emitted to the assembly file.
4051da177e4SLinus Torvalds  *
4060d7ebbbcSDavid Rientjes  * As of gcc 3.4, static functions that are not marked with attribute((used))
4070d7ebbbcSDavid Rientjes  * may be elided from the assembly file.  As of gcc 3.4, static data not so
4081da177e4SLinus Torvalds  * marked will not be elided, but this may change in a future gcc version.
4091da177e4SLinus Torvalds  *
4100d7ebbbcSDavid Rientjes  * NOTE: Because distributions shipped with a backported unit-at-a-time
4110d7ebbbcSDavid Rientjes  * compiler in gcc 3.3, we must define __used to be __attribute__((used))
4120d7ebbbcSDavid Rientjes  * for gcc >=3.3 instead of 3.4.
4130d7ebbbcSDavid Rientjes  *
4141da177e4SLinus Torvalds  * In prior versions of gcc, such functions and data would be emitted, but
4151da177e4SLinus Torvalds  * would be warned about except with attribute((unused)).
4160d7ebbbcSDavid Rientjes  *
4170d7ebbbcSDavid Rientjes  * Mark functions that are referenced only in inline assembly as __used so
4180d7ebbbcSDavid Rientjes  * the code is emitted even though it appears to be unreferenced.
4191da177e4SLinus Torvalds  */
4200d7ebbbcSDavid Rientjes #ifndef __used
4210d7ebbbcSDavid Rientjes # define __used			/* unimplemented */
4220d7ebbbcSDavid Rientjes #endif
4230d7ebbbcSDavid Rientjes 
4240d7ebbbcSDavid Rientjes #ifndef __maybe_unused
4250d7ebbbcSDavid Rientjes # define __maybe_unused		/* unimplemented */
4261da177e4SLinus Torvalds #endif
4271da177e4SLinus Torvalds 
4287b2a3513SLi Zefan #ifndef __always_unused
4297b2a3513SLi Zefan # define __always_unused	/* unimplemented */
4307b2a3513SLi Zefan #endif
4317b2a3513SLi Zefan 
432423bc7b2SDavid Woodhouse #ifndef noinline
433423bc7b2SDavid Woodhouse #define noinline
434423bc7b2SDavid Woodhouse #endif
435423bc7b2SDavid Woodhouse 
436735c4fb9SAndrew Morton /*
437735c4fb9SAndrew Morton  * Rather then using noinline to prevent stack consumption, use
438e6be0c9eSAlexander Stein  * noinline_for_stack instead.  For documentation reasons.
439735c4fb9SAndrew Morton  */
440735c4fb9SAndrew Morton #define noinline_for_stack noinline
441735c4fb9SAndrew Morton 
442423bc7b2SDavid Woodhouse #ifndef __always_inline
443423bc7b2SDavid Woodhouse #define __always_inline inline
444423bc7b2SDavid Woodhouse #endif
445423bc7b2SDavid Woodhouse 
446423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */
447423bc7b2SDavid Woodhouse 
4481da177e4SLinus Torvalds /*
4491da177e4SLinus Torvalds  * From the GCC manual:
4501da177e4SLinus Torvalds  *
4511da177e4SLinus Torvalds  * Many functions do not examine any values except their arguments,
4521da177e4SLinus Torvalds  * and have no effects except the return value.  Basically this is
4531da177e4SLinus Torvalds  * just slightly more strict class than the `pure' attribute above,
4541da177e4SLinus Torvalds  * since function is not allowed to read global memory.
4551da177e4SLinus Torvalds  *
4561da177e4SLinus Torvalds  * Note that a function that has pointer arguments and examines the
4571da177e4SLinus Torvalds  * data pointed to must _not_ be declared `const'.  Likewise, a
4581da177e4SLinus Torvalds  * function that calls a non-`const' function usually must not be
4591da177e4SLinus Torvalds  * `const'.  It does not make sense for a `const' function to return
4601da177e4SLinus Torvalds  * `void'.
4611da177e4SLinus Torvalds  */
4621da177e4SLinus Torvalds #ifndef __attribute_const__
4631da177e4SLinus Torvalds # define __attribute_const__	/* unimplemented */
4641da177e4SLinus Torvalds #endif
4651da177e4SLinus Torvalds 
4660aa5e49cSKees Cook #ifndef __designated_init
4670aa5e49cSKees Cook # define __designated_init
4680aa5e49cSKees Cook #endif
4690aa5e49cSKees Cook 
4700766f788SEmese Revfy #ifndef __latent_entropy
4710766f788SEmese Revfy # define __latent_entropy
4720766f788SEmese Revfy #endif
4730766f788SEmese Revfy 
474313dd1b6SKees Cook #ifndef __randomize_layout
475313dd1b6SKees Cook # define __randomize_layout __designated_init
476313dd1b6SKees Cook #endif
477313dd1b6SKees Cook 
478313dd1b6SKees Cook #ifndef __no_randomize_layout
479313dd1b6SKees Cook # define __no_randomize_layout
480313dd1b6SKees Cook #endif
481313dd1b6SKees Cook 
48229e48ce8SKees Cook #ifndef randomized_struct_fields_start
48329e48ce8SKees Cook # define randomized_struct_fields_start
48429e48ce8SKees Cook # define randomized_struct_fields_end
48529e48ce8SKees Cook #endif
48629e48ce8SKees Cook 
487a586df06SAndi Kleen /*
488a586df06SAndi Kleen  * Tell gcc if a function is cold. The compiler will assume any path
489a586df06SAndi Kleen  * directly leading to the call is unlikely.
490a586df06SAndi Kleen  */
491a586df06SAndi Kleen 
492a586df06SAndi Kleen #ifndef __cold
493a586df06SAndi Kleen #define __cold
494a586df06SAndi Kleen #endif
495a586df06SAndi Kleen 
496f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */
497f3fe866dSSam Ravnborg #ifndef __section
498f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S)))
499f3fe866dSSam Ravnborg #endif
500f3fe866dSSam Ravnborg 
5019a858dc7SAndi Kleen #ifndef __visible
5029a858dc7SAndi Kleen #define __visible
5039a858dc7SAndi Kleen #endif
5049a858dc7SAndi Kleen 
5057375ae3aSTom Lendacky #ifndef __nostackprotector
5067375ae3aSTom Lendacky # define __nostackprotector
5077375ae3aSTom Lendacky #endif
5087375ae3aSTom Lendacky 
509a744fd17SRasmus Villemoes /*
510a744fd17SRasmus Villemoes  * Assume alignment of return value.
511a744fd17SRasmus Villemoes  */
512a744fd17SRasmus Villemoes #ifndef __assume_aligned
513a744fd17SRasmus Villemoes #define __assume_aligned(a, ...)
514a744fd17SRasmus Villemoes #endif
515a744fd17SRasmus Villemoes 
516a744fd17SRasmus Villemoes 
517d2c123c2SRusty Russell /* Are two types/vars the same type (ignoring qualifiers)? */
518d2c123c2SRusty Russell #ifndef __same_type
519d2c123c2SRusty Russell # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
520d2c123c2SRusty Russell #endif
521d2c123c2SRusty Russell 
52247933ad4SPeter Zijlstra /* Is this type a native word size -- useful for atomic operations */
52347933ad4SPeter Zijlstra #ifndef __native_word
524536fa402SPaul E. McKenney # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
52547933ad4SPeter Zijlstra #endif
52647933ad4SPeter Zijlstra 
5279f0cf4adSArjan van de Ven /* Compile time object size, -1 for unknown */
5289f0cf4adSArjan van de Ven #ifndef __compiletime_object_size
5299f0cf4adSArjan van de Ven # define __compiletime_object_size(obj) -1
5309f0cf4adSArjan van de Ven #endif
5314a312769SArjan van de Ven #ifndef __compiletime_warning
5324a312769SArjan van de Ven # define __compiletime_warning(message)
5334a312769SArjan van de Ven #endif
53463312b6aSArjan van de Ven #ifndef __compiletime_error
53563312b6aSArjan van de Ven # define __compiletime_error(message)
5362c0d259eSJames Hogan /*
5372c0d259eSJames Hogan  * Sparse complains of variable sized arrays due to the temporary variable in
5382c0d259eSJames Hogan  * __compiletime_assert. Unfortunately we can't just expand it out to make
5392c0d259eSJames Hogan  * sparse see a constant array size without breaking compiletime_assert on old
5402c0d259eSJames Hogan  * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
5412c0d259eSJames Hogan  */
5422c0d259eSJames Hogan # ifndef __CHECKER__
543c361d3e5SDaniel Santos #  define __compiletime_error_fallback(condition) \
5449a8ab1c3SDaniel Santos 	do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
5452c0d259eSJames Hogan # endif
5462c0d259eSJames Hogan #endif
5472c0d259eSJames Hogan #ifndef __compiletime_error_fallback
548c361d3e5SDaniel Santos # define __compiletime_error_fallback(condition) do { } while (0)
54963312b6aSArjan van de Ven #endif
550c361d3e5SDaniel Santos 
551c03567a8SJoe Stringer #ifdef __OPTIMIZE__
5529a8ab1c3SDaniel Santos # define __compiletime_assert(condition, msg, prefix, suffix)		\
5539a8ab1c3SDaniel Santos 	do {								\
5549a8ab1c3SDaniel Santos 		bool __cond = !(condition);				\
5559a8ab1c3SDaniel Santos 		extern void prefix ## suffix(void) __compiletime_error(msg); \
5569a8ab1c3SDaniel Santos 		if (__cond)						\
5579a8ab1c3SDaniel Santos 			prefix ## suffix();				\
5589a8ab1c3SDaniel Santos 		__compiletime_error_fallback(__cond);			\
5599a8ab1c3SDaniel Santos 	} while (0)
560c03567a8SJoe Stringer #else
561c03567a8SJoe Stringer # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
562c03567a8SJoe Stringer #endif
5639a8ab1c3SDaniel Santos 
5649a8ab1c3SDaniel Santos #define _compiletime_assert(condition, msg, prefix, suffix) \
5659a8ab1c3SDaniel Santos 	__compiletime_assert(condition, msg, prefix, suffix)
5669a8ab1c3SDaniel Santos 
5679a8ab1c3SDaniel Santos /**
5689a8ab1c3SDaniel Santos  * compiletime_assert - break build and emit msg if condition is false
5699a8ab1c3SDaniel Santos  * @condition: a compile-time constant condition to check
5709a8ab1c3SDaniel Santos  * @msg:       a message to emit if condition is false
5719a8ab1c3SDaniel Santos  *
5729a8ab1c3SDaniel Santos  * In tradition of POSIX assert, this macro will break the build if the
5739a8ab1c3SDaniel Santos  * supplied condition is *false*, emitting the supplied error message if the
5749a8ab1c3SDaniel Santos  * compiler has support to do so.
5759a8ab1c3SDaniel Santos  */
5769a8ab1c3SDaniel Santos #define compiletime_assert(condition, msg) \
5779a8ab1c3SDaniel Santos 	_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
5789a8ab1c3SDaniel Santos 
57947933ad4SPeter Zijlstra #define compiletime_assert_atomic_type(t)				\
58047933ad4SPeter Zijlstra 	compiletime_assert(__native_word(t),				\
58147933ad4SPeter Zijlstra 		"Need native word sized stores/loads for atomicity.")
58247933ad4SPeter Zijlstra 
5839c3cdc1fSLinus Torvalds /*
5849c3cdc1fSLinus Torvalds  * Prevent the compiler from merging or refetching accesses.  The compiler
5859c3cdc1fSLinus Torvalds  * is also forbidden from reordering successive instances of ACCESS_ONCE(),
5869c3cdc1fSLinus Torvalds  * but only when the compiler is aware of some particular ordering.  One way
5879c3cdc1fSLinus Torvalds  * to make the compiler aware of ordering is to put the two invocations of
5889c3cdc1fSLinus Torvalds  * ACCESS_ONCE() in different C statements.
5899c3cdc1fSLinus Torvalds  *
590927609d6SChristian Borntraeger  * ACCESS_ONCE will only work on scalar types. For union types, ACCESS_ONCE
591927609d6SChristian Borntraeger  * on a union member will work as long as the size of the member matches the
592927609d6SChristian Borntraeger  * size of the union and the size is smaller than word size.
593927609d6SChristian Borntraeger  *
594927609d6SChristian Borntraeger  * The major use cases of ACCESS_ONCE used to be (1) Mediating communication
595927609d6SChristian Borntraeger  * between process-level code and irq/NMI handlers, all running on the same CPU,
596927609d6SChristian Borntraeger  * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
597927609d6SChristian Borntraeger  * mutilate accesses that either do not require ordering or that interact
598927609d6SChristian Borntraeger  * with an explicit memory barrier or atomic instruction that provides the
599927609d6SChristian Borntraeger  * required ordering.
600927609d6SChristian Borntraeger  *
601663fdcbeSPreeti U Murthy  * If possible use READ_ONCE()/WRITE_ONCE() instead.
6029c3cdc1fSLinus Torvalds  */
603927609d6SChristian Borntraeger #define __ACCESS_ONCE(x) ({ \
604c5b19946SChristian Borntraeger 	 __maybe_unused typeof(x) __var = (__force typeof(x)) 0; \
605927609d6SChristian Borntraeger 	(volatile typeof(x) *)&(x); })
606927609d6SChristian Borntraeger #define ACCESS_ONCE(x) (*__ACCESS_ONCE(x))
6079c3cdc1fSLinus Torvalds 
6080a04b016SPeter Zijlstra /**
6090a04b016SPeter Zijlstra  * lockless_dereference() - safely load a pointer for later dereference
6100a04b016SPeter Zijlstra  * @p: The pointer to load
6110a04b016SPeter Zijlstra  *
6120a04b016SPeter Zijlstra  * Similar to rcu_dereference(), but for situations where the pointed-to
6130a04b016SPeter Zijlstra  * object's lifetime is managed by something other than RCU.  That
6140a04b016SPeter Zijlstra  * "something other" might be reference counting or simple immortality.
615331b6d8cSPeter Zijlstra  *
616d7127b5eSJohannes Berg  * The seemingly unused variable ___typecheck_p validates that @p is
617d7127b5eSJohannes Berg  * indeed a pointer type by using a pointer to typeof(*p) as the type.
618d7127b5eSJohannes Berg  * Taking a pointer to typeof(*p) again is needed in case p is void *.
6190a04b016SPeter Zijlstra  */
6200a04b016SPeter Zijlstra #define lockless_dereference(p) \
6210a04b016SPeter Zijlstra ({ \
62238183b9cSStephen Rothwell 	typeof(p) _________p1 = READ_ONCE(p); \
623d7127b5eSJohannes Berg 	typeof(*(p)) *___typecheck_p __maybe_unused; \
6240a04b016SPeter Zijlstra 	smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
6250a04b016SPeter Zijlstra 	(_________p1); \
6260a04b016SPeter Zijlstra })
6270a04b016SPeter Zijlstra 
6281da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */
629