xref: /linux/include/linux/compiler.h (revision 649ea4d5a624f061a111b1f1cb0e47cfdc3ac21b)
11da177e4SLinus Torvalds #ifndef __LINUX_COMPILER_H
21da177e4SLinus Torvalds #define __LINUX_COMPILER_H
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds #ifndef __ASSEMBLY__
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #ifdef __CHECKER__
71da177e4SLinus Torvalds # define __user		__attribute__((noderef, address_space(1)))
8e0fdb0e0SRusty Russell # define __kernel	__attribute__((address_space(0)))
91da177e4SLinus Torvalds # define __safe		__attribute__((safe))
101da177e4SLinus Torvalds # define __force	__attribute__((force))
111da177e4SLinus Torvalds # define __nocast	__attribute__((nocast))
121da177e4SLinus Torvalds # define __iomem	__attribute__((noderef, address_space(2)))
138529091eSJosh Triplett # define __must_hold(x)	__attribute__((context(x,1,1)))
14c902e0a0SJosh Triplett # define __acquires(x)	__attribute__((context(x,0,1)))
15c902e0a0SJosh Triplett # define __releases(x)	__attribute__((context(x,1,0)))
16c902e0a0SJosh Triplett # define __acquire(x)	__context__(x,1)
17c902e0a0SJosh Triplett # define __release(x)	__context__(x,-1)
18dcc8e559SJosh Triplett # define __cond_lock(x,c)	((c) ? ({ __acquire(x); 1; }) : 0)
19e0fdb0e0SRusty Russell # define __percpu	__attribute__((noderef, address_space(3)))
20ca5ecddfSPaul E. McKenney # define __rcu		__attribute__((noderef, address_space(4)))
21ad315455SBoqun Feng # define __private	__attribute__((noderef))
22c47ffe3dSAl Viro extern void __chk_user_ptr(const volatile void __user *);
23c47ffe3dSAl Viro extern void __chk_io_ptr(const volatile void __iomem *);
24ad315455SBoqun Feng # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member))
25ad315455SBoqun Feng #else /* __CHECKER__ */
26c61f13eaSKees Cook # ifdef STRUCTLEAK_PLUGIN
27c61f13eaSKees Cook #  define __user __attribute__((user))
28c61f13eaSKees Cook # else
291da177e4SLinus Torvalds #  define __user
30c61f13eaSKees Cook # endif
311da177e4SLinus Torvalds # define __kernel
321da177e4SLinus Torvalds # define __safe
331da177e4SLinus Torvalds # define __force
341da177e4SLinus Torvalds # define __nocast
351da177e4SLinus Torvalds # define __iomem
361da177e4SLinus Torvalds # define __chk_user_ptr(x) (void)0
371da177e4SLinus Torvalds # define __chk_io_ptr(x) (void)0
381da177e4SLinus Torvalds # define __builtin_warning(x, y...) (1)
398529091eSJosh Triplett # define __must_hold(x)
401da177e4SLinus Torvalds # define __acquires(x)
411da177e4SLinus Torvalds # define __releases(x)
421da177e4SLinus Torvalds # define __acquire(x) (void)0
431da177e4SLinus Torvalds # define __release(x) (void)0
44dcc8e559SJosh Triplett # define __cond_lock(x,c) (c)
45e0fdb0e0SRusty Russell # define __percpu
4671d1d5c7SPaul E. McKenney # define __rcu
47ad315455SBoqun Feng # define __private
48ad315455SBoqun Feng # define ACCESS_PRIVATE(p, member) ((p)->member)
49ad315455SBoqun Feng #endif /* __CHECKER__ */
501da177e4SLinus Torvalds 
516f33d587SRusty Russell /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
526f33d587SRusty Russell #define ___PASTE(a,b) a##b
536f33d587SRusty Russell #define __PASTE(a,b) ___PASTE(a,b)
546f33d587SRusty Russell 
551da177e4SLinus Torvalds #ifdef __KERNEL__
561da177e4SLinus Torvalds 
57f153b821SLinus Torvalds #ifdef __GNUC__
58f153b821SLinus Torvalds #include <linux/compiler-gcc.h>
591da177e4SLinus Torvalds #endif
601da177e4SLinus Torvalds 
610c5a69f4SChristian Borntraeger #if defined(CC_USING_HOTPATCH) && !defined(__CHECKER__)
6261f55214SHeiko Carstens #define notrace __attribute__((hotpatch(0,0)))
6361f55214SHeiko Carstens #else
6428614889SSteven Rostedt #define notrace __attribute__((no_instrument_function))
6561f55214SHeiko Carstens #endif
6628614889SSteven Rostedt 
671da177e4SLinus Torvalds /* Intel compiler defines __GNUC__. So we will overwrite implementations
681da177e4SLinus Torvalds  * coming from above header files here
691da177e4SLinus Torvalds  */
701da177e4SLinus Torvalds #ifdef __INTEL_COMPILER
711da177e4SLinus Torvalds # include <linux/compiler-intel.h>
721da177e4SLinus Torvalds #endif
731da177e4SLinus Torvalds 
74565cbdc2SMark Charlebois /* Clang compiler defines __GNUC__. So we will overwrite implementations
75565cbdc2SMark Charlebois  * coming from above header files here
76565cbdc2SMark Charlebois  */
77565cbdc2SMark Charlebois #ifdef __clang__
78565cbdc2SMark Charlebois #include <linux/compiler-clang.h>
79565cbdc2SMark Charlebois #endif
80565cbdc2SMark Charlebois 
811da177e4SLinus Torvalds /*
821da177e4SLinus Torvalds  * Generic compiler-dependent macros required for kernel
831da177e4SLinus Torvalds  * build go below this comment. Actual compiler/compiler version
841da177e4SLinus Torvalds  * specific implementations come from the above header files
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds 
872ed84eebSSteven Rostedt struct ftrace_branch_data {
881f0d69a9SSteven Rostedt 	const char *func;
891f0d69a9SSteven Rostedt 	const char *file;
901f0d69a9SSteven Rostedt 	unsigned line;
912bcd521aSSteven Rostedt 	union {
922bcd521aSSteven Rostedt 		struct {
931f0d69a9SSteven Rostedt 			unsigned long correct;
941f0d69a9SSteven Rostedt 			unsigned long incorrect;
951f0d69a9SSteven Rostedt 		};
962bcd521aSSteven Rostedt 		struct {
972bcd521aSSteven Rostedt 			unsigned long miss;
982bcd521aSSteven Rostedt 			unsigned long hit;
992bcd521aSSteven Rostedt 		};
10097e7e4f3SWitold Baryluk 		unsigned long miss_hit[2];
1012bcd521aSSteven Rostedt 	};
1022bcd521aSSteven Rostedt };
1032ed84eebSSteven Rostedt 
104134e6a03SSteven Rostedt (VMware) struct ftrace_likely_data {
105134e6a03SSteven Rostedt (VMware) 	struct ftrace_branch_data	data;
106134e6a03SSteven Rostedt (VMware) 	unsigned long			constant;
107134e6a03SSteven Rostedt (VMware) };
108134e6a03SSteven Rostedt (VMware) 
1092ed84eebSSteven Rostedt /*
1102ed84eebSSteven Rostedt  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
1112ed84eebSSteven Rostedt  * to disable branch tracing on a per file basis.
1122ed84eebSSteven Rostedt  */
113d9ad8bc0SBart Van Assche #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
114d9ad8bc0SBart Van Assche     && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
115134e6a03SSteven Rostedt (VMware) void ftrace_likely_update(struct ftrace_likely_data *f, int val,
116d45ae1f7SSteven Rostedt (VMware) 			  int expect, int is_constant);
1171f0d69a9SSteven Rostedt 
1181f0d69a9SSteven Rostedt #define likely_notrace(x)	__builtin_expect(!!(x), 1)
1191f0d69a9SSteven Rostedt #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
1201f0d69a9SSteven Rostedt 
121d45ae1f7SSteven Rostedt (VMware) #define __branch_check__(x, expect, is_constant) ({			\
1221f0d69a9SSteven Rostedt 			int ______r;					\
123134e6a03SSteven Rostedt (VMware) 			static struct ftrace_likely_data		\
1241f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
12545b79749SSteven Rostedt 				__attribute__((section("_ftrace_annotated_branch"))) \
1261f0d69a9SSteven Rostedt 				______f = {				\
127134e6a03SSteven Rostedt (VMware) 				.data.func = __func__,			\
128134e6a03SSteven Rostedt (VMware) 				.data.file = __FILE__,			\
129134e6a03SSteven Rostedt (VMware) 				.data.line = __LINE__,			\
1301f0d69a9SSteven Rostedt 			};						\
131d45ae1f7SSteven Rostedt (VMware) 			______r = __builtin_expect(!!(x), expect);	\
132d45ae1f7SSteven Rostedt (VMware) 			ftrace_likely_update(&______f, ______r,		\
133d45ae1f7SSteven Rostedt (VMware) 					     expect, is_constant);	\
1341f0d69a9SSteven Rostedt 			______r;					\
1351f0d69a9SSteven Rostedt 		})
1361f0d69a9SSteven Rostedt 
1371f0d69a9SSteven Rostedt /*
1381f0d69a9SSteven Rostedt  * Using __builtin_constant_p(x) to ignore cases where the return
1391f0d69a9SSteven Rostedt  * value is always the same.  This idea is taken from a similar patch
1401f0d69a9SSteven Rostedt  * written by Daniel Walker.
1411f0d69a9SSteven Rostedt  */
1421f0d69a9SSteven Rostedt # ifndef likely
143d45ae1f7SSteven Rostedt (VMware) #  define likely(x)	(__branch_check__(x, 1, __builtin_constant_p(x)))
1441f0d69a9SSteven Rostedt # endif
1451f0d69a9SSteven Rostedt # ifndef unlikely
146d45ae1f7SSteven Rostedt (VMware) #  define unlikely(x)	(__branch_check__(x, 0, __builtin_constant_p(x)))
1471f0d69a9SSteven Rostedt # endif
1482bcd521aSSteven Rostedt 
1492bcd521aSSteven Rostedt #ifdef CONFIG_PROFILE_ALL_BRANCHES
1502bcd521aSSteven Rostedt /*
1512bcd521aSSteven Rostedt  * "Define 'is'", Bill Clinton
1522bcd521aSSteven Rostedt  * "Define 'if'", Steven Rostedt
1532bcd521aSSteven Rostedt  */
154ab3c9c68SLinus Torvalds #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
155ab3c9c68SLinus Torvalds #define __trace_if(cond) \
156b33c8ff4SArnd Bergmann 	if (__builtin_constant_p(!!(cond)) ? !!(cond) :			\
1572bcd521aSSteven Rostedt 	({								\
1582bcd521aSSteven Rostedt 		int ______r;						\
1592bcd521aSSteven Rostedt 		static struct ftrace_branch_data			\
1602bcd521aSSteven Rostedt 			__attribute__((__aligned__(4)))			\
1612bcd521aSSteven Rostedt 			__attribute__((section("_ftrace_branch")))	\
1622bcd521aSSteven Rostedt 			______f = {					\
1632bcd521aSSteven Rostedt 				.func = __func__,			\
1642bcd521aSSteven Rostedt 				.file = __FILE__,			\
1652bcd521aSSteven Rostedt 				.line = __LINE__,			\
1662bcd521aSSteven Rostedt 			};						\
1672bcd521aSSteven Rostedt 		______r = !!(cond);					\
16897e7e4f3SWitold Baryluk 		______f.miss_hit[______r]++;					\
1692bcd521aSSteven Rostedt 		______r;						\
1702bcd521aSSteven Rostedt 	}))
1712bcd521aSSteven Rostedt #endif /* CONFIG_PROFILE_ALL_BRANCHES */
1722bcd521aSSteven Rostedt 
1731f0d69a9SSteven Rostedt #else
1741da177e4SLinus Torvalds # define likely(x)	__builtin_expect(!!(x), 1)
1751da177e4SLinus Torvalds # define unlikely(x)	__builtin_expect(!!(x), 0)
1761f0d69a9SSteven Rostedt #endif
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* Optimization barrier */
1791da177e4SLinus Torvalds #ifndef barrier
1801da177e4SLinus Torvalds # define barrier() __memory_barrier()
1811da177e4SLinus Torvalds #endif
1821da177e4SLinus Torvalds 
1837829fb09SDaniel Borkmann #ifndef barrier_data
1847829fb09SDaniel Borkmann # define barrier_data(ptr) barrier()
1857829fb09SDaniel Borkmann #endif
1867829fb09SDaniel Borkmann 
18738938c87SDavid Daney /* Unreachable code */
188*649ea4d5SJosh Poimboeuf #ifdef CONFIG_STACK_VALIDATION
189*649ea4d5SJosh Poimboeuf #define annotate_reachable() ({						\
190*649ea4d5SJosh Poimboeuf 	asm("%c0:\n\t"							\
191*649ea4d5SJosh Poimboeuf 	    ".pushsection .discard.reachable\n\t"			\
192*649ea4d5SJosh Poimboeuf 	    ".long %c0b - .\n\t"					\
193*649ea4d5SJosh Poimboeuf 	    ".popsection\n\t" : : "i" (__LINE__));			\
194*649ea4d5SJosh Poimboeuf })
195*649ea4d5SJosh Poimboeuf #define annotate_unreachable() ({					\
196*649ea4d5SJosh Poimboeuf 	asm("%c0:\n\t"							\
197*649ea4d5SJosh Poimboeuf 	    ".pushsection .discard.unreachable\n\t"			\
198*649ea4d5SJosh Poimboeuf 	    ".long %c0b - .\n\t"					\
199*649ea4d5SJosh Poimboeuf 	    ".popsection\n\t" : : "i" (__LINE__));			\
200*649ea4d5SJosh Poimboeuf })
201*649ea4d5SJosh Poimboeuf #define ASM_UNREACHABLE							\
202*649ea4d5SJosh Poimboeuf 	"999:\n\t"							\
203*649ea4d5SJosh Poimboeuf 	".pushsection .discard.unreachable\n\t"				\
204*649ea4d5SJosh Poimboeuf 	".long 999b - .\n\t"						\
205*649ea4d5SJosh Poimboeuf 	".popsection\n\t"
206*649ea4d5SJosh Poimboeuf #else
207*649ea4d5SJosh Poimboeuf #define annotate_reachable()
208*649ea4d5SJosh Poimboeuf #define annotate_unreachable()
209*649ea4d5SJosh Poimboeuf #endif
210*649ea4d5SJosh Poimboeuf 
211aa5d1b81SKees Cook #ifndef ASM_UNREACHABLE
212aa5d1b81SKees Cook # define ASM_UNREACHABLE
213aa5d1b81SKees Cook #endif
21438938c87SDavid Daney #ifndef unreachable
215*649ea4d5SJosh Poimboeuf # define unreachable() do { annotate_reachable(); do { } while (1); } while (0)
21638938c87SDavid Daney #endif
21738938c87SDavid Daney 
218b67067f1SNicholas Piggin /*
219b67067f1SNicholas Piggin  * KENTRY - kernel entry point
220b67067f1SNicholas Piggin  * This can be used to annotate symbols (functions or data) that are used
221b67067f1SNicholas Piggin  * without their linker symbol being referenced explicitly. For example,
222b67067f1SNicholas Piggin  * interrupt vector handlers, or functions in the kernel image that are found
223b67067f1SNicholas Piggin  * programatically.
224b67067f1SNicholas Piggin  *
225b67067f1SNicholas Piggin  * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those
226b67067f1SNicholas Piggin  * are handled in their own way (with KEEP() in linker scripts).
227b67067f1SNicholas Piggin  *
228b67067f1SNicholas Piggin  * KENTRY can be avoided if the symbols in question are marked as KEEP() in the
229b67067f1SNicholas Piggin  * linker script. For example an architecture could KEEP() its entire
230b67067f1SNicholas Piggin  * boot/exception vector code rather than annotate each function and data.
231b67067f1SNicholas Piggin  */
232b67067f1SNicholas Piggin #ifndef KENTRY
233b67067f1SNicholas Piggin # define KENTRY(sym)						\
234b67067f1SNicholas Piggin 	extern typeof(sym) sym;					\
235b67067f1SNicholas Piggin 	static const unsigned long __kentry_##sym		\
236b67067f1SNicholas Piggin 	__used							\
237b67067f1SNicholas Piggin 	__attribute__((section("___kentry" "+" #sym ), used))	\
238b67067f1SNicholas Piggin 	= (unsigned long)&sym;
239b67067f1SNicholas Piggin #endif
240b67067f1SNicholas Piggin 
2411da177e4SLinus Torvalds #ifndef RELOC_HIDE
2421da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off)					\
2431da177e4SLinus Torvalds   ({ unsigned long __ptr;					\
2441da177e4SLinus Torvalds      __ptr = (unsigned long) (ptr);				\
2451da177e4SLinus Torvalds     (typeof(ptr)) (__ptr + (off)); })
2461da177e4SLinus Torvalds #endif
2471da177e4SLinus Torvalds 
248fe8c8a12SCesar Eduardo Barros #ifndef OPTIMIZER_HIDE_VAR
249fe8c8a12SCesar Eduardo Barros #define OPTIMIZER_HIDE_VAR(var) barrier()
250fe8c8a12SCesar Eduardo Barros #endif
251fe8c8a12SCesar Eduardo Barros 
2526f33d587SRusty Russell /* Not-quite-unique ID. */
2536f33d587SRusty Russell #ifndef __UNIQUE_ID
2546f33d587SRusty Russell # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
2556f33d587SRusty Russell #endif
2566f33d587SRusty Russell 
257230fa253SChristian Borntraeger #include <uapi/linux/types.h>
258230fa253SChristian Borntraeger 
259d976441fSAndrey Ryabinin #define __READ_ONCE_SIZE						\
260d976441fSAndrey Ryabinin ({									\
261d976441fSAndrey Ryabinin 	switch (size) {							\
262d976441fSAndrey Ryabinin 	case 1: *(__u8 *)res = *(volatile __u8 *)p; break;		\
263d976441fSAndrey Ryabinin 	case 2: *(__u16 *)res = *(volatile __u16 *)p; break;		\
264d976441fSAndrey Ryabinin 	case 4: *(__u32 *)res = *(volatile __u32 *)p; break;		\
265d976441fSAndrey Ryabinin 	case 8: *(__u64 *)res = *(volatile __u64 *)p; break;		\
266d976441fSAndrey Ryabinin 	default:							\
267d976441fSAndrey Ryabinin 		barrier();						\
268d976441fSAndrey Ryabinin 		__builtin_memcpy((void *)res, (const void *)p, size);	\
269d976441fSAndrey Ryabinin 		barrier();						\
270d976441fSAndrey Ryabinin 	}								\
271d976441fSAndrey Ryabinin })
272d976441fSAndrey Ryabinin 
273d976441fSAndrey Ryabinin static __always_inline
274d976441fSAndrey Ryabinin void __read_once_size(const volatile void *p, void *res, int size)
275230fa253SChristian Borntraeger {
276d976441fSAndrey Ryabinin 	__READ_ONCE_SIZE;
277230fa253SChristian Borntraeger }
278d976441fSAndrey Ryabinin 
279d976441fSAndrey Ryabinin #ifdef CONFIG_KASAN
280d976441fSAndrey Ryabinin /*
281d976441fSAndrey Ryabinin  * This function is not 'inline' because __no_sanitize_address confilcts
282d976441fSAndrey Ryabinin  * with inlining. Attempt to inline it may cause a build failure.
283d976441fSAndrey Ryabinin  * 	https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
284d976441fSAndrey Ryabinin  * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
285d976441fSAndrey Ryabinin  */
286d976441fSAndrey Ryabinin static __no_sanitize_address __maybe_unused
287d976441fSAndrey Ryabinin void __read_once_size_nocheck(const volatile void *p, void *res, int size)
288d976441fSAndrey Ryabinin {
289d976441fSAndrey Ryabinin 	__READ_ONCE_SIZE;
290230fa253SChristian Borntraeger }
291d976441fSAndrey Ryabinin #else
292d976441fSAndrey Ryabinin static __always_inline
293d976441fSAndrey Ryabinin void __read_once_size_nocheck(const volatile void *p, void *res, int size)
294d976441fSAndrey Ryabinin {
295d976441fSAndrey Ryabinin 	__READ_ONCE_SIZE;
296d976441fSAndrey Ryabinin }
297d976441fSAndrey Ryabinin #endif
298230fa253SChristian Borntraeger 
29943239cbeSChristian Borntraeger static __always_inline void __write_once_size(volatile void *p, void *res, int size)
300230fa253SChristian Borntraeger {
301230fa253SChristian Borntraeger 	switch (size) {
302230fa253SChristian Borntraeger 	case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
303230fa253SChristian Borntraeger 	case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
304230fa253SChristian Borntraeger 	case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
305230fa253SChristian Borntraeger 	case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
306230fa253SChristian Borntraeger 	default:
307230fa253SChristian Borntraeger 		barrier();
308230fa253SChristian Borntraeger 		__builtin_memcpy((void *)p, (const void *)res, size);
309230fa253SChristian Borntraeger 		barrier();
310230fa253SChristian Borntraeger 	}
311230fa253SChristian Borntraeger }
312230fa253SChristian Borntraeger 
313230fa253SChristian Borntraeger /*
314230fa253SChristian Borntraeger  * Prevent the compiler from merging or refetching reads or writes. The
315230fa253SChristian Borntraeger  * compiler is also forbidden from reordering successive instances of
31643239cbeSChristian Borntraeger  * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
317230fa253SChristian Borntraeger  * compiler is aware of some particular ordering.  One way to make the
318230fa253SChristian Borntraeger  * compiler aware of ordering is to put the two invocations of READ_ONCE,
31943239cbeSChristian Borntraeger  * WRITE_ONCE or ACCESS_ONCE() in different C statements.
320230fa253SChristian Borntraeger  *
321230fa253SChristian Borntraeger  * In contrast to ACCESS_ONCE these two macros will also work on aggregate
322230fa253SChristian Borntraeger  * data types like structs or unions. If the size of the accessed data
323230fa253SChristian Borntraeger  * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
324fed0764fSKonrad Rzeszutek Wilk  * READ_ONCE() and WRITE_ONCE() will fall back to memcpy(). There's at
325fed0764fSKonrad Rzeszutek Wilk  * least two memcpy()s: one for the __builtin_memcpy() and then one for
326fed0764fSKonrad Rzeszutek Wilk  * the macro doing the copy of variable - '__u' allocated on the stack.
327230fa253SChristian Borntraeger  *
328230fa253SChristian Borntraeger  * Their two major use cases are: (1) Mediating communication between
329230fa253SChristian Borntraeger  * process-level code and irq/NMI handlers, all running on the same CPU,
330230fa253SChristian Borntraeger  * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
331230fa253SChristian Borntraeger  * mutilate accesses that either do not require ordering or that interact
332230fa253SChristian Borntraeger  * with an explicit memory barrier or atomic instruction that provides the
333230fa253SChristian Borntraeger  * required ordering.
334230fa253SChristian Borntraeger  */
335230fa253SChristian Borntraeger 
336d976441fSAndrey Ryabinin #define __READ_ONCE(x, check)						\
337d976441fSAndrey Ryabinin ({									\
338d976441fSAndrey Ryabinin 	union { typeof(x) __val; char __c[1]; } __u;			\
339d976441fSAndrey Ryabinin 	if (check)							\
340d976441fSAndrey Ryabinin 		__read_once_size(&(x), __u.__c, sizeof(x));		\
341d976441fSAndrey Ryabinin 	else								\
342d976441fSAndrey Ryabinin 		__read_once_size_nocheck(&(x), __u.__c, sizeof(x));	\
343d976441fSAndrey Ryabinin 	__u.__val;							\
344d976441fSAndrey Ryabinin })
345d976441fSAndrey Ryabinin #define READ_ONCE(x) __READ_ONCE(x, 1)
346d976441fSAndrey Ryabinin 
347d976441fSAndrey Ryabinin /*
348d976441fSAndrey Ryabinin  * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
349d976441fSAndrey Ryabinin  * to hide memory access from KASAN.
350d976441fSAndrey Ryabinin  */
351d976441fSAndrey Ryabinin #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
352230fa253SChristian Borntraeger 
35343239cbeSChristian Borntraeger #define WRITE_ONCE(x, val) \
354ba33034fSChristian Borntraeger ({							\
355ba33034fSChristian Borntraeger 	union { typeof(x) __val; char __c[1]; } __u =	\
356ba33034fSChristian Borntraeger 		{ .__val = (__force typeof(x)) (val) }; \
357ba33034fSChristian Borntraeger 	__write_once_size(&(x), __u.__c, sizeof(x));	\
358ba33034fSChristian Borntraeger 	__u.__val;					\
359ba33034fSChristian Borntraeger })
360230fa253SChristian Borntraeger 
3611da177e4SLinus Torvalds #endif /* __KERNEL__ */
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */
3641da177e4SLinus Torvalds 
3654f79c3ffSDavid Woodhouse #ifdef __KERNEL__
3661da177e4SLinus Torvalds /*
3671da177e4SLinus Torvalds  * Allow us to mark functions as 'deprecated' and have gcc emit a nice
3681da177e4SLinus Torvalds  * warning for each use, in hopes of speeding the functions removal.
3691da177e4SLinus Torvalds  * Usage is:
3701da177e4SLinus Torvalds  * 		int __deprecated foo(void)
3711da177e4SLinus Torvalds  */
3721da177e4SLinus Torvalds #ifndef __deprecated
3731da177e4SLinus Torvalds # define __deprecated		/* unimplemented */
3741da177e4SLinus Torvalds #endif
3751da177e4SLinus Torvalds 
376512345beSPaul E. McKenney #ifdef MODULE
377512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated
378512345beSPaul E. McKenney #else
379512345beSPaul E. McKenney #define __deprecated_for_modules
380512345beSPaul E. McKenney #endif
381512345beSPaul E. McKenney 
3821da177e4SLinus Torvalds #ifndef __must_check
3831da177e4SLinus Torvalds #define __must_check
3841da177e4SLinus Torvalds #endif
3851da177e4SLinus Torvalds 
386cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK
387cebc04baSAndrew Morton #undef __must_check
388cebc04baSAndrew Morton #define __must_check
389cebc04baSAndrew Morton #endif
390de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED
391de488443SJeff Garzik #undef __deprecated
392de488443SJeff Garzik #undef __deprecated_for_modules
393de488443SJeff Garzik #define __deprecated
394de488443SJeff Garzik #define __deprecated_for_modules
395de488443SJeff Garzik #endif
396cebc04baSAndrew Morton 
397d64e85d3SRasmus Villemoes #ifndef __malloc
398d64e85d3SRasmus Villemoes #define __malloc
399d64e85d3SRasmus Villemoes #endif
400d64e85d3SRasmus Villemoes 
4011da177e4SLinus Torvalds /*
4021da177e4SLinus Torvalds  * Allow us to avoid 'defined but not used' warnings on functions and data,
4031da177e4SLinus Torvalds  * as well as force them to be emitted to the assembly file.
4041da177e4SLinus Torvalds  *
4050d7ebbbcSDavid Rientjes  * As of gcc 3.4, static functions that are not marked with attribute((used))
4060d7ebbbcSDavid Rientjes  * may be elided from the assembly file.  As of gcc 3.4, static data not so
4071da177e4SLinus Torvalds  * marked will not be elided, but this may change in a future gcc version.
4081da177e4SLinus Torvalds  *
4090d7ebbbcSDavid Rientjes  * NOTE: Because distributions shipped with a backported unit-at-a-time
4100d7ebbbcSDavid Rientjes  * compiler in gcc 3.3, we must define __used to be __attribute__((used))
4110d7ebbbcSDavid Rientjes  * for gcc >=3.3 instead of 3.4.
4120d7ebbbcSDavid Rientjes  *
4131da177e4SLinus Torvalds  * In prior versions of gcc, such functions and data would be emitted, but
4141da177e4SLinus Torvalds  * would be warned about except with attribute((unused)).
4150d7ebbbcSDavid Rientjes  *
4160d7ebbbcSDavid Rientjes  * Mark functions that are referenced only in inline assembly as __used so
4170d7ebbbcSDavid Rientjes  * the code is emitted even though it appears to be unreferenced.
4181da177e4SLinus Torvalds  */
4190d7ebbbcSDavid Rientjes #ifndef __used
4200d7ebbbcSDavid Rientjes # define __used			/* unimplemented */
4210d7ebbbcSDavid Rientjes #endif
4220d7ebbbcSDavid Rientjes 
4230d7ebbbcSDavid Rientjes #ifndef __maybe_unused
4240d7ebbbcSDavid Rientjes # define __maybe_unused		/* unimplemented */
4251da177e4SLinus Torvalds #endif
4261da177e4SLinus Torvalds 
4277b2a3513SLi Zefan #ifndef __always_unused
4287b2a3513SLi Zefan # define __always_unused	/* unimplemented */
4297b2a3513SLi Zefan #endif
4307b2a3513SLi Zefan 
431423bc7b2SDavid Woodhouse #ifndef noinline
432423bc7b2SDavid Woodhouse #define noinline
433423bc7b2SDavid Woodhouse #endif
434423bc7b2SDavid Woodhouse 
435735c4fb9SAndrew Morton /*
436735c4fb9SAndrew Morton  * Rather then using noinline to prevent stack consumption, use
437e6be0c9eSAlexander Stein  * noinline_for_stack instead.  For documentation reasons.
438735c4fb9SAndrew Morton  */
439735c4fb9SAndrew Morton #define noinline_for_stack noinline
440735c4fb9SAndrew Morton 
441423bc7b2SDavid Woodhouse #ifndef __always_inline
442423bc7b2SDavid Woodhouse #define __always_inline inline
443423bc7b2SDavid Woodhouse #endif
444423bc7b2SDavid Woodhouse 
445423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */
446423bc7b2SDavid Woodhouse 
4471da177e4SLinus Torvalds /*
4481da177e4SLinus Torvalds  * From the GCC manual:
4491da177e4SLinus Torvalds  *
4501da177e4SLinus Torvalds  * Many functions do not examine any values except their arguments,
4511da177e4SLinus Torvalds  * and have no effects except the return value.  Basically this is
4521da177e4SLinus Torvalds  * just slightly more strict class than the `pure' attribute above,
4531da177e4SLinus Torvalds  * since function is not allowed to read global memory.
4541da177e4SLinus Torvalds  *
4551da177e4SLinus Torvalds  * Note that a function that has pointer arguments and examines the
4561da177e4SLinus Torvalds  * data pointed to must _not_ be declared `const'.  Likewise, a
4571da177e4SLinus Torvalds  * function that calls a non-`const' function usually must not be
4581da177e4SLinus Torvalds  * `const'.  It does not make sense for a `const' function to return
4591da177e4SLinus Torvalds  * `void'.
4601da177e4SLinus Torvalds  */
4611da177e4SLinus Torvalds #ifndef __attribute_const__
4621da177e4SLinus Torvalds # define __attribute_const__	/* unimplemented */
4631da177e4SLinus Torvalds #endif
4641da177e4SLinus Torvalds 
4650aa5e49cSKees Cook #ifndef __designated_init
4660aa5e49cSKees Cook # define __designated_init
4670aa5e49cSKees Cook #endif
4680aa5e49cSKees Cook 
4690766f788SEmese Revfy #ifndef __latent_entropy
4700766f788SEmese Revfy # define __latent_entropy
4710766f788SEmese Revfy #endif
4720766f788SEmese Revfy 
473313dd1b6SKees Cook #ifndef __randomize_layout
474313dd1b6SKees Cook # define __randomize_layout __designated_init
475313dd1b6SKees Cook #endif
476313dd1b6SKees Cook 
477313dd1b6SKees Cook #ifndef __no_randomize_layout
478313dd1b6SKees Cook # define __no_randomize_layout
479313dd1b6SKees Cook #endif
480313dd1b6SKees Cook 
481a586df06SAndi Kleen /*
482a586df06SAndi Kleen  * Tell gcc if a function is cold. The compiler will assume any path
483a586df06SAndi Kleen  * directly leading to the call is unlikely.
484a586df06SAndi Kleen  */
485a586df06SAndi Kleen 
486a586df06SAndi Kleen #ifndef __cold
487a586df06SAndi Kleen #define __cold
488a586df06SAndi Kleen #endif
489a586df06SAndi Kleen 
490f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */
491f3fe866dSSam Ravnborg #ifndef __section
492f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S)))
493f3fe866dSSam Ravnborg #endif
494f3fe866dSSam Ravnborg 
4959a858dc7SAndi Kleen #ifndef __visible
4969a858dc7SAndi Kleen #define __visible
4979a858dc7SAndi Kleen #endif
4989a858dc7SAndi Kleen 
499a744fd17SRasmus Villemoes /*
500a744fd17SRasmus Villemoes  * Assume alignment of return value.
501a744fd17SRasmus Villemoes  */
502a744fd17SRasmus Villemoes #ifndef __assume_aligned
503a744fd17SRasmus Villemoes #define __assume_aligned(a, ...)
504a744fd17SRasmus Villemoes #endif
505a744fd17SRasmus Villemoes 
506a744fd17SRasmus Villemoes 
507d2c123c2SRusty Russell /* Are two types/vars the same type (ignoring qualifiers)? */
508d2c123c2SRusty Russell #ifndef __same_type
509d2c123c2SRusty Russell # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
510d2c123c2SRusty Russell #endif
511d2c123c2SRusty Russell 
51247933ad4SPeter Zijlstra /* Is this type a native word size -- useful for atomic operations */
51347933ad4SPeter Zijlstra #ifndef __native_word
514536fa402SPaul E. McKenney # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
51547933ad4SPeter Zijlstra #endif
51647933ad4SPeter Zijlstra 
5179f0cf4adSArjan van de Ven /* Compile time object size, -1 for unknown */
5189f0cf4adSArjan van de Ven #ifndef __compiletime_object_size
5199f0cf4adSArjan van de Ven # define __compiletime_object_size(obj) -1
5209f0cf4adSArjan van de Ven #endif
5214a312769SArjan van de Ven #ifndef __compiletime_warning
5224a312769SArjan van de Ven # define __compiletime_warning(message)
5234a312769SArjan van de Ven #endif
52463312b6aSArjan van de Ven #ifndef __compiletime_error
52563312b6aSArjan van de Ven # define __compiletime_error(message)
5262c0d259eSJames Hogan /*
5272c0d259eSJames Hogan  * Sparse complains of variable sized arrays due to the temporary variable in
5282c0d259eSJames Hogan  * __compiletime_assert. Unfortunately we can't just expand it out to make
5292c0d259eSJames Hogan  * sparse see a constant array size without breaking compiletime_assert on old
5302c0d259eSJames Hogan  * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
5312c0d259eSJames Hogan  */
5322c0d259eSJames Hogan # ifndef __CHECKER__
533c361d3e5SDaniel Santos #  define __compiletime_error_fallback(condition) \
5349a8ab1c3SDaniel Santos 	do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
5352c0d259eSJames Hogan # endif
5362c0d259eSJames Hogan #endif
5372c0d259eSJames Hogan #ifndef __compiletime_error_fallback
538c361d3e5SDaniel Santos # define __compiletime_error_fallback(condition) do { } while (0)
53963312b6aSArjan van de Ven #endif
540c361d3e5SDaniel Santos 
5419a8ab1c3SDaniel Santos #define __compiletime_assert(condition, msg, prefix, suffix)		\
5429a8ab1c3SDaniel Santos 	do {								\
5439a8ab1c3SDaniel Santos 		bool __cond = !(condition);				\
5449a8ab1c3SDaniel Santos 		extern void prefix ## suffix(void) __compiletime_error(msg); \
5459a8ab1c3SDaniel Santos 		if (__cond)						\
5469a8ab1c3SDaniel Santos 			prefix ## suffix();				\
5479a8ab1c3SDaniel Santos 		__compiletime_error_fallback(__cond);			\
5489a8ab1c3SDaniel Santos 	} while (0)
5499a8ab1c3SDaniel Santos 
5509a8ab1c3SDaniel Santos #define _compiletime_assert(condition, msg, prefix, suffix) \
5519a8ab1c3SDaniel Santos 	__compiletime_assert(condition, msg, prefix, suffix)
5529a8ab1c3SDaniel Santos 
5539a8ab1c3SDaniel Santos /**
5549a8ab1c3SDaniel Santos  * compiletime_assert - break build and emit msg if condition is false
5559a8ab1c3SDaniel Santos  * @condition: a compile-time constant condition to check
5569a8ab1c3SDaniel Santos  * @msg:       a message to emit if condition is false
5579a8ab1c3SDaniel Santos  *
5589a8ab1c3SDaniel Santos  * In tradition of POSIX assert, this macro will break the build if the
5599a8ab1c3SDaniel Santos  * supplied condition is *false*, emitting the supplied error message if the
5609a8ab1c3SDaniel Santos  * compiler has support to do so.
5619a8ab1c3SDaniel Santos  */
5629a8ab1c3SDaniel Santos #define compiletime_assert(condition, msg) \
5639a8ab1c3SDaniel Santos 	_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
5649a8ab1c3SDaniel Santos 
56547933ad4SPeter Zijlstra #define compiletime_assert_atomic_type(t)				\
56647933ad4SPeter Zijlstra 	compiletime_assert(__native_word(t),				\
56747933ad4SPeter Zijlstra 		"Need native word sized stores/loads for atomicity.")
56847933ad4SPeter Zijlstra 
5699c3cdc1fSLinus Torvalds /*
5709c3cdc1fSLinus Torvalds  * Prevent the compiler from merging or refetching accesses.  The compiler
5719c3cdc1fSLinus Torvalds  * is also forbidden from reordering successive instances of ACCESS_ONCE(),
5729c3cdc1fSLinus Torvalds  * but only when the compiler is aware of some particular ordering.  One way
5739c3cdc1fSLinus Torvalds  * to make the compiler aware of ordering is to put the two invocations of
5749c3cdc1fSLinus Torvalds  * ACCESS_ONCE() in different C statements.
5759c3cdc1fSLinus Torvalds  *
576927609d6SChristian Borntraeger  * ACCESS_ONCE will only work on scalar types. For union types, ACCESS_ONCE
577927609d6SChristian Borntraeger  * on a union member will work as long as the size of the member matches the
578927609d6SChristian Borntraeger  * size of the union and the size is smaller than word size.
579927609d6SChristian Borntraeger  *
580927609d6SChristian Borntraeger  * The major use cases of ACCESS_ONCE used to be (1) Mediating communication
581927609d6SChristian Borntraeger  * between process-level code and irq/NMI handlers, all running on the same CPU,
582927609d6SChristian Borntraeger  * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
583927609d6SChristian Borntraeger  * mutilate accesses that either do not require ordering or that interact
584927609d6SChristian Borntraeger  * with an explicit memory barrier or atomic instruction that provides the
585927609d6SChristian Borntraeger  * required ordering.
586927609d6SChristian Borntraeger  *
587663fdcbeSPreeti U Murthy  * If possible use READ_ONCE()/WRITE_ONCE() instead.
5889c3cdc1fSLinus Torvalds  */
589927609d6SChristian Borntraeger #define __ACCESS_ONCE(x) ({ \
590c5b19946SChristian Borntraeger 	 __maybe_unused typeof(x) __var = (__force typeof(x)) 0; \
591927609d6SChristian Borntraeger 	(volatile typeof(x) *)&(x); })
592927609d6SChristian Borntraeger #define ACCESS_ONCE(x) (*__ACCESS_ONCE(x))
5939c3cdc1fSLinus Torvalds 
5940a04b016SPeter Zijlstra /**
5950a04b016SPeter Zijlstra  * lockless_dereference() - safely load a pointer for later dereference
5960a04b016SPeter Zijlstra  * @p: The pointer to load
5970a04b016SPeter Zijlstra  *
5980a04b016SPeter Zijlstra  * Similar to rcu_dereference(), but for situations where the pointed-to
5990a04b016SPeter Zijlstra  * object's lifetime is managed by something other than RCU.  That
6000a04b016SPeter Zijlstra  * "something other" might be reference counting or simple immortality.
601331b6d8cSPeter Zijlstra  *
602d7127b5eSJohannes Berg  * The seemingly unused variable ___typecheck_p validates that @p is
603d7127b5eSJohannes Berg  * indeed a pointer type by using a pointer to typeof(*p) as the type.
604d7127b5eSJohannes Berg  * Taking a pointer to typeof(*p) again is needed in case p is void *.
6050a04b016SPeter Zijlstra  */
6060a04b016SPeter Zijlstra #define lockless_dereference(p) \
6070a04b016SPeter Zijlstra ({ \
60838183b9cSStephen Rothwell 	typeof(p) _________p1 = READ_ONCE(p); \
609d7127b5eSJohannes Berg 	typeof(*(p)) *___typecheck_p __maybe_unused; \
6100a04b016SPeter Zijlstra 	smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
6110a04b016SPeter Zijlstra 	(_________p1); \
6120a04b016SPeter Zijlstra })
6130a04b016SPeter Zijlstra 
6141da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */
615