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))) 2031f02455SDan Williams # define __pmem __attribute__((noderef, address_space(5))) 21ca5ecddfSPaul E. McKenney #ifdef CONFIG_SPARSE_RCU_POINTER 22ca5ecddfSPaul E. McKenney # define __rcu __attribute__((noderef, address_space(4))) 23ca5ecddfSPaul E. McKenney #else 2471d1d5c7SPaul E. McKenney # define __rcu 25ca5ecddfSPaul E. McKenney #endif 26c47ffe3dSAl Viro extern void __chk_user_ptr(const volatile void __user *); 27c47ffe3dSAl Viro extern void __chk_io_ptr(const volatile void __iomem *); 281da177e4SLinus Torvalds #else 291da177e4SLinus Torvalds # define __user 301da177e4SLinus Torvalds # define __kernel 311da177e4SLinus Torvalds # define __safe 321da177e4SLinus Torvalds # define __force 331da177e4SLinus Torvalds # define __nocast 341da177e4SLinus Torvalds # define __iomem 351da177e4SLinus Torvalds # define __chk_user_ptr(x) (void)0 361da177e4SLinus Torvalds # define __chk_io_ptr(x) (void)0 371da177e4SLinus Torvalds # define __builtin_warning(x, y...) (1) 388529091eSJosh Triplett # define __must_hold(x) 391da177e4SLinus Torvalds # define __acquires(x) 401da177e4SLinus Torvalds # define __releases(x) 411da177e4SLinus Torvalds # define __acquire(x) (void)0 421da177e4SLinus Torvalds # define __release(x) (void)0 43dcc8e559SJosh Triplett # define __cond_lock(x,c) (c) 44e0fdb0e0SRusty Russell # define __percpu 4571d1d5c7SPaul E. McKenney # define __rcu 4661031952SRoss Zwisler # define __pmem 471da177e4SLinus Torvalds #endif 481da177e4SLinus Torvalds 496f33d587SRusty Russell /* Indirect macros required for expanded argument pasting, eg. __LINE__. */ 506f33d587SRusty Russell #define ___PASTE(a,b) a##b 516f33d587SRusty Russell #define __PASTE(a,b) ___PASTE(a,b) 526f33d587SRusty Russell 531da177e4SLinus Torvalds #ifdef __KERNEL__ 541da177e4SLinus Torvalds 55f153b821SLinus Torvalds #ifdef __GNUC__ 56f153b821SLinus Torvalds #include <linux/compiler-gcc.h> 571da177e4SLinus Torvalds #endif 581da177e4SLinus Torvalds 5961f55214SHeiko Carstens #ifdef CC_USING_HOTPATCH 6061f55214SHeiko Carstens #define notrace __attribute__((hotpatch(0,0))) 6161f55214SHeiko Carstens #else 6228614889SSteven Rostedt #define notrace __attribute__((no_instrument_function)) 6361f55214SHeiko Carstens #endif 6428614889SSteven Rostedt 651da177e4SLinus Torvalds /* Intel compiler defines __GNUC__. So we will overwrite implementations 661da177e4SLinus Torvalds * coming from above header files here 671da177e4SLinus Torvalds */ 681da177e4SLinus Torvalds #ifdef __INTEL_COMPILER 691da177e4SLinus Torvalds # include <linux/compiler-intel.h> 701da177e4SLinus Torvalds #endif 711da177e4SLinus Torvalds 72565cbdc2SMark Charlebois /* Clang compiler defines __GNUC__. So we will overwrite implementations 73565cbdc2SMark Charlebois * coming from above header files here 74565cbdc2SMark Charlebois */ 75565cbdc2SMark Charlebois #ifdef __clang__ 76565cbdc2SMark Charlebois #include <linux/compiler-clang.h> 77565cbdc2SMark Charlebois #endif 78565cbdc2SMark Charlebois 791da177e4SLinus Torvalds /* 801da177e4SLinus Torvalds * Generic compiler-dependent macros required for kernel 811da177e4SLinus Torvalds * build go below this comment. Actual compiler/compiler version 821da177e4SLinus Torvalds * specific implementations come from the above header files 831da177e4SLinus Torvalds */ 841da177e4SLinus Torvalds 852ed84eebSSteven Rostedt struct ftrace_branch_data { 861f0d69a9SSteven Rostedt const char *func; 871f0d69a9SSteven Rostedt const char *file; 881f0d69a9SSteven Rostedt unsigned line; 892bcd521aSSteven Rostedt union { 902bcd521aSSteven Rostedt struct { 911f0d69a9SSteven Rostedt unsigned long correct; 921f0d69a9SSteven Rostedt unsigned long incorrect; 931f0d69a9SSteven Rostedt }; 942bcd521aSSteven Rostedt struct { 952bcd521aSSteven Rostedt unsigned long miss; 962bcd521aSSteven Rostedt unsigned long hit; 972bcd521aSSteven Rostedt }; 9897e7e4f3SWitold Baryluk unsigned long miss_hit[2]; 992bcd521aSSteven Rostedt }; 1002bcd521aSSteven Rostedt }; 1012ed84eebSSteven Rostedt 1022ed84eebSSteven Rostedt /* 1032ed84eebSSteven Rostedt * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code 1042ed84eebSSteven Rostedt * to disable branch tracing on a per file basis. 1052ed84eebSSteven Rostedt */ 106d9ad8bc0SBart Van Assche #if defined(CONFIG_TRACE_BRANCH_PROFILING) \ 107d9ad8bc0SBart Van Assche && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__) 1082ed84eebSSteven Rostedt void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect); 1091f0d69a9SSteven Rostedt 1101f0d69a9SSteven Rostedt #define likely_notrace(x) __builtin_expect(!!(x), 1) 1111f0d69a9SSteven Rostedt #define unlikely_notrace(x) __builtin_expect(!!(x), 0) 1121f0d69a9SSteven Rostedt 11345b79749SSteven Rostedt #define __branch_check__(x, expect) ({ \ 1141f0d69a9SSteven Rostedt int ______r; \ 1152ed84eebSSteven Rostedt static struct ftrace_branch_data \ 1161f0d69a9SSteven Rostedt __attribute__((__aligned__(4))) \ 11745b79749SSteven Rostedt __attribute__((section("_ftrace_annotated_branch"))) \ 1181f0d69a9SSteven Rostedt ______f = { \ 1191f0d69a9SSteven Rostedt .func = __func__, \ 1201f0d69a9SSteven Rostedt .file = __FILE__, \ 1211f0d69a9SSteven Rostedt .line = __LINE__, \ 1221f0d69a9SSteven Rostedt }; \ 1231f0d69a9SSteven Rostedt ______r = likely_notrace(x); \ 12445b79749SSteven Rostedt ftrace_likely_update(&______f, ______r, expect); \ 1251f0d69a9SSteven Rostedt ______r; \ 1261f0d69a9SSteven Rostedt }) 1271f0d69a9SSteven Rostedt 1281f0d69a9SSteven Rostedt /* 1291f0d69a9SSteven Rostedt * Using __builtin_constant_p(x) to ignore cases where the return 1301f0d69a9SSteven Rostedt * value is always the same. This idea is taken from a similar patch 1311f0d69a9SSteven Rostedt * written by Daniel Walker. 1321f0d69a9SSteven Rostedt */ 1331f0d69a9SSteven Rostedt # ifndef likely 13445b79749SSteven Rostedt # define likely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1)) 1351f0d69a9SSteven Rostedt # endif 1361f0d69a9SSteven Rostedt # ifndef unlikely 13745b79749SSteven Rostedt # define unlikely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0)) 1381f0d69a9SSteven Rostedt # endif 1392bcd521aSSteven Rostedt 1402bcd521aSSteven Rostedt #ifdef CONFIG_PROFILE_ALL_BRANCHES 1412bcd521aSSteven Rostedt /* 1422bcd521aSSteven Rostedt * "Define 'is'", Bill Clinton 1432bcd521aSSteven Rostedt * "Define 'if'", Steven Rostedt 1442bcd521aSSteven Rostedt */ 145ab3c9c68SLinus Torvalds #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) ) 146ab3c9c68SLinus Torvalds #define __trace_if(cond) \ 147ab3c9c68SLinus Torvalds if (__builtin_constant_p((cond)) ? !!(cond) : \ 1482bcd521aSSteven Rostedt ({ \ 1492bcd521aSSteven Rostedt int ______r; \ 1502bcd521aSSteven Rostedt static struct ftrace_branch_data \ 1512bcd521aSSteven Rostedt __attribute__((__aligned__(4))) \ 1522bcd521aSSteven Rostedt __attribute__((section("_ftrace_branch"))) \ 1532bcd521aSSteven Rostedt ______f = { \ 1542bcd521aSSteven Rostedt .func = __func__, \ 1552bcd521aSSteven Rostedt .file = __FILE__, \ 1562bcd521aSSteven Rostedt .line = __LINE__, \ 1572bcd521aSSteven Rostedt }; \ 1582bcd521aSSteven Rostedt ______r = !!(cond); \ 15997e7e4f3SWitold Baryluk ______f.miss_hit[______r]++; \ 1602bcd521aSSteven Rostedt ______r; \ 1612bcd521aSSteven Rostedt })) 1622bcd521aSSteven Rostedt #endif /* CONFIG_PROFILE_ALL_BRANCHES */ 1632bcd521aSSteven Rostedt 1641f0d69a9SSteven Rostedt #else 1651da177e4SLinus Torvalds # define likely(x) __builtin_expect(!!(x), 1) 1661da177e4SLinus Torvalds # define unlikely(x) __builtin_expect(!!(x), 0) 1671f0d69a9SSteven Rostedt #endif 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* Optimization barrier */ 1701da177e4SLinus Torvalds #ifndef barrier 1711da177e4SLinus Torvalds # define barrier() __memory_barrier() 1721da177e4SLinus Torvalds #endif 1731da177e4SLinus Torvalds 1747829fb09SDaniel Borkmann #ifndef barrier_data 1757829fb09SDaniel Borkmann # define barrier_data(ptr) barrier() 1767829fb09SDaniel Borkmann #endif 1777829fb09SDaniel Borkmann 17838938c87SDavid Daney /* Unreachable code */ 17938938c87SDavid Daney #ifndef unreachable 18038938c87SDavid Daney # define unreachable() do { } while (1) 18138938c87SDavid Daney #endif 18238938c87SDavid Daney 1831da177e4SLinus Torvalds #ifndef RELOC_HIDE 1841da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off) \ 1851da177e4SLinus Torvalds ({ unsigned long __ptr; \ 1861da177e4SLinus Torvalds __ptr = (unsigned long) (ptr); \ 1871da177e4SLinus Torvalds (typeof(ptr)) (__ptr + (off)); }) 1881da177e4SLinus Torvalds #endif 1891da177e4SLinus Torvalds 190fe8c8a12SCesar Eduardo Barros #ifndef OPTIMIZER_HIDE_VAR 191fe8c8a12SCesar Eduardo Barros #define OPTIMIZER_HIDE_VAR(var) barrier() 192fe8c8a12SCesar Eduardo Barros #endif 193fe8c8a12SCesar Eduardo Barros 1946f33d587SRusty Russell /* Not-quite-unique ID. */ 1956f33d587SRusty Russell #ifndef __UNIQUE_ID 1966f33d587SRusty Russell # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__) 1976f33d587SRusty Russell #endif 1986f33d587SRusty Russell 199230fa253SChristian Borntraeger #include <uapi/linux/types.h> 200230fa253SChristian Borntraeger 201*d976441fSAndrey Ryabinin #define __READ_ONCE_SIZE \ 202*d976441fSAndrey Ryabinin ({ \ 203*d976441fSAndrey Ryabinin switch (size) { \ 204*d976441fSAndrey Ryabinin case 1: *(__u8 *)res = *(volatile __u8 *)p; break; \ 205*d976441fSAndrey Ryabinin case 2: *(__u16 *)res = *(volatile __u16 *)p; break; \ 206*d976441fSAndrey Ryabinin case 4: *(__u32 *)res = *(volatile __u32 *)p; break; \ 207*d976441fSAndrey Ryabinin case 8: *(__u64 *)res = *(volatile __u64 *)p; break; \ 208*d976441fSAndrey Ryabinin default: \ 209*d976441fSAndrey Ryabinin barrier(); \ 210*d976441fSAndrey Ryabinin __builtin_memcpy((void *)res, (const void *)p, size); \ 211*d976441fSAndrey Ryabinin barrier(); \ 212*d976441fSAndrey Ryabinin } \ 213*d976441fSAndrey Ryabinin }) 214*d976441fSAndrey Ryabinin 215*d976441fSAndrey Ryabinin static __always_inline 216*d976441fSAndrey Ryabinin void __read_once_size(const volatile void *p, void *res, int size) 217230fa253SChristian Borntraeger { 218*d976441fSAndrey Ryabinin __READ_ONCE_SIZE; 219230fa253SChristian Borntraeger } 220*d976441fSAndrey Ryabinin 221*d976441fSAndrey Ryabinin #ifdef CONFIG_KASAN 222*d976441fSAndrey Ryabinin /* 223*d976441fSAndrey Ryabinin * This function is not 'inline' because __no_sanitize_address confilcts 224*d976441fSAndrey Ryabinin * with inlining. Attempt to inline it may cause a build failure. 225*d976441fSAndrey Ryabinin * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368 226*d976441fSAndrey Ryabinin * '__maybe_unused' allows us to avoid defined-but-not-used warnings. 227*d976441fSAndrey Ryabinin */ 228*d976441fSAndrey Ryabinin static __no_sanitize_address __maybe_unused 229*d976441fSAndrey Ryabinin void __read_once_size_nocheck(const volatile void *p, void *res, int size) 230*d976441fSAndrey Ryabinin { 231*d976441fSAndrey Ryabinin __READ_ONCE_SIZE; 232230fa253SChristian Borntraeger } 233*d976441fSAndrey Ryabinin #else 234*d976441fSAndrey Ryabinin static __always_inline 235*d976441fSAndrey Ryabinin void __read_once_size_nocheck(const volatile void *p, void *res, int size) 236*d976441fSAndrey Ryabinin { 237*d976441fSAndrey Ryabinin __READ_ONCE_SIZE; 238*d976441fSAndrey Ryabinin } 239*d976441fSAndrey Ryabinin #endif 240230fa253SChristian Borntraeger 24143239cbeSChristian Borntraeger static __always_inline void __write_once_size(volatile void *p, void *res, int size) 242230fa253SChristian Borntraeger { 243230fa253SChristian Borntraeger switch (size) { 244230fa253SChristian Borntraeger case 1: *(volatile __u8 *)p = *(__u8 *)res; break; 245230fa253SChristian Borntraeger case 2: *(volatile __u16 *)p = *(__u16 *)res; break; 246230fa253SChristian Borntraeger case 4: *(volatile __u32 *)p = *(__u32 *)res; break; 247230fa253SChristian Borntraeger case 8: *(volatile __u64 *)p = *(__u64 *)res; break; 248230fa253SChristian Borntraeger default: 249230fa253SChristian Borntraeger barrier(); 250230fa253SChristian Borntraeger __builtin_memcpy((void *)p, (const void *)res, size); 251230fa253SChristian Borntraeger barrier(); 252230fa253SChristian Borntraeger } 253230fa253SChristian Borntraeger } 254230fa253SChristian Borntraeger 255230fa253SChristian Borntraeger /* 256230fa253SChristian Borntraeger * Prevent the compiler from merging or refetching reads or writes. The 257230fa253SChristian Borntraeger * compiler is also forbidden from reordering successive instances of 25843239cbeSChristian Borntraeger * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the 259230fa253SChristian Borntraeger * compiler is aware of some particular ordering. One way to make the 260230fa253SChristian Borntraeger * compiler aware of ordering is to put the two invocations of READ_ONCE, 26143239cbeSChristian Borntraeger * WRITE_ONCE or ACCESS_ONCE() in different C statements. 262230fa253SChristian Borntraeger * 263230fa253SChristian Borntraeger * In contrast to ACCESS_ONCE these two macros will also work on aggregate 264230fa253SChristian Borntraeger * data types like structs or unions. If the size of the accessed data 265230fa253SChristian Borntraeger * type exceeds the word size of the machine (e.g., 32 bits or 64 bits) 26643239cbeSChristian Borntraeger * READ_ONCE() and WRITE_ONCE() will fall back to memcpy and print a 267230fa253SChristian Borntraeger * compile-time warning. 268230fa253SChristian Borntraeger * 269230fa253SChristian Borntraeger * Their two major use cases are: (1) Mediating communication between 270230fa253SChristian Borntraeger * process-level code and irq/NMI handlers, all running on the same CPU, 271230fa253SChristian Borntraeger * and (2) Ensuring that the compiler does not fold, spindle, or otherwise 272230fa253SChristian Borntraeger * mutilate accesses that either do not require ordering or that interact 273230fa253SChristian Borntraeger * with an explicit memory barrier or atomic instruction that provides the 274230fa253SChristian Borntraeger * required ordering. 275230fa253SChristian Borntraeger */ 276230fa253SChristian Borntraeger 277*d976441fSAndrey Ryabinin #define __READ_ONCE(x, check) \ 278*d976441fSAndrey Ryabinin ({ \ 279*d976441fSAndrey Ryabinin union { typeof(x) __val; char __c[1]; } __u; \ 280*d976441fSAndrey Ryabinin if (check) \ 281*d976441fSAndrey Ryabinin __read_once_size(&(x), __u.__c, sizeof(x)); \ 282*d976441fSAndrey Ryabinin else \ 283*d976441fSAndrey Ryabinin __read_once_size_nocheck(&(x), __u.__c, sizeof(x)); \ 284*d976441fSAndrey Ryabinin __u.__val; \ 285*d976441fSAndrey Ryabinin }) 286*d976441fSAndrey Ryabinin #define READ_ONCE(x) __READ_ONCE(x, 1) 287*d976441fSAndrey Ryabinin 288*d976441fSAndrey Ryabinin /* 289*d976441fSAndrey Ryabinin * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need 290*d976441fSAndrey Ryabinin * to hide memory access from KASAN. 291*d976441fSAndrey Ryabinin */ 292*d976441fSAndrey Ryabinin #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0) 293230fa253SChristian Borntraeger 29443239cbeSChristian Borntraeger #define WRITE_ONCE(x, val) \ 295ba33034fSChristian Borntraeger ({ \ 296ba33034fSChristian Borntraeger union { typeof(x) __val; char __c[1]; } __u = \ 297ba33034fSChristian Borntraeger { .__val = (__force typeof(x)) (val) }; \ 298ba33034fSChristian Borntraeger __write_once_size(&(x), __u.__c, sizeof(x)); \ 299ba33034fSChristian Borntraeger __u.__val; \ 300ba33034fSChristian Borntraeger }) 301230fa253SChristian Borntraeger 3025af4692aSPaul E. McKenney /** 3035af4692aSPaul E. McKenney * READ_ONCE_CTRL - Read a value heading a control dependency 3045af4692aSPaul E. McKenney * @x: The value to be read, heading the control dependency 3055af4692aSPaul E. McKenney * 3065af4692aSPaul E. McKenney * Control dependencies are tricky. See Documentation/memory-barriers.txt 3075af4692aSPaul E. McKenney * for important information on how to use them. Note that in many cases, 3085af4692aSPaul E. McKenney * use of smp_load_acquire() will be much simpler. Control dependencies 3095af4692aSPaul E. McKenney * should be avoided except on the hottest of hotpaths. 3105af4692aSPaul E. McKenney */ 3115af4692aSPaul E. McKenney #define READ_ONCE_CTRL(x) \ 3125af4692aSPaul E. McKenney ({ \ 3135af4692aSPaul E. McKenney typeof(x) __val = READ_ONCE(x); \ 3145af4692aSPaul E. McKenney smp_read_barrier_depends(); /* Enforce control dependency. */ \ 3155af4692aSPaul E. McKenney __val; \ 3165af4692aSPaul E. McKenney }) 3175af4692aSPaul E. McKenney 3181da177e4SLinus Torvalds #endif /* __KERNEL__ */ 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */ 3211da177e4SLinus Torvalds 3224f79c3ffSDavid Woodhouse #ifdef __KERNEL__ 3231da177e4SLinus Torvalds /* 3241da177e4SLinus Torvalds * Allow us to mark functions as 'deprecated' and have gcc emit a nice 3251da177e4SLinus Torvalds * warning for each use, in hopes of speeding the functions removal. 3261da177e4SLinus Torvalds * Usage is: 3271da177e4SLinus Torvalds * int __deprecated foo(void) 3281da177e4SLinus Torvalds */ 3291da177e4SLinus Torvalds #ifndef __deprecated 3301da177e4SLinus Torvalds # define __deprecated /* unimplemented */ 3311da177e4SLinus Torvalds #endif 3321da177e4SLinus Torvalds 333512345beSPaul E. McKenney #ifdef MODULE 334512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated 335512345beSPaul E. McKenney #else 336512345beSPaul E. McKenney #define __deprecated_for_modules 337512345beSPaul E. McKenney #endif 338512345beSPaul E. McKenney 3391da177e4SLinus Torvalds #ifndef __must_check 3401da177e4SLinus Torvalds #define __must_check 3411da177e4SLinus Torvalds #endif 3421da177e4SLinus Torvalds 343cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK 344cebc04baSAndrew Morton #undef __must_check 345cebc04baSAndrew Morton #define __must_check 346cebc04baSAndrew Morton #endif 347de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED 348de488443SJeff Garzik #undef __deprecated 349de488443SJeff Garzik #undef __deprecated_for_modules 350de488443SJeff Garzik #define __deprecated 351de488443SJeff Garzik #define __deprecated_for_modules 352de488443SJeff Garzik #endif 353cebc04baSAndrew Morton 3541da177e4SLinus Torvalds /* 3551da177e4SLinus Torvalds * Allow us to avoid 'defined but not used' warnings on functions and data, 3561da177e4SLinus Torvalds * as well as force them to be emitted to the assembly file. 3571da177e4SLinus Torvalds * 3580d7ebbbcSDavid Rientjes * As of gcc 3.4, static functions that are not marked with attribute((used)) 3590d7ebbbcSDavid Rientjes * may be elided from the assembly file. As of gcc 3.4, static data not so 3601da177e4SLinus Torvalds * marked will not be elided, but this may change in a future gcc version. 3611da177e4SLinus Torvalds * 3620d7ebbbcSDavid Rientjes * NOTE: Because distributions shipped with a backported unit-at-a-time 3630d7ebbbcSDavid Rientjes * compiler in gcc 3.3, we must define __used to be __attribute__((used)) 3640d7ebbbcSDavid Rientjes * for gcc >=3.3 instead of 3.4. 3650d7ebbbcSDavid Rientjes * 3661da177e4SLinus Torvalds * In prior versions of gcc, such functions and data would be emitted, but 3671da177e4SLinus Torvalds * would be warned about except with attribute((unused)). 3680d7ebbbcSDavid Rientjes * 3690d7ebbbcSDavid Rientjes * Mark functions that are referenced only in inline assembly as __used so 3700d7ebbbcSDavid Rientjes * the code is emitted even though it appears to be unreferenced. 3711da177e4SLinus Torvalds */ 3720d7ebbbcSDavid Rientjes #ifndef __used 3730d7ebbbcSDavid Rientjes # define __used /* unimplemented */ 3740d7ebbbcSDavid Rientjes #endif 3750d7ebbbcSDavid Rientjes 3760d7ebbbcSDavid Rientjes #ifndef __maybe_unused 3770d7ebbbcSDavid Rientjes # define __maybe_unused /* unimplemented */ 3781da177e4SLinus Torvalds #endif 3791da177e4SLinus Torvalds 3807b2a3513SLi Zefan #ifndef __always_unused 3817b2a3513SLi Zefan # define __always_unused /* unimplemented */ 3827b2a3513SLi Zefan #endif 3837b2a3513SLi Zefan 384423bc7b2SDavid Woodhouse #ifndef noinline 385423bc7b2SDavid Woodhouse #define noinline 386423bc7b2SDavid Woodhouse #endif 387423bc7b2SDavid Woodhouse 388735c4fb9SAndrew Morton /* 389735c4fb9SAndrew Morton * Rather then using noinline to prevent stack consumption, use 390e6be0c9eSAlexander Stein * noinline_for_stack instead. For documentation reasons. 391735c4fb9SAndrew Morton */ 392735c4fb9SAndrew Morton #define noinline_for_stack noinline 393735c4fb9SAndrew Morton 394423bc7b2SDavid Woodhouse #ifndef __always_inline 395423bc7b2SDavid Woodhouse #define __always_inline inline 396423bc7b2SDavid Woodhouse #endif 397423bc7b2SDavid Woodhouse 398423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */ 399423bc7b2SDavid Woodhouse 4001da177e4SLinus Torvalds /* 4011da177e4SLinus Torvalds * From the GCC manual: 4021da177e4SLinus Torvalds * 4031da177e4SLinus Torvalds * Many functions do not examine any values except their arguments, 4041da177e4SLinus Torvalds * and have no effects except the return value. Basically this is 4051da177e4SLinus Torvalds * just slightly more strict class than the `pure' attribute above, 4061da177e4SLinus Torvalds * since function is not allowed to read global memory. 4071da177e4SLinus Torvalds * 4081da177e4SLinus Torvalds * Note that a function that has pointer arguments and examines the 4091da177e4SLinus Torvalds * data pointed to must _not_ be declared `const'. Likewise, a 4101da177e4SLinus Torvalds * function that calls a non-`const' function usually must not be 4111da177e4SLinus Torvalds * `const'. It does not make sense for a `const' function to return 4121da177e4SLinus Torvalds * `void'. 4131da177e4SLinus Torvalds */ 4141da177e4SLinus Torvalds #ifndef __attribute_const__ 4151da177e4SLinus Torvalds # define __attribute_const__ /* unimplemented */ 4161da177e4SLinus Torvalds #endif 4171da177e4SLinus Torvalds 418a586df06SAndi Kleen /* 419a586df06SAndi Kleen * Tell gcc if a function is cold. The compiler will assume any path 420a586df06SAndi Kleen * directly leading to the call is unlikely. 421a586df06SAndi Kleen */ 422a586df06SAndi Kleen 423a586df06SAndi Kleen #ifndef __cold 424a586df06SAndi Kleen #define __cold 425a586df06SAndi Kleen #endif 426a586df06SAndi Kleen 427f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */ 428f3fe866dSSam Ravnborg #ifndef __section 429f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S))) 430f3fe866dSSam Ravnborg #endif 431f3fe866dSSam Ravnborg 4329a858dc7SAndi Kleen #ifndef __visible 4339a858dc7SAndi Kleen #define __visible 4349a858dc7SAndi Kleen #endif 4359a858dc7SAndi Kleen 436d2c123c2SRusty Russell /* Are two types/vars the same type (ignoring qualifiers)? */ 437d2c123c2SRusty Russell #ifndef __same_type 438d2c123c2SRusty Russell # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) 439d2c123c2SRusty Russell #endif 440d2c123c2SRusty Russell 44147933ad4SPeter Zijlstra /* Is this type a native word size -- useful for atomic operations */ 44247933ad4SPeter Zijlstra #ifndef __native_word 443536fa402SPaul E. McKenney # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long)) 44447933ad4SPeter Zijlstra #endif 44547933ad4SPeter Zijlstra 4469f0cf4adSArjan van de Ven /* Compile time object size, -1 for unknown */ 4479f0cf4adSArjan van de Ven #ifndef __compiletime_object_size 4489f0cf4adSArjan van de Ven # define __compiletime_object_size(obj) -1 4499f0cf4adSArjan van de Ven #endif 4504a312769SArjan van de Ven #ifndef __compiletime_warning 4514a312769SArjan van de Ven # define __compiletime_warning(message) 4524a312769SArjan van de Ven #endif 45363312b6aSArjan van de Ven #ifndef __compiletime_error 45463312b6aSArjan van de Ven # define __compiletime_error(message) 4552c0d259eSJames Hogan /* 4562c0d259eSJames Hogan * Sparse complains of variable sized arrays due to the temporary variable in 4572c0d259eSJames Hogan * __compiletime_assert. Unfortunately we can't just expand it out to make 4582c0d259eSJames Hogan * sparse see a constant array size without breaking compiletime_assert on old 4592c0d259eSJames Hogan * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether. 4602c0d259eSJames Hogan */ 4612c0d259eSJames Hogan # ifndef __CHECKER__ 462c361d3e5SDaniel Santos # define __compiletime_error_fallback(condition) \ 4639a8ab1c3SDaniel Santos do { ((void)sizeof(char[1 - 2 * condition])); } while (0) 4642c0d259eSJames Hogan # endif 4652c0d259eSJames Hogan #endif 4662c0d259eSJames Hogan #ifndef __compiletime_error_fallback 467c361d3e5SDaniel Santos # define __compiletime_error_fallback(condition) do { } while (0) 46863312b6aSArjan van de Ven #endif 469c361d3e5SDaniel Santos 4709a8ab1c3SDaniel Santos #define __compiletime_assert(condition, msg, prefix, suffix) \ 4719a8ab1c3SDaniel Santos do { \ 4729a8ab1c3SDaniel Santos bool __cond = !(condition); \ 4739a8ab1c3SDaniel Santos extern void prefix ## suffix(void) __compiletime_error(msg); \ 4749a8ab1c3SDaniel Santos if (__cond) \ 4759a8ab1c3SDaniel Santos prefix ## suffix(); \ 4769a8ab1c3SDaniel Santos __compiletime_error_fallback(__cond); \ 4779a8ab1c3SDaniel Santos } while (0) 4789a8ab1c3SDaniel Santos 4799a8ab1c3SDaniel Santos #define _compiletime_assert(condition, msg, prefix, suffix) \ 4809a8ab1c3SDaniel Santos __compiletime_assert(condition, msg, prefix, suffix) 4819a8ab1c3SDaniel Santos 4829a8ab1c3SDaniel Santos /** 4839a8ab1c3SDaniel Santos * compiletime_assert - break build and emit msg if condition is false 4849a8ab1c3SDaniel Santos * @condition: a compile-time constant condition to check 4859a8ab1c3SDaniel Santos * @msg: a message to emit if condition is false 4869a8ab1c3SDaniel Santos * 4879a8ab1c3SDaniel Santos * In tradition of POSIX assert, this macro will break the build if the 4889a8ab1c3SDaniel Santos * supplied condition is *false*, emitting the supplied error message if the 4899a8ab1c3SDaniel Santos * compiler has support to do so. 4909a8ab1c3SDaniel Santos */ 4919a8ab1c3SDaniel Santos #define compiletime_assert(condition, msg) \ 4929a8ab1c3SDaniel Santos _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__) 4939a8ab1c3SDaniel Santos 49447933ad4SPeter Zijlstra #define compiletime_assert_atomic_type(t) \ 49547933ad4SPeter Zijlstra compiletime_assert(__native_word(t), \ 49647933ad4SPeter Zijlstra "Need native word sized stores/loads for atomicity.") 49747933ad4SPeter Zijlstra 4989c3cdc1fSLinus Torvalds /* 4999c3cdc1fSLinus Torvalds * Prevent the compiler from merging or refetching accesses. The compiler 5009c3cdc1fSLinus Torvalds * is also forbidden from reordering successive instances of ACCESS_ONCE(), 5019c3cdc1fSLinus Torvalds * but only when the compiler is aware of some particular ordering. One way 5029c3cdc1fSLinus Torvalds * to make the compiler aware of ordering is to put the two invocations of 5039c3cdc1fSLinus Torvalds * ACCESS_ONCE() in different C statements. 5049c3cdc1fSLinus Torvalds * 505927609d6SChristian Borntraeger * ACCESS_ONCE will only work on scalar types. For union types, ACCESS_ONCE 506927609d6SChristian Borntraeger * on a union member will work as long as the size of the member matches the 507927609d6SChristian Borntraeger * size of the union and the size is smaller than word size. 508927609d6SChristian Borntraeger * 509927609d6SChristian Borntraeger * The major use cases of ACCESS_ONCE used to be (1) Mediating communication 510927609d6SChristian Borntraeger * between process-level code and irq/NMI handlers, all running on the same CPU, 511927609d6SChristian Borntraeger * and (2) Ensuring that the compiler does not fold, spindle, or otherwise 512927609d6SChristian Borntraeger * mutilate accesses that either do not require ordering or that interact 513927609d6SChristian Borntraeger * with an explicit memory barrier or atomic instruction that provides the 514927609d6SChristian Borntraeger * required ordering. 515927609d6SChristian Borntraeger * 516663fdcbeSPreeti U Murthy * If possible use READ_ONCE()/WRITE_ONCE() instead. 5179c3cdc1fSLinus Torvalds */ 518927609d6SChristian Borntraeger #define __ACCESS_ONCE(x) ({ \ 519c5b19946SChristian Borntraeger __maybe_unused typeof(x) __var = (__force typeof(x)) 0; \ 520927609d6SChristian Borntraeger (volatile typeof(x) *)&(x); }) 521927609d6SChristian Borntraeger #define ACCESS_ONCE(x) (*__ACCESS_ONCE(x)) 5229c3cdc1fSLinus Torvalds 5230a04b016SPeter Zijlstra /** 5240a04b016SPeter Zijlstra * lockless_dereference() - safely load a pointer for later dereference 5250a04b016SPeter Zijlstra * @p: The pointer to load 5260a04b016SPeter Zijlstra * 5270a04b016SPeter Zijlstra * Similar to rcu_dereference(), but for situations where the pointed-to 5280a04b016SPeter Zijlstra * object's lifetime is managed by something other than RCU. That 5290a04b016SPeter Zijlstra * "something other" might be reference counting or simple immortality. 5300a04b016SPeter Zijlstra */ 5310a04b016SPeter Zijlstra #define lockless_dereference(p) \ 5320a04b016SPeter Zijlstra ({ \ 53338183b9cSStephen Rothwell typeof(p) _________p1 = READ_ONCE(p); \ 5340a04b016SPeter Zijlstra smp_read_barrier_depends(); /* Dependency order vs. p above. */ \ 5350a04b016SPeter Zijlstra (_________p1); \ 5360a04b016SPeter Zijlstra }) 5370a04b016SPeter Zijlstra 538324670b6SMasami Hiramatsu /* Ignore/forbid kprobes attach on very low level functions marked by this attribute: */ 539324670b6SMasami Hiramatsu #ifdef CONFIG_KPROBES 540324670b6SMasami Hiramatsu # define __kprobes __attribute__((__section__(".kprobes.text"))) 541376e2424SMasami Hiramatsu # define nokprobe_inline __always_inline 542324670b6SMasami Hiramatsu #else 543324670b6SMasami Hiramatsu # define __kprobes 544376e2424SMasami Hiramatsu # define nokprobe_inline inline 545324670b6SMasami Hiramatsu #endif 5461da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */ 547