xref: /linux/include/linux/compiler.h (revision b67067f1176df6ee727450546b58704e4b588563)
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 #ifdef CONFIG_SPARSE_RCU_POINTER
21ca5ecddfSPaul E. McKenney # define __rcu		__attribute__((noderef, address_space(4)))
22ad315455SBoqun Feng #else /* CONFIG_SPARSE_RCU_POINTER */
2371d1d5c7SPaul E. McKenney # define __rcu
24ad315455SBoqun Feng #endif /* CONFIG_SPARSE_RCU_POINTER */
25ad315455SBoqun Feng # define __private	__attribute__((noderef))
26c47ffe3dSAl Viro extern void __chk_user_ptr(const volatile void __user *);
27c47ffe3dSAl Viro extern void __chk_io_ptr(const volatile void __iomem *);
28ad315455SBoqun Feng # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member))
29ad315455SBoqun Feng #else /* __CHECKER__ */
301da177e4SLinus Torvalds # define __user
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 
1042ed84eebSSteven Rostedt /*
1052ed84eebSSteven Rostedt  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
1062ed84eebSSteven Rostedt  * to disable branch tracing on a per file basis.
1072ed84eebSSteven Rostedt  */
108d9ad8bc0SBart Van Assche #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
109d9ad8bc0SBart Van Assche     && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
1102ed84eebSSteven Rostedt void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
1111f0d69a9SSteven Rostedt 
1121f0d69a9SSteven Rostedt #define likely_notrace(x)	__builtin_expect(!!(x), 1)
1131f0d69a9SSteven Rostedt #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
1141f0d69a9SSteven Rostedt 
11545b79749SSteven Rostedt #define __branch_check__(x, expect) ({					\
1161f0d69a9SSteven Rostedt 			int ______r;					\
1172ed84eebSSteven Rostedt 			static struct ftrace_branch_data		\
1181f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
11945b79749SSteven Rostedt 				__attribute__((section("_ftrace_annotated_branch"))) \
1201f0d69a9SSteven Rostedt 				______f = {				\
1211f0d69a9SSteven Rostedt 				.func = __func__,			\
1221f0d69a9SSteven Rostedt 				.file = __FILE__,			\
1231f0d69a9SSteven Rostedt 				.line = __LINE__,			\
1241f0d69a9SSteven Rostedt 			};						\
1251f0d69a9SSteven Rostedt 			______r = likely_notrace(x);			\
12645b79749SSteven Rostedt 			ftrace_likely_update(&______f, ______r, expect); \
1271f0d69a9SSteven Rostedt 			______r;					\
1281f0d69a9SSteven Rostedt 		})
1291f0d69a9SSteven Rostedt 
1301f0d69a9SSteven Rostedt /*
1311f0d69a9SSteven Rostedt  * Using __builtin_constant_p(x) to ignore cases where the return
1321f0d69a9SSteven Rostedt  * value is always the same.  This idea is taken from a similar patch
1331f0d69a9SSteven Rostedt  * written by Daniel Walker.
1341f0d69a9SSteven Rostedt  */
1351f0d69a9SSteven Rostedt # ifndef likely
13645b79749SSteven Rostedt #  define likely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
1371f0d69a9SSteven Rostedt # endif
1381f0d69a9SSteven Rostedt # ifndef unlikely
13945b79749SSteven Rostedt #  define unlikely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
1401f0d69a9SSteven Rostedt # endif
1412bcd521aSSteven Rostedt 
1422bcd521aSSteven Rostedt #ifdef CONFIG_PROFILE_ALL_BRANCHES
1432bcd521aSSteven Rostedt /*
1442bcd521aSSteven Rostedt  * "Define 'is'", Bill Clinton
1452bcd521aSSteven Rostedt  * "Define 'if'", Steven Rostedt
1462bcd521aSSteven Rostedt  */
147ab3c9c68SLinus Torvalds #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
148ab3c9c68SLinus Torvalds #define __trace_if(cond) \
149b33c8ff4SArnd Bergmann 	if (__builtin_constant_p(!!(cond)) ? !!(cond) :			\
1502bcd521aSSteven Rostedt 	({								\
1512bcd521aSSteven Rostedt 		int ______r;						\
1522bcd521aSSteven Rostedt 		static struct ftrace_branch_data			\
1532bcd521aSSteven Rostedt 			__attribute__((__aligned__(4)))			\
1542bcd521aSSteven Rostedt 			__attribute__((section("_ftrace_branch")))	\
1552bcd521aSSteven Rostedt 			______f = {					\
1562bcd521aSSteven Rostedt 				.func = __func__,			\
1572bcd521aSSteven Rostedt 				.file = __FILE__,			\
1582bcd521aSSteven Rostedt 				.line = __LINE__,			\
1592bcd521aSSteven Rostedt 			};						\
1602bcd521aSSteven Rostedt 		______r = !!(cond);					\
16197e7e4f3SWitold Baryluk 		______f.miss_hit[______r]++;					\
1622bcd521aSSteven Rostedt 		______r;						\
1632bcd521aSSteven Rostedt 	}))
1642bcd521aSSteven Rostedt #endif /* CONFIG_PROFILE_ALL_BRANCHES */
1652bcd521aSSteven Rostedt 
1661f0d69a9SSteven Rostedt #else
1671da177e4SLinus Torvalds # define likely(x)	__builtin_expect(!!(x), 1)
1681da177e4SLinus Torvalds # define unlikely(x)	__builtin_expect(!!(x), 0)
1691f0d69a9SSteven Rostedt #endif
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds /* Optimization barrier */
1721da177e4SLinus Torvalds #ifndef barrier
1731da177e4SLinus Torvalds # define barrier() __memory_barrier()
1741da177e4SLinus Torvalds #endif
1751da177e4SLinus Torvalds 
1767829fb09SDaniel Borkmann #ifndef barrier_data
1777829fb09SDaniel Borkmann # define barrier_data(ptr) barrier()
1787829fb09SDaniel Borkmann #endif
1797829fb09SDaniel Borkmann 
18038938c87SDavid Daney /* Unreachable code */
18138938c87SDavid Daney #ifndef unreachable
18238938c87SDavid Daney # define unreachable() do { } while (1)
18338938c87SDavid Daney #endif
18438938c87SDavid Daney 
185*b67067f1SNicholas Piggin /*
186*b67067f1SNicholas Piggin  * KENTRY - kernel entry point
187*b67067f1SNicholas Piggin  * This can be used to annotate symbols (functions or data) that are used
188*b67067f1SNicholas Piggin  * without their linker symbol being referenced explicitly. For example,
189*b67067f1SNicholas Piggin  * interrupt vector handlers, or functions in the kernel image that are found
190*b67067f1SNicholas Piggin  * programatically.
191*b67067f1SNicholas Piggin  *
192*b67067f1SNicholas Piggin  * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those
193*b67067f1SNicholas Piggin  * are handled in their own way (with KEEP() in linker scripts).
194*b67067f1SNicholas Piggin  *
195*b67067f1SNicholas Piggin  * KENTRY can be avoided if the symbols in question are marked as KEEP() in the
196*b67067f1SNicholas Piggin  * linker script. For example an architecture could KEEP() its entire
197*b67067f1SNicholas Piggin  * boot/exception vector code rather than annotate each function and data.
198*b67067f1SNicholas Piggin  */
199*b67067f1SNicholas Piggin #ifndef KENTRY
200*b67067f1SNicholas Piggin # define KENTRY(sym)						\
201*b67067f1SNicholas Piggin 	extern typeof(sym) sym;					\
202*b67067f1SNicholas Piggin 	static const unsigned long __kentry_##sym		\
203*b67067f1SNicholas Piggin 	__used							\
204*b67067f1SNicholas Piggin 	__attribute__((section("___kentry" "+" #sym ), used))	\
205*b67067f1SNicholas Piggin 	= (unsigned long)&sym;
206*b67067f1SNicholas Piggin #endif
207*b67067f1SNicholas Piggin 
2081da177e4SLinus Torvalds #ifndef RELOC_HIDE
2091da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off)					\
2101da177e4SLinus Torvalds   ({ unsigned long __ptr;					\
2111da177e4SLinus Torvalds      __ptr = (unsigned long) (ptr);				\
2121da177e4SLinus Torvalds     (typeof(ptr)) (__ptr + (off)); })
2131da177e4SLinus Torvalds #endif
2141da177e4SLinus Torvalds 
215fe8c8a12SCesar Eduardo Barros #ifndef OPTIMIZER_HIDE_VAR
216fe8c8a12SCesar Eduardo Barros #define OPTIMIZER_HIDE_VAR(var) barrier()
217fe8c8a12SCesar Eduardo Barros #endif
218fe8c8a12SCesar Eduardo Barros 
2196f33d587SRusty Russell /* Not-quite-unique ID. */
2206f33d587SRusty Russell #ifndef __UNIQUE_ID
2216f33d587SRusty Russell # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
2226f33d587SRusty Russell #endif
2236f33d587SRusty Russell 
224230fa253SChristian Borntraeger #include <uapi/linux/types.h>
225230fa253SChristian Borntraeger 
226d976441fSAndrey Ryabinin #define __READ_ONCE_SIZE						\
227d976441fSAndrey Ryabinin ({									\
228d976441fSAndrey Ryabinin 	switch (size) {							\
229d976441fSAndrey Ryabinin 	case 1: *(__u8 *)res = *(volatile __u8 *)p; break;		\
230d976441fSAndrey Ryabinin 	case 2: *(__u16 *)res = *(volatile __u16 *)p; break;		\
231d976441fSAndrey Ryabinin 	case 4: *(__u32 *)res = *(volatile __u32 *)p; break;		\
232d976441fSAndrey Ryabinin 	case 8: *(__u64 *)res = *(volatile __u64 *)p; break;		\
233d976441fSAndrey Ryabinin 	default:							\
234d976441fSAndrey Ryabinin 		barrier();						\
235d976441fSAndrey Ryabinin 		__builtin_memcpy((void *)res, (const void *)p, size);	\
236d976441fSAndrey Ryabinin 		barrier();						\
237d976441fSAndrey Ryabinin 	}								\
238d976441fSAndrey Ryabinin })
239d976441fSAndrey Ryabinin 
240d976441fSAndrey Ryabinin static __always_inline
241d976441fSAndrey Ryabinin void __read_once_size(const volatile void *p, void *res, int size)
242230fa253SChristian Borntraeger {
243d976441fSAndrey Ryabinin 	__READ_ONCE_SIZE;
244230fa253SChristian Borntraeger }
245d976441fSAndrey Ryabinin 
246d976441fSAndrey Ryabinin #ifdef CONFIG_KASAN
247d976441fSAndrey Ryabinin /*
248d976441fSAndrey Ryabinin  * This function is not 'inline' because __no_sanitize_address confilcts
249d976441fSAndrey Ryabinin  * with inlining. Attempt to inline it may cause a build failure.
250d976441fSAndrey Ryabinin  * 	https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
251d976441fSAndrey Ryabinin  * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
252d976441fSAndrey Ryabinin  */
253d976441fSAndrey Ryabinin static __no_sanitize_address __maybe_unused
254d976441fSAndrey Ryabinin void __read_once_size_nocheck(const volatile void *p, void *res, int size)
255d976441fSAndrey Ryabinin {
256d976441fSAndrey Ryabinin 	__READ_ONCE_SIZE;
257230fa253SChristian Borntraeger }
258d976441fSAndrey Ryabinin #else
259d976441fSAndrey Ryabinin static __always_inline
260d976441fSAndrey Ryabinin void __read_once_size_nocheck(const volatile void *p, void *res, int size)
261d976441fSAndrey Ryabinin {
262d976441fSAndrey Ryabinin 	__READ_ONCE_SIZE;
263d976441fSAndrey Ryabinin }
264d976441fSAndrey Ryabinin #endif
265230fa253SChristian Borntraeger 
26643239cbeSChristian Borntraeger static __always_inline void __write_once_size(volatile void *p, void *res, int size)
267230fa253SChristian Borntraeger {
268230fa253SChristian Borntraeger 	switch (size) {
269230fa253SChristian Borntraeger 	case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
270230fa253SChristian Borntraeger 	case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
271230fa253SChristian Borntraeger 	case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
272230fa253SChristian Borntraeger 	case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
273230fa253SChristian Borntraeger 	default:
274230fa253SChristian Borntraeger 		barrier();
275230fa253SChristian Borntraeger 		__builtin_memcpy((void *)p, (const void *)res, size);
276230fa253SChristian Borntraeger 		barrier();
277230fa253SChristian Borntraeger 	}
278230fa253SChristian Borntraeger }
279230fa253SChristian Borntraeger 
280230fa253SChristian Borntraeger /*
281230fa253SChristian Borntraeger  * Prevent the compiler from merging or refetching reads or writes. The
282230fa253SChristian Borntraeger  * compiler is also forbidden from reordering successive instances of
28343239cbeSChristian Borntraeger  * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
284230fa253SChristian Borntraeger  * compiler is aware of some particular ordering.  One way to make the
285230fa253SChristian Borntraeger  * compiler aware of ordering is to put the two invocations of READ_ONCE,
28643239cbeSChristian Borntraeger  * WRITE_ONCE or ACCESS_ONCE() in different C statements.
287230fa253SChristian Borntraeger  *
288230fa253SChristian Borntraeger  * In contrast to ACCESS_ONCE these two macros will also work on aggregate
289230fa253SChristian Borntraeger  * data types like structs or unions. If the size of the accessed data
290230fa253SChristian Borntraeger  * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
291fed0764fSKonrad Rzeszutek Wilk  * READ_ONCE() and WRITE_ONCE() will fall back to memcpy(). There's at
292fed0764fSKonrad Rzeszutek Wilk  * least two memcpy()s: one for the __builtin_memcpy() and then one for
293fed0764fSKonrad Rzeszutek Wilk  * the macro doing the copy of variable - '__u' allocated on the stack.
294230fa253SChristian Borntraeger  *
295230fa253SChristian Borntraeger  * Their two major use cases are: (1) Mediating communication between
296230fa253SChristian Borntraeger  * process-level code and irq/NMI handlers, all running on the same CPU,
297230fa253SChristian Borntraeger  * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
298230fa253SChristian Borntraeger  * mutilate accesses that either do not require ordering or that interact
299230fa253SChristian Borntraeger  * with an explicit memory barrier or atomic instruction that provides the
300230fa253SChristian Borntraeger  * required ordering.
301230fa253SChristian Borntraeger  */
302230fa253SChristian Borntraeger 
303d976441fSAndrey Ryabinin #define __READ_ONCE(x, check)						\
304d976441fSAndrey Ryabinin ({									\
305d976441fSAndrey Ryabinin 	union { typeof(x) __val; char __c[1]; } __u;			\
306d976441fSAndrey Ryabinin 	if (check)							\
307d976441fSAndrey Ryabinin 		__read_once_size(&(x), __u.__c, sizeof(x));		\
308d976441fSAndrey Ryabinin 	else								\
309d976441fSAndrey Ryabinin 		__read_once_size_nocheck(&(x), __u.__c, sizeof(x));	\
310d976441fSAndrey Ryabinin 	__u.__val;							\
311d976441fSAndrey Ryabinin })
312d976441fSAndrey Ryabinin #define READ_ONCE(x) __READ_ONCE(x, 1)
313d976441fSAndrey Ryabinin 
314d976441fSAndrey Ryabinin /*
315d976441fSAndrey Ryabinin  * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
316d976441fSAndrey Ryabinin  * to hide memory access from KASAN.
317d976441fSAndrey Ryabinin  */
318d976441fSAndrey Ryabinin #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
319230fa253SChristian Borntraeger 
32043239cbeSChristian Borntraeger #define WRITE_ONCE(x, val) \
321ba33034fSChristian Borntraeger ({							\
322ba33034fSChristian Borntraeger 	union { typeof(x) __val; char __c[1]; } __u =	\
323ba33034fSChristian Borntraeger 		{ .__val = (__force typeof(x)) (val) }; \
324ba33034fSChristian Borntraeger 	__write_once_size(&(x), __u.__c, sizeof(x));	\
325ba33034fSChristian Borntraeger 	__u.__val;					\
326ba33034fSChristian Borntraeger })
327230fa253SChristian Borntraeger 
3281da177e4SLinus Torvalds #endif /* __KERNEL__ */
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */
3311da177e4SLinus Torvalds 
3324f79c3ffSDavid Woodhouse #ifdef __KERNEL__
3331da177e4SLinus Torvalds /*
3341da177e4SLinus Torvalds  * Allow us to mark functions as 'deprecated' and have gcc emit a nice
3351da177e4SLinus Torvalds  * warning for each use, in hopes of speeding the functions removal.
3361da177e4SLinus Torvalds  * Usage is:
3371da177e4SLinus Torvalds  * 		int __deprecated foo(void)
3381da177e4SLinus Torvalds  */
3391da177e4SLinus Torvalds #ifndef __deprecated
3401da177e4SLinus Torvalds # define __deprecated		/* unimplemented */
3411da177e4SLinus Torvalds #endif
3421da177e4SLinus Torvalds 
343512345beSPaul E. McKenney #ifdef MODULE
344512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated
345512345beSPaul E. McKenney #else
346512345beSPaul E. McKenney #define __deprecated_for_modules
347512345beSPaul E. McKenney #endif
348512345beSPaul E. McKenney 
3491da177e4SLinus Torvalds #ifndef __must_check
3501da177e4SLinus Torvalds #define __must_check
3511da177e4SLinus Torvalds #endif
3521da177e4SLinus Torvalds 
353cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK
354cebc04baSAndrew Morton #undef __must_check
355cebc04baSAndrew Morton #define __must_check
356cebc04baSAndrew Morton #endif
357de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED
358de488443SJeff Garzik #undef __deprecated
359de488443SJeff Garzik #undef __deprecated_for_modules
360de488443SJeff Garzik #define __deprecated
361de488443SJeff Garzik #define __deprecated_for_modules
362de488443SJeff Garzik #endif
363cebc04baSAndrew Morton 
364d64e85d3SRasmus Villemoes #ifndef __malloc
365d64e85d3SRasmus Villemoes #define __malloc
366d64e85d3SRasmus Villemoes #endif
367d64e85d3SRasmus Villemoes 
3681da177e4SLinus Torvalds /*
3691da177e4SLinus Torvalds  * Allow us to avoid 'defined but not used' warnings on functions and data,
3701da177e4SLinus Torvalds  * as well as force them to be emitted to the assembly file.
3711da177e4SLinus Torvalds  *
3720d7ebbbcSDavid Rientjes  * As of gcc 3.4, static functions that are not marked with attribute((used))
3730d7ebbbcSDavid Rientjes  * may be elided from the assembly file.  As of gcc 3.4, static data not so
3741da177e4SLinus Torvalds  * marked will not be elided, but this may change in a future gcc version.
3751da177e4SLinus Torvalds  *
3760d7ebbbcSDavid Rientjes  * NOTE: Because distributions shipped with a backported unit-at-a-time
3770d7ebbbcSDavid Rientjes  * compiler in gcc 3.3, we must define __used to be __attribute__((used))
3780d7ebbbcSDavid Rientjes  * for gcc >=3.3 instead of 3.4.
3790d7ebbbcSDavid Rientjes  *
3801da177e4SLinus Torvalds  * In prior versions of gcc, such functions and data would be emitted, but
3811da177e4SLinus Torvalds  * would be warned about except with attribute((unused)).
3820d7ebbbcSDavid Rientjes  *
3830d7ebbbcSDavid Rientjes  * Mark functions that are referenced only in inline assembly as __used so
3840d7ebbbcSDavid Rientjes  * the code is emitted even though it appears to be unreferenced.
3851da177e4SLinus Torvalds  */
3860d7ebbbcSDavid Rientjes #ifndef __used
3870d7ebbbcSDavid Rientjes # define __used			/* unimplemented */
3880d7ebbbcSDavid Rientjes #endif
3890d7ebbbcSDavid Rientjes 
3900d7ebbbcSDavid Rientjes #ifndef __maybe_unused
3910d7ebbbcSDavid Rientjes # define __maybe_unused		/* unimplemented */
3921da177e4SLinus Torvalds #endif
3931da177e4SLinus Torvalds 
3947b2a3513SLi Zefan #ifndef __always_unused
3957b2a3513SLi Zefan # define __always_unused	/* unimplemented */
3967b2a3513SLi Zefan #endif
3977b2a3513SLi Zefan 
398423bc7b2SDavid Woodhouse #ifndef noinline
399423bc7b2SDavid Woodhouse #define noinline
400423bc7b2SDavid Woodhouse #endif
401423bc7b2SDavid Woodhouse 
402735c4fb9SAndrew Morton /*
403735c4fb9SAndrew Morton  * Rather then using noinline to prevent stack consumption, use
404e6be0c9eSAlexander Stein  * noinline_for_stack instead.  For documentation reasons.
405735c4fb9SAndrew Morton  */
406735c4fb9SAndrew Morton #define noinline_for_stack noinline
407735c4fb9SAndrew Morton 
408423bc7b2SDavid Woodhouse #ifndef __always_inline
409423bc7b2SDavid Woodhouse #define __always_inline inline
410423bc7b2SDavid Woodhouse #endif
411423bc7b2SDavid Woodhouse 
412423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */
413423bc7b2SDavid Woodhouse 
4141da177e4SLinus Torvalds /*
4151da177e4SLinus Torvalds  * From the GCC manual:
4161da177e4SLinus Torvalds  *
4171da177e4SLinus Torvalds  * Many functions do not examine any values except their arguments,
4181da177e4SLinus Torvalds  * and have no effects except the return value.  Basically this is
4191da177e4SLinus Torvalds  * just slightly more strict class than the `pure' attribute above,
4201da177e4SLinus Torvalds  * since function is not allowed to read global memory.
4211da177e4SLinus Torvalds  *
4221da177e4SLinus Torvalds  * Note that a function that has pointer arguments and examines the
4231da177e4SLinus Torvalds  * data pointed to must _not_ be declared `const'.  Likewise, a
4241da177e4SLinus Torvalds  * function that calls a non-`const' function usually must not be
4251da177e4SLinus Torvalds  * `const'.  It does not make sense for a `const' function to return
4261da177e4SLinus Torvalds  * `void'.
4271da177e4SLinus Torvalds  */
4281da177e4SLinus Torvalds #ifndef __attribute_const__
4291da177e4SLinus Torvalds # define __attribute_const__	/* unimplemented */
4301da177e4SLinus Torvalds #endif
4311da177e4SLinus Torvalds 
432a586df06SAndi Kleen /*
433a586df06SAndi Kleen  * Tell gcc if a function is cold. The compiler will assume any path
434a586df06SAndi Kleen  * directly leading to the call is unlikely.
435a586df06SAndi Kleen  */
436a586df06SAndi Kleen 
437a586df06SAndi Kleen #ifndef __cold
438a586df06SAndi Kleen #define __cold
439a586df06SAndi Kleen #endif
440a586df06SAndi Kleen 
441f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */
442f3fe866dSSam Ravnborg #ifndef __section
443f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S)))
444f3fe866dSSam Ravnborg #endif
445f3fe866dSSam Ravnborg 
4469a858dc7SAndi Kleen #ifndef __visible
4479a858dc7SAndi Kleen #define __visible
4489a858dc7SAndi Kleen #endif
4499a858dc7SAndi Kleen 
450a744fd17SRasmus Villemoes /*
451a744fd17SRasmus Villemoes  * Assume alignment of return value.
452a744fd17SRasmus Villemoes  */
453a744fd17SRasmus Villemoes #ifndef __assume_aligned
454a744fd17SRasmus Villemoes #define __assume_aligned(a, ...)
455a744fd17SRasmus Villemoes #endif
456a744fd17SRasmus Villemoes 
457a744fd17SRasmus Villemoes 
458d2c123c2SRusty Russell /* Are two types/vars the same type (ignoring qualifiers)? */
459d2c123c2SRusty Russell #ifndef __same_type
460d2c123c2SRusty Russell # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
461d2c123c2SRusty Russell #endif
462d2c123c2SRusty Russell 
46347933ad4SPeter Zijlstra /* Is this type a native word size -- useful for atomic operations */
46447933ad4SPeter Zijlstra #ifndef __native_word
465536fa402SPaul E. McKenney # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
46647933ad4SPeter Zijlstra #endif
46747933ad4SPeter Zijlstra 
4689f0cf4adSArjan van de Ven /* Compile time object size, -1 for unknown */
4699f0cf4adSArjan van de Ven #ifndef __compiletime_object_size
4709f0cf4adSArjan van de Ven # define __compiletime_object_size(obj) -1
4719f0cf4adSArjan van de Ven #endif
4724a312769SArjan van de Ven #ifndef __compiletime_warning
4734a312769SArjan van de Ven # define __compiletime_warning(message)
4744a312769SArjan van de Ven #endif
47563312b6aSArjan van de Ven #ifndef __compiletime_error
47663312b6aSArjan van de Ven # define __compiletime_error(message)
4772c0d259eSJames Hogan /*
4782c0d259eSJames Hogan  * Sparse complains of variable sized arrays due to the temporary variable in
4792c0d259eSJames Hogan  * __compiletime_assert. Unfortunately we can't just expand it out to make
4802c0d259eSJames Hogan  * sparse see a constant array size without breaking compiletime_assert on old
4812c0d259eSJames Hogan  * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
4822c0d259eSJames Hogan  */
4832c0d259eSJames Hogan # ifndef __CHECKER__
484c361d3e5SDaniel Santos #  define __compiletime_error_fallback(condition) \
4859a8ab1c3SDaniel Santos 	do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
4862c0d259eSJames Hogan # endif
4872c0d259eSJames Hogan #endif
4882c0d259eSJames Hogan #ifndef __compiletime_error_fallback
489c361d3e5SDaniel Santos # define __compiletime_error_fallback(condition) do { } while (0)
49063312b6aSArjan van de Ven #endif
491c361d3e5SDaniel Santos 
4929a8ab1c3SDaniel Santos #define __compiletime_assert(condition, msg, prefix, suffix)		\
4939a8ab1c3SDaniel Santos 	do {								\
4949a8ab1c3SDaniel Santos 		bool __cond = !(condition);				\
4959a8ab1c3SDaniel Santos 		extern void prefix ## suffix(void) __compiletime_error(msg); \
4969a8ab1c3SDaniel Santos 		if (__cond)						\
4979a8ab1c3SDaniel Santos 			prefix ## suffix();				\
4989a8ab1c3SDaniel Santos 		__compiletime_error_fallback(__cond);			\
4999a8ab1c3SDaniel Santos 	} while (0)
5009a8ab1c3SDaniel Santos 
5019a8ab1c3SDaniel Santos #define _compiletime_assert(condition, msg, prefix, suffix) \
5029a8ab1c3SDaniel Santos 	__compiletime_assert(condition, msg, prefix, suffix)
5039a8ab1c3SDaniel Santos 
5049a8ab1c3SDaniel Santos /**
5059a8ab1c3SDaniel Santos  * compiletime_assert - break build and emit msg if condition is false
5069a8ab1c3SDaniel Santos  * @condition: a compile-time constant condition to check
5079a8ab1c3SDaniel Santos  * @msg:       a message to emit if condition is false
5089a8ab1c3SDaniel Santos  *
5099a8ab1c3SDaniel Santos  * In tradition of POSIX assert, this macro will break the build if the
5109a8ab1c3SDaniel Santos  * supplied condition is *false*, emitting the supplied error message if the
5119a8ab1c3SDaniel Santos  * compiler has support to do so.
5129a8ab1c3SDaniel Santos  */
5139a8ab1c3SDaniel Santos #define compiletime_assert(condition, msg) \
5149a8ab1c3SDaniel Santos 	_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
5159a8ab1c3SDaniel Santos 
51647933ad4SPeter Zijlstra #define compiletime_assert_atomic_type(t)				\
51747933ad4SPeter Zijlstra 	compiletime_assert(__native_word(t),				\
51847933ad4SPeter Zijlstra 		"Need native word sized stores/loads for atomicity.")
51947933ad4SPeter Zijlstra 
5209c3cdc1fSLinus Torvalds /*
5219c3cdc1fSLinus Torvalds  * Prevent the compiler from merging or refetching accesses.  The compiler
5229c3cdc1fSLinus Torvalds  * is also forbidden from reordering successive instances of ACCESS_ONCE(),
5239c3cdc1fSLinus Torvalds  * but only when the compiler is aware of some particular ordering.  One way
5249c3cdc1fSLinus Torvalds  * to make the compiler aware of ordering is to put the two invocations of
5259c3cdc1fSLinus Torvalds  * ACCESS_ONCE() in different C statements.
5269c3cdc1fSLinus Torvalds  *
527927609d6SChristian Borntraeger  * ACCESS_ONCE will only work on scalar types. For union types, ACCESS_ONCE
528927609d6SChristian Borntraeger  * on a union member will work as long as the size of the member matches the
529927609d6SChristian Borntraeger  * size of the union and the size is smaller than word size.
530927609d6SChristian Borntraeger  *
531927609d6SChristian Borntraeger  * The major use cases of ACCESS_ONCE used to be (1) Mediating communication
532927609d6SChristian Borntraeger  * between process-level code and irq/NMI handlers, all running on the same CPU,
533927609d6SChristian Borntraeger  * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
534927609d6SChristian Borntraeger  * mutilate accesses that either do not require ordering or that interact
535927609d6SChristian Borntraeger  * with an explicit memory barrier or atomic instruction that provides the
536927609d6SChristian Borntraeger  * required ordering.
537927609d6SChristian Borntraeger  *
538663fdcbeSPreeti U Murthy  * If possible use READ_ONCE()/WRITE_ONCE() instead.
5399c3cdc1fSLinus Torvalds  */
540927609d6SChristian Borntraeger #define __ACCESS_ONCE(x) ({ \
541c5b19946SChristian Borntraeger 	 __maybe_unused typeof(x) __var = (__force typeof(x)) 0; \
542927609d6SChristian Borntraeger 	(volatile typeof(x) *)&(x); })
543927609d6SChristian Borntraeger #define ACCESS_ONCE(x) (*__ACCESS_ONCE(x))
5449c3cdc1fSLinus Torvalds 
5450a04b016SPeter Zijlstra /**
5460a04b016SPeter Zijlstra  * lockless_dereference() - safely load a pointer for later dereference
5470a04b016SPeter Zijlstra  * @p: The pointer to load
5480a04b016SPeter Zijlstra  *
5490a04b016SPeter Zijlstra  * Similar to rcu_dereference(), but for situations where the pointed-to
5500a04b016SPeter Zijlstra  * object's lifetime is managed by something other than RCU.  That
5510a04b016SPeter Zijlstra  * "something other" might be reference counting or simple immortality.
552331b6d8cSPeter Zijlstra  *
553331b6d8cSPeter Zijlstra  * The seemingly unused void * variable is to validate @p is indeed a pointer
554331b6d8cSPeter Zijlstra  * type. All pointer types silently cast to void *.
5550a04b016SPeter Zijlstra  */
5560a04b016SPeter Zijlstra #define lockless_dereference(p) \
5570a04b016SPeter Zijlstra ({ \
55838183b9cSStephen Rothwell 	typeof(p) _________p1 = READ_ONCE(p); \
559331b6d8cSPeter Zijlstra 	__maybe_unused const void * const _________p2 = _________p1; \
5600a04b016SPeter Zijlstra 	smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
5610a04b016SPeter Zijlstra 	(_________p1); \
5620a04b016SPeter Zijlstra })
5630a04b016SPeter Zijlstra 
564324670b6SMasami Hiramatsu /* Ignore/forbid kprobes attach on very low level functions marked by this attribute: */
565324670b6SMasami Hiramatsu #ifdef CONFIG_KPROBES
566324670b6SMasami Hiramatsu # define __kprobes	__attribute__((__section__(".kprobes.text")))
567376e2424SMasami Hiramatsu # define nokprobe_inline	__always_inline
568324670b6SMasami Hiramatsu #else
569324670b6SMasami Hiramatsu # define __kprobes
570376e2424SMasami Hiramatsu # define nokprobe_inline	inline
571324670b6SMasami Hiramatsu #endif
5721da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */
573