compiler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #ifndef __LINUX_COMPILER_H
  2. #define __LINUX_COMPILER_H
  3. #include <linux/compiler_types.h>
  4. #ifndef __ASSEMBLY__
  5. #ifdef __KERNEL__
  6. /*
  7. * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
  8. * to disable branch tracing on a per file basis.
  9. */
  10. #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
  11. && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
  12. void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  13. int expect, int is_constant);
  14. #define likely_notrace(x) __builtin_expect(!!(x), 1)
  15. #define unlikely_notrace(x) __builtin_expect(!!(x), 0)
  16. #define __branch_check__(x, expect, is_constant) ({ \
  17. int ______r; \
  18. static struct ftrace_likely_data \
  19. __attribute__((__aligned__(4))) \
  20. __attribute__((section("_ftrace_annotated_branch"))) \
  21. ______f = { \
  22. .data.func = __func__, \
  23. .data.file = __FILE__, \
  24. .data.line = __LINE__, \
  25. }; \
  26. ______r = __builtin_expect(!!(x), expect); \
  27. ftrace_likely_update(&______f, ______r, \
  28. expect, is_constant); \
  29. ______r; \
  30. })
  31. /*
  32. * Using __builtin_constant_p(x) to ignore cases where the return
  33. * value is always the same. This idea is taken from a similar patch
  34. * written by Daniel Walker.
  35. */
  36. # ifndef likely
  37. # define likely(x) (__branch_check__(x, 1, __builtin_constant_p(x)))
  38. # endif
  39. # ifndef unlikely
  40. # define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))
  41. # endif
  42. #ifdef CONFIG_PROFILE_ALL_BRANCHES
  43. /*
  44. * "Define 'is'", Bill Clinton
  45. * "Define 'if'", Steven Rostedt
  46. */
  47. #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
  48. #define __trace_if(cond) \
  49. if (__builtin_constant_p(!!(cond)) ? !!(cond) : \
  50. ({ \
  51. int ______r; \
  52. static struct ftrace_branch_data \
  53. __attribute__((__aligned__(4))) \
  54. __attribute__((section("_ftrace_branch"))) \
  55. ______f = { \
  56. .func = __func__, \
  57. .file = __FILE__, \
  58. .line = __LINE__, \
  59. }; \
  60. ______r = !!(cond); \
  61. ______f.miss_hit[______r]++; \
  62. ______r; \
  63. }))
  64. #endif /* CONFIG_PROFILE_ALL_BRANCHES */
  65. #else
  66. # define likely(x) __builtin_expect(!!(x), 1)
  67. # define unlikely(x) __builtin_expect(!!(x), 0)
  68. #endif
  69. /* Optimization barrier */
  70. #ifndef barrier
  71. # define barrier() __memory_barrier()
  72. #endif
  73. #ifndef barrier_data
  74. # define barrier_data(ptr) barrier()
  75. #endif
  76. /* Unreachable code */
  77. #ifdef CONFIG_STACK_VALIDATION
  78. #define annotate_reachable() ({ \
  79. asm("%c0:\n\t" \
  80. ".pushsection .discard.reachable\n\t" \
  81. ".long %c0b - .\n\t" \
  82. ".popsection\n\t" : : "i" (__LINE__)); \
  83. })
  84. #define annotate_unreachable() ({ \
  85. asm("%c0:\n\t" \
  86. ".pushsection .discard.unreachable\n\t" \
  87. ".long %c0b - .\n\t" \
  88. ".popsection\n\t" : : "i" (__LINE__)); \
  89. })
  90. #define ASM_UNREACHABLE \
  91. "999:\n\t" \
  92. ".pushsection .discard.unreachable\n\t" \
  93. ".long 999b - .\n\t" \
  94. ".popsection\n\t"
  95. #else
  96. #define annotate_reachable()
  97. #define annotate_unreachable()
  98. #endif
  99. #ifndef ASM_UNREACHABLE
  100. # define ASM_UNREACHABLE
  101. #endif
  102. #ifndef unreachable
  103. # define unreachable() do { annotate_reachable(); do { } while (1); } while (0)
  104. #endif
  105. /*
  106. * KENTRY - kernel entry point
  107. * This can be used to annotate symbols (functions or data) that are used
  108. * without their linker symbol being referenced explicitly. For example,
  109. * interrupt vector handlers, or functions in the kernel image that are found
  110. * programatically.
  111. *
  112. * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those
  113. * are handled in their own way (with KEEP() in linker scripts).
  114. *
  115. * KENTRY can be avoided if the symbols in question are marked as KEEP() in the
  116. * linker script. For example an architecture could KEEP() its entire
  117. * boot/exception vector code rather than annotate each function and data.
  118. */
  119. #ifndef KENTRY
  120. # define KENTRY(sym) \
  121. extern typeof(sym) sym; \
  122. static const unsigned long __kentry_##sym \
  123. __used \
  124. __attribute__((section("___kentry" "+" #sym ), used)) \
  125. = (unsigned long)&sym;
  126. #endif
  127. #ifndef RELOC_HIDE
  128. # define RELOC_HIDE(ptr, off) \
  129. ({ unsigned long __ptr; \
  130. __ptr = (unsigned long) (ptr); \
  131. (typeof(ptr)) (__ptr + (off)); })
  132. #endif
  133. #ifndef OPTIMIZER_HIDE_VAR
  134. #define OPTIMIZER_HIDE_VAR(var) barrier()
  135. #endif
  136. /* Not-quite-unique ID. */
  137. #ifndef __UNIQUE_ID
  138. # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
  139. #endif
  140. #include <uapi/linux/types.h>
  141. #define __READ_ONCE_SIZE \
  142. ({ \
  143. switch (size) { \
  144. case 1: *(__u8 *)res = *(volatile __u8 *)p; break; \
  145. case 2: *(__u16 *)res = *(volatile __u16 *)p; break; \
  146. case 4: *(__u32 *)res = *(volatile __u32 *)p; break; \
  147. case 8: *(__u64 *)res = *(volatile __u64 *)p; break; \
  148. default: \
  149. barrier(); \
  150. __builtin_memcpy((void *)res, (const void *)p, size); \
  151. barrier(); \
  152. } \
  153. })
  154. static __always_inline
  155. void __read_once_size(const volatile void *p, void *res, int size)
  156. {
  157. __READ_ONCE_SIZE;
  158. }
  159. #ifdef CONFIG_KASAN
  160. /*
  161. * This function is not 'inline' because __no_sanitize_address confilcts
  162. * with inlining. Attempt to inline it may cause a build failure.
  163. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
  164. * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
  165. */
  166. static __no_sanitize_address __maybe_unused
  167. void __read_once_size_nocheck(const volatile void *p, void *res, int size)
  168. {
  169. __READ_ONCE_SIZE;
  170. }
  171. #else
  172. static __always_inline
  173. void __read_once_size_nocheck(const volatile void *p, void *res, int size)
  174. {
  175. __READ_ONCE_SIZE;
  176. }
  177. #endif
  178. static __always_inline void __write_once_size(volatile void *p, void *res, int size)
  179. {
  180. switch (size) {
  181. case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
  182. case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
  183. case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
  184. case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
  185. default:
  186. barrier();
  187. __builtin_memcpy((void *)p, (const void *)res, size);
  188. barrier();
  189. }
  190. }
  191. /*
  192. * Prevent the compiler from merging or refetching reads or writes. The
  193. * compiler is also forbidden from reordering successive instances of
  194. * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
  195. * compiler is aware of some particular ordering. One way to make the
  196. * compiler aware of ordering is to put the two invocations of READ_ONCE,
  197. * WRITE_ONCE or ACCESS_ONCE() in different C statements.
  198. *
  199. * In contrast to ACCESS_ONCE these two macros will also work on aggregate
  200. * data types like structs or unions. If the size of the accessed data
  201. * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
  202. * READ_ONCE() and WRITE_ONCE() will fall back to memcpy(). There's at
  203. * least two memcpy()s: one for the __builtin_memcpy() and then one for
  204. * the macro doing the copy of variable - '__u' allocated on the stack.
  205. *
  206. * Their two major use cases are: (1) Mediating communication between
  207. * process-level code and irq/NMI handlers, all running on the same CPU,
  208. * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
  209. * mutilate accesses that either do not require ordering or that interact
  210. * with an explicit memory barrier or atomic instruction that provides the
  211. * required ordering.
  212. */
  213. #include <asm/barrier.h>
  214. #define __READ_ONCE(x, check) \
  215. ({ \
  216. union { typeof(x) __val; char __c[1]; } __u; \
  217. if (check) \
  218. __read_once_size(&(x), __u.__c, sizeof(x)); \
  219. else \
  220. __read_once_size_nocheck(&(x), __u.__c, sizeof(x)); \
  221. smp_read_barrier_depends(); /* Enforce dependency ordering from x */ \
  222. __u.__val; \
  223. })
  224. #define READ_ONCE(x) __READ_ONCE(x, 1)
  225. /*
  226. * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
  227. * to hide memory access from KASAN.
  228. */
  229. #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
  230. #define WRITE_ONCE(x, val) \
  231. ({ \
  232. union { typeof(x) __val; char __c[1]; } __u = \
  233. { .__val = (__force typeof(x)) (val) }; \
  234. __write_once_size(&(x), __u.__c, sizeof(x)); \
  235. __u.__val; \
  236. })
  237. #endif /* __KERNEL__ */
  238. #endif /* __ASSEMBLY__ */
  239. /* Compile time object size, -1 for unknown */
  240. #ifndef __compiletime_object_size
  241. # define __compiletime_object_size(obj) -1
  242. #endif
  243. #ifndef __compiletime_warning
  244. # define __compiletime_warning(message)
  245. #endif
  246. #ifndef __compiletime_error
  247. # define __compiletime_error(message)
  248. /*
  249. * Sparse complains of variable sized arrays due to the temporary variable in
  250. * __compiletime_assert. Unfortunately we can't just expand it out to make
  251. * sparse see a constant array size without breaking compiletime_assert on old
  252. * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
  253. */
  254. # ifndef __CHECKER__
  255. # define __compiletime_error_fallback(condition) \
  256. do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
  257. # endif
  258. #endif
  259. #ifndef __compiletime_error_fallback
  260. # define __compiletime_error_fallback(condition) do { } while (0)
  261. #endif
  262. #ifdef __OPTIMIZE__
  263. # define __compiletime_assert(condition, msg, prefix, suffix) \
  264. do { \
  265. bool __cond = !(condition); \
  266. extern void prefix ## suffix(void) __compiletime_error(msg); \
  267. if (__cond) \
  268. prefix ## suffix(); \
  269. __compiletime_error_fallback(__cond); \
  270. } while (0)
  271. #else
  272. # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
  273. #endif
  274. #define _compiletime_assert(condition, msg, prefix, suffix) \
  275. __compiletime_assert(condition, msg, prefix, suffix)
  276. /**
  277. * compiletime_assert - break build and emit msg if condition is false
  278. * @condition: a compile-time constant condition to check
  279. * @msg: a message to emit if condition is false
  280. *
  281. * In tradition of POSIX assert, this macro will break the build if the
  282. * supplied condition is *false*, emitting the supplied error message if the
  283. * compiler has support to do so.
  284. */
  285. #define compiletime_assert(condition, msg) \
  286. _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
  287. #define compiletime_assert_atomic_type(t) \
  288. compiletime_assert(__native_word(t), \
  289. "Need native word sized stores/loads for atomicity.")
  290. /*
  291. * Prevent the compiler from merging or refetching accesses. The compiler
  292. * is also forbidden from reordering successive instances of ACCESS_ONCE(),
  293. * but only when the compiler is aware of some particular ordering. One way
  294. * to make the compiler aware of ordering is to put the two invocations of
  295. * ACCESS_ONCE() in different C statements.
  296. *
  297. * ACCESS_ONCE will only work on scalar types. For union types, ACCESS_ONCE
  298. * on a union member will work as long as the size of the member matches the
  299. * size of the union and the size is smaller than word size.
  300. *
  301. * The major use cases of ACCESS_ONCE used to be (1) Mediating communication
  302. * between process-level code and irq/NMI handlers, all running on the same CPU,
  303. * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
  304. * mutilate accesses that either do not require ordering or that interact
  305. * with an explicit memory barrier or atomic instruction that provides the
  306. * required ordering.
  307. *
  308. * If possible use READ_ONCE()/WRITE_ONCE() instead.
  309. */
  310. #define __ACCESS_ONCE(x) ({ \
  311. __maybe_unused typeof(x) __var = (__force typeof(x)) 0; \
  312. (volatile typeof(x) *)&(x); })
  313. #define ACCESS_ONCE(x) (*__ACCESS_ONCE(x))
  314. #endif /* __LINUX_COMPILER_H */