compiler.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. #ifndef __LINUX_COMPILER_H
  2. #define __LINUX_COMPILER_H
  3. #ifndef __ASSEMBLY__
  4. #ifdef __CHECKER__
  5. # define __user __attribute__((noderef, address_space(1)))
  6. # define __kernel __attribute__((address_space(0)))
  7. # define __safe __attribute__((safe))
  8. # define __force __attribute__((force))
  9. # define __nocast __attribute__((nocast))
  10. # define __iomem __attribute__((noderef, address_space(2)))
  11. # define __must_hold(x) __attribute__((context(x,1,1)))
  12. # define __acquires(x) __attribute__((context(x,0,1)))
  13. # define __releases(x) __attribute__((context(x,1,0)))
  14. # define __acquire(x) __context__(x,1)
  15. # define __release(x) __context__(x,-1)
  16. # define __cond_lock(x,c) ((c) ? ({ __acquire(x); 1; }) : 0)
  17. # define __percpu __attribute__((noderef, address_space(3)))
  18. #ifdef CONFIG_SPARSE_RCU_POINTER
  19. # define __rcu __attribute__((noderef, address_space(4)))
  20. #else /* CONFIG_SPARSE_RCU_POINTER */
  21. # define __rcu
  22. #endif /* CONFIG_SPARSE_RCU_POINTER */
  23. # define __private __attribute__((noderef))
  24. extern void __chk_user_ptr(const volatile void __user *);
  25. extern void __chk_io_ptr(const volatile void __iomem *);
  26. # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member))
  27. #else /* __CHECKER__ */
  28. # ifdef STRUCTLEAK_PLUGIN
  29. # define __user __attribute__((user))
  30. # else
  31. # define __user
  32. # endif
  33. # define __kernel
  34. # define __safe
  35. # define __force
  36. # define __nocast
  37. # define __iomem
  38. # define __chk_user_ptr(x) (void)0
  39. # define __chk_io_ptr(x) (void)0
  40. # define __builtin_warning(x, y...) (1)
  41. # define __must_hold(x)
  42. # define __acquires(x)
  43. # define __releases(x)
  44. # define __acquire(x) (void)0
  45. # define __release(x) (void)0
  46. # define __cond_lock(x,c) (c)
  47. # define __percpu
  48. # define __rcu
  49. # define __private
  50. # define ACCESS_PRIVATE(p, member) ((p)->member)
  51. #endif /* __CHECKER__ */
  52. /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
  53. #define ___PASTE(a,b) a##b
  54. #define __PASTE(a,b) ___PASTE(a,b)
  55. #ifdef __KERNEL__
  56. #ifdef __GNUC__
  57. #include <linux/compiler-gcc.h>
  58. #endif
  59. #if defined(CC_USING_HOTPATCH) && !defined(__CHECKER__)
  60. #define notrace __attribute__((hotpatch(0,0)))
  61. #else
  62. #define notrace __attribute__((no_instrument_function))
  63. #endif
  64. /* Intel compiler defines __GNUC__. So we will overwrite implementations
  65. * coming from above header files here
  66. */
  67. #ifdef __INTEL_COMPILER
  68. # include <linux/compiler-intel.h>
  69. #endif
  70. /* Clang compiler defines __GNUC__. So we will overwrite implementations
  71. * coming from above header files here
  72. */
  73. #ifdef __clang__
  74. #include <linux/compiler-clang.h>
  75. #endif
  76. /*
  77. * Generic compiler-dependent macros required for kernel
  78. * build go below this comment. Actual compiler/compiler version
  79. * specific implementations come from the above header files
  80. */
  81. struct ftrace_branch_data {
  82. const char *func;
  83. const char *file;
  84. unsigned line;
  85. union {
  86. struct {
  87. unsigned long correct;
  88. unsigned long incorrect;
  89. };
  90. struct {
  91. unsigned long miss;
  92. unsigned long hit;
  93. };
  94. unsigned long miss_hit[2];
  95. };
  96. };
  97. struct ftrace_likely_data {
  98. struct ftrace_branch_data data;
  99. unsigned long constant;
  100. };
  101. /*
  102. * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
  103. * to disable branch tracing on a per file basis.
  104. */
  105. #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
  106. && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
  107. void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  108. int expect, int is_constant);
  109. #define likely_notrace(x) __builtin_expect(!!(x), 1)
  110. #define unlikely_notrace(x) __builtin_expect(!!(x), 0)
  111. #define __branch_check__(x, expect, is_constant) ({ \
  112. int ______r; \
  113. static struct ftrace_likely_data \
  114. __attribute__((__aligned__(4))) \
  115. __attribute__((section("_ftrace_annotated_branch"))) \
  116. ______f = { \
  117. .data.func = __func__, \
  118. .data.file = __FILE__, \
  119. .data.line = __LINE__, \
  120. }; \
  121. ______r = __builtin_expect(!!(x), expect); \
  122. ftrace_likely_update(&______f, ______r, \
  123. expect, is_constant); \
  124. ______r; \
  125. })
  126. /*
  127. * Using __builtin_constant_p(x) to ignore cases where the return
  128. * value is always the same. This idea is taken from a similar patch
  129. * written by Daniel Walker.
  130. */
  131. # ifndef likely
  132. # define likely(x) (__branch_check__(x, 1, __builtin_constant_p(x)))
  133. # endif
  134. # ifndef unlikely
  135. # define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))
  136. # endif
  137. #ifdef CONFIG_PROFILE_ALL_BRANCHES
  138. /*
  139. * "Define 'is'", Bill Clinton
  140. * "Define 'if'", Steven Rostedt
  141. */
  142. #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
  143. #define __trace_if(cond) \
  144. if (__builtin_constant_p(!!(cond)) ? !!(cond) : \
  145. ({ \
  146. int ______r; \
  147. static struct ftrace_branch_data \
  148. __attribute__((__aligned__(4))) \
  149. __attribute__((section("_ftrace_branch"))) \
  150. ______f = { \
  151. .func = __func__, \
  152. .file = __FILE__, \
  153. .line = __LINE__, \
  154. }; \
  155. ______r = !!(cond); \
  156. ______f.miss_hit[______r]++; \
  157. ______r; \
  158. }))
  159. #endif /* CONFIG_PROFILE_ALL_BRANCHES */
  160. #else
  161. # define likely(x) __builtin_expect(!!(x), 1)
  162. # define unlikely(x) __builtin_expect(!!(x), 0)
  163. #endif
  164. /* Optimization barrier */
  165. #ifndef barrier
  166. # define barrier() __memory_barrier()
  167. #endif
  168. #ifndef barrier_data
  169. # define barrier_data(ptr) barrier()
  170. #endif
  171. /* Unreachable code */
  172. #ifndef unreachable
  173. # define unreachable() do { } while (1)
  174. #endif
  175. /*
  176. * KENTRY - kernel entry point
  177. * This can be used to annotate symbols (functions or data) that are used
  178. * without their linker symbol being referenced explicitly. For example,
  179. * interrupt vector handlers, or functions in the kernel image that are found
  180. * programatically.
  181. *
  182. * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those
  183. * are handled in their own way (with KEEP() in linker scripts).
  184. *
  185. * KENTRY can be avoided if the symbols in question are marked as KEEP() in the
  186. * linker script. For example an architecture could KEEP() its entire
  187. * boot/exception vector code rather than annotate each function and data.
  188. */
  189. #ifndef KENTRY
  190. # define KENTRY(sym) \
  191. extern typeof(sym) sym; \
  192. static const unsigned long __kentry_##sym \
  193. __used \
  194. __attribute__((section("___kentry" "+" #sym ), used)) \
  195. = (unsigned long)&sym;
  196. #endif
  197. #ifndef RELOC_HIDE
  198. # define RELOC_HIDE(ptr, off) \
  199. ({ unsigned long __ptr; \
  200. __ptr = (unsigned long) (ptr); \
  201. (typeof(ptr)) (__ptr + (off)); })
  202. #endif
  203. #ifndef OPTIMIZER_HIDE_VAR
  204. #define OPTIMIZER_HIDE_VAR(var) barrier()
  205. #endif
  206. /* Not-quite-unique ID. */
  207. #ifndef __UNIQUE_ID
  208. # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
  209. #endif
  210. #include <uapi/linux/types.h>
  211. #define __READ_ONCE_SIZE \
  212. ({ \
  213. switch (size) { \
  214. case 1: *(__u8 *)res = *(volatile __u8 *)p; break; \
  215. case 2: *(__u16 *)res = *(volatile __u16 *)p; break; \
  216. case 4: *(__u32 *)res = *(volatile __u32 *)p; break; \
  217. case 8: *(__u64 *)res = *(volatile __u64 *)p; break; \
  218. default: \
  219. barrier(); \
  220. __builtin_memcpy((void *)res, (const void *)p, size); \
  221. barrier(); \
  222. } \
  223. })
  224. static __always_inline
  225. void __read_once_size(const volatile void *p, void *res, int size)
  226. {
  227. __READ_ONCE_SIZE;
  228. }
  229. #ifdef CONFIG_KASAN
  230. /*
  231. * This function is not 'inline' because __no_sanitize_address confilcts
  232. * with inlining. Attempt to inline it may cause a build failure.
  233. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
  234. * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
  235. */
  236. static __no_sanitize_address __maybe_unused
  237. void __read_once_size_nocheck(const volatile void *p, void *res, int size)
  238. {
  239. __READ_ONCE_SIZE;
  240. }
  241. #else
  242. static __always_inline
  243. void __read_once_size_nocheck(const volatile void *p, void *res, int size)
  244. {
  245. __READ_ONCE_SIZE;
  246. }
  247. #endif
  248. static __always_inline void __write_once_size(volatile void *p, void *res, int size)
  249. {
  250. switch (size) {
  251. case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
  252. case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
  253. case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
  254. case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
  255. default:
  256. barrier();
  257. __builtin_memcpy((void *)p, (const void *)res, size);
  258. barrier();
  259. }
  260. }
  261. /*
  262. * Prevent the compiler from merging or refetching reads or writes. The
  263. * compiler is also forbidden from reordering successive instances of
  264. * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
  265. * compiler is aware of some particular ordering. One way to make the
  266. * compiler aware of ordering is to put the two invocations of READ_ONCE,
  267. * WRITE_ONCE or ACCESS_ONCE() in different C statements.
  268. *
  269. * In contrast to ACCESS_ONCE these two macros will also work on aggregate
  270. * data types like structs or unions. If the size of the accessed data
  271. * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
  272. * READ_ONCE() and WRITE_ONCE() will fall back to memcpy(). There's at
  273. * least two memcpy()s: one for the __builtin_memcpy() and then one for
  274. * the macro doing the copy of variable - '__u' allocated on the stack.
  275. *
  276. * Their two major use cases are: (1) Mediating communication between
  277. * process-level code and irq/NMI handlers, all running on the same CPU,
  278. * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
  279. * mutilate accesses that either do not require ordering or that interact
  280. * with an explicit memory barrier or atomic instruction that provides the
  281. * required ordering.
  282. */
  283. #define __READ_ONCE(x, check) \
  284. ({ \
  285. union { typeof(x) __val; char __c[1]; } __u; \
  286. if (check) \
  287. __read_once_size(&(x), __u.__c, sizeof(x)); \
  288. else \
  289. __read_once_size_nocheck(&(x), __u.__c, sizeof(x)); \
  290. __u.__val; \
  291. })
  292. #define READ_ONCE(x) __READ_ONCE(x, 1)
  293. /*
  294. * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
  295. * to hide memory access from KASAN.
  296. */
  297. #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
  298. #define WRITE_ONCE(x, val) \
  299. ({ \
  300. union { typeof(x) __val; char __c[1]; } __u = \
  301. { .__val = (__force typeof(x)) (val) }; \
  302. __write_once_size(&(x), __u.__c, sizeof(x)); \
  303. __u.__val; \
  304. })
  305. #endif /* __KERNEL__ */
  306. #endif /* __ASSEMBLY__ */
  307. #ifdef __KERNEL__
  308. /*
  309. * Allow us to mark functions as 'deprecated' and have gcc emit a nice
  310. * warning for each use, in hopes of speeding the functions removal.
  311. * Usage is:
  312. * int __deprecated foo(void)
  313. */
  314. #ifndef __deprecated
  315. # define __deprecated /* unimplemented */
  316. #endif
  317. #ifdef MODULE
  318. #define __deprecated_for_modules __deprecated
  319. #else
  320. #define __deprecated_for_modules
  321. #endif
  322. #ifndef __must_check
  323. #define __must_check
  324. #endif
  325. #ifndef CONFIG_ENABLE_MUST_CHECK
  326. #undef __must_check
  327. #define __must_check
  328. #endif
  329. #ifndef CONFIG_ENABLE_WARN_DEPRECATED
  330. #undef __deprecated
  331. #undef __deprecated_for_modules
  332. #define __deprecated
  333. #define __deprecated_for_modules
  334. #endif
  335. #ifndef __malloc
  336. #define __malloc
  337. #endif
  338. /*
  339. * Allow us to avoid 'defined but not used' warnings on functions and data,
  340. * as well as force them to be emitted to the assembly file.
  341. *
  342. * As of gcc 3.4, static functions that are not marked with attribute((used))
  343. * may be elided from the assembly file. As of gcc 3.4, static data not so
  344. * marked will not be elided, but this may change in a future gcc version.
  345. *
  346. * NOTE: Because distributions shipped with a backported unit-at-a-time
  347. * compiler in gcc 3.3, we must define __used to be __attribute__((used))
  348. * for gcc >=3.3 instead of 3.4.
  349. *
  350. * In prior versions of gcc, such functions and data would be emitted, but
  351. * would be warned about except with attribute((unused)).
  352. *
  353. * Mark functions that are referenced only in inline assembly as __used so
  354. * the code is emitted even though it appears to be unreferenced.
  355. */
  356. #ifndef __used
  357. # define __used /* unimplemented */
  358. #endif
  359. #ifndef __maybe_unused
  360. # define __maybe_unused /* unimplemented */
  361. #endif
  362. #ifndef __always_unused
  363. # define __always_unused /* unimplemented */
  364. #endif
  365. #ifndef noinline
  366. #define noinline
  367. #endif
  368. /*
  369. * Rather then using noinline to prevent stack consumption, use
  370. * noinline_for_stack instead. For documentation reasons.
  371. */
  372. #define noinline_for_stack noinline
  373. #ifndef __always_inline
  374. #define __always_inline inline
  375. #endif
  376. #endif /* __KERNEL__ */
  377. /*
  378. * From the GCC manual:
  379. *
  380. * Many functions do not examine any values except their arguments,
  381. * and have no effects except the return value. Basically this is
  382. * just slightly more strict class than the `pure' attribute above,
  383. * since function is not allowed to read global memory.
  384. *
  385. * Note that a function that has pointer arguments and examines the
  386. * data pointed to must _not_ be declared `const'. Likewise, a
  387. * function that calls a non-`const' function usually must not be
  388. * `const'. It does not make sense for a `const' function to return
  389. * `void'.
  390. */
  391. #ifndef __attribute_const__
  392. # define __attribute_const__ /* unimplemented */
  393. #endif
  394. #ifndef __designated_init
  395. # define __designated_init
  396. #endif
  397. #ifndef __latent_entropy
  398. # define __latent_entropy
  399. #endif
  400. #ifndef __randomize_layout
  401. # define __randomize_layout __designated_init
  402. #endif
  403. #ifndef __no_randomize_layout
  404. # define __no_randomize_layout
  405. #endif
  406. /*
  407. * Tell gcc if a function is cold. The compiler will assume any path
  408. * directly leading to the call is unlikely.
  409. */
  410. #ifndef __cold
  411. #define __cold
  412. #endif
  413. /* Simple shorthand for a section definition */
  414. #ifndef __section
  415. # define __section(S) __attribute__ ((__section__(#S)))
  416. #endif
  417. #ifndef __visible
  418. #define __visible
  419. #endif
  420. /*
  421. * Assume alignment of return value.
  422. */
  423. #ifndef __assume_aligned
  424. #define __assume_aligned(a, ...)
  425. #endif
  426. /* Are two types/vars the same type (ignoring qualifiers)? */
  427. #ifndef __same_type
  428. # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
  429. #endif
  430. /* Is this type a native word size -- useful for atomic operations */
  431. #ifndef __native_word
  432. # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
  433. #endif
  434. /* Compile time object size, -1 for unknown */
  435. #ifndef __compiletime_object_size
  436. # define __compiletime_object_size(obj) -1
  437. #endif
  438. #ifndef __compiletime_warning
  439. # define __compiletime_warning(message)
  440. #endif
  441. #ifndef __compiletime_error
  442. # define __compiletime_error(message)
  443. /*
  444. * Sparse complains of variable sized arrays due to the temporary variable in
  445. * __compiletime_assert. Unfortunately we can't just expand it out to make
  446. * sparse see a constant array size without breaking compiletime_assert on old
  447. * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
  448. */
  449. # ifndef __CHECKER__
  450. # define __compiletime_error_fallback(condition) \
  451. do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
  452. # endif
  453. #endif
  454. #ifndef __compiletime_error_fallback
  455. # define __compiletime_error_fallback(condition) do { } while (0)
  456. #endif
  457. #define __compiletime_assert(condition, msg, prefix, suffix) \
  458. do { \
  459. bool __cond = !(condition); \
  460. extern void prefix ## suffix(void) __compiletime_error(msg); \
  461. if (__cond) \
  462. prefix ## suffix(); \
  463. __compiletime_error_fallback(__cond); \
  464. } while (0)
  465. #define _compiletime_assert(condition, msg, prefix, suffix) \
  466. __compiletime_assert(condition, msg, prefix, suffix)
  467. /**
  468. * compiletime_assert - break build and emit msg if condition is false
  469. * @condition: a compile-time constant condition to check
  470. * @msg: a message to emit if condition is false
  471. *
  472. * In tradition of POSIX assert, this macro will break the build if the
  473. * supplied condition is *false*, emitting the supplied error message if the
  474. * compiler has support to do so.
  475. */
  476. #define compiletime_assert(condition, msg) \
  477. _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
  478. #define compiletime_assert_atomic_type(t) \
  479. compiletime_assert(__native_word(t), \
  480. "Need native word sized stores/loads for atomicity.")
  481. /*
  482. * Prevent the compiler from merging or refetching accesses. The compiler
  483. * is also forbidden from reordering successive instances of ACCESS_ONCE(),
  484. * but only when the compiler is aware of some particular ordering. One way
  485. * to make the compiler aware of ordering is to put the two invocations of
  486. * ACCESS_ONCE() in different C statements.
  487. *
  488. * ACCESS_ONCE will only work on scalar types. For union types, ACCESS_ONCE
  489. * on a union member will work as long as the size of the member matches the
  490. * size of the union and the size is smaller than word size.
  491. *
  492. * The major use cases of ACCESS_ONCE used to be (1) Mediating communication
  493. * between process-level code and irq/NMI handlers, all running on the same CPU,
  494. * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
  495. * mutilate accesses that either do not require ordering or that interact
  496. * with an explicit memory barrier or atomic instruction that provides the
  497. * required ordering.
  498. *
  499. * If possible use READ_ONCE()/WRITE_ONCE() instead.
  500. */
  501. #define __ACCESS_ONCE(x) ({ \
  502. __maybe_unused typeof(x) __var = (__force typeof(x)) 0; \
  503. (volatile typeof(x) *)&(x); })
  504. #define ACCESS_ONCE(x) (*__ACCESS_ONCE(x))
  505. /**
  506. * lockless_dereference() - safely load a pointer for later dereference
  507. * @p: The pointer to load
  508. *
  509. * Similar to rcu_dereference(), but for situations where the pointed-to
  510. * object's lifetime is managed by something other than RCU. That
  511. * "something other" might be reference counting or simple immortality.
  512. *
  513. * The seemingly unused variable ___typecheck_p validates that @p is
  514. * indeed a pointer type by using a pointer to typeof(*p) as the type.
  515. * Taking a pointer to typeof(*p) again is needed in case p is void *.
  516. */
  517. #define lockless_dereference(p) \
  518. ({ \
  519. typeof(p) _________p1 = READ_ONCE(p); \
  520. typeof(*(p)) *___typecheck_p __maybe_unused; \
  521. smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
  522. (_________p1); \
  523. })
  524. #endif /* __LINUX_COMPILER_H */