compiler_types.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_COMPILER_TYPES_H
  3. #define __LINUX_COMPILER_TYPES_H
  4. #ifndef __ASSEMBLY__
  5. #include "compiler_attributes.h"
  6. /*
  7. * Skipped when running bindgen due to a libclang issue;
  8. * see https://github.com/rust-lang/rust-bindgen/issues/2244.
  9. */
  10. #if defined(CONFIG_DEBUG_INFO_BTF) && defined(CONFIG_PAHOLE_HAS_BTF_TAG) && \
  11. __has_attribute(btf_type_tag) && !defined(__BINDGEN__)
  12. #define BTF_TYPE_TAG(value) __attribute__((btf_type_tag(#value)))
  13. #else
  14. #define BTF_TYPE_TAG(value) /* nothing */
  15. #endif
  16. /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
  17. #define ___PASTE(a, b) a##b
  18. #define __PASTE(a, b) ___PASTE(a, b)
  19. /* sparse defines __CHECKER__; see Documentation/dev-tools/sparse.rst */
  20. #ifdef __CHECKER__
  21. /* address spaces */
  22. #define __kernel __attribute__((address_space(0)))
  23. #define __user __attribute__((noderef, address_space(__user)))
  24. #define __iomem __attribute__((noderef, address_space(__iomem)))
  25. #define __percpu __attribute__((noderef, address_space(__percpu)))
  26. #define __rcu __attribute__((noderef, address_space(__rcu)))
  27. static inline void __chk_user_ptr(const volatile void __user *ptr)
  28. {
  29. }
  30. static inline void __chk_io_ptr(const volatile void __iomem *ptr)
  31. {
  32. }
  33. /* context/locking */
  34. #define __must_hold(x) __attribute__((context(x, 1, 1)))
  35. #define __acquires(x) __attribute__((context(x, 0, 1)))
  36. #define __cond_acquires(x) __attribute__((context(x, 0, -1)))
  37. #define __releases(x) __attribute__((context(x, 1, 0)))
  38. #define __acquire(x) __context__(x, 1)
  39. #define __release(x) __context__(x, -1)
  40. #define __cond_lock(x, c) \
  41. ((c) ? ({ \
  42. __acquire(x); \
  43. 1; \
  44. }) : \
  45. 0)
  46. /* other */
  47. #define __force __attribute__((force))
  48. #define __nocast __attribute__((nocast))
  49. #define __safe __attribute__((safe))
  50. #define __private __attribute__((noderef))
  51. #define ACCESS_PRIVATE(p, member) \
  52. (*((typeof((p)->member) __force *)&(p)->member))
  53. #else /* __CHECKER__ */
  54. /* address spaces */
  55. #define __kernel
  56. #ifdef STRUCTLEAK_PLUGIN
  57. #define __user __attribute__((user))
  58. #else
  59. #define __user BTF_TYPE_TAG(user)
  60. #endif
  61. #define __iomem
  62. #define __percpu BTF_TYPE_TAG(percpu)
  63. #define __rcu BTF_TYPE_TAG(rcu)
  64. #define __chk_user_ptr(x) (void)0
  65. #define __chk_io_ptr(x) (void)0
  66. /* context/locking */
  67. #define __must_hold(x)
  68. #define __acquires(x)
  69. #define __cond_acquires(x)
  70. #define __releases(x)
  71. #define __acquire(x) (void)0
  72. #define __release(x) (void)0
  73. #define __cond_lock(x, c) (c)
  74. /* other */
  75. #define __force
  76. #define __nocast
  77. #define __safe
  78. #define __private
  79. #define ACCESS_PRIVATE(p, member) ((p)->member)
  80. #define __builtin_warning(x, y...) (1)
  81. #endif /* __CHECKER__ */
  82. /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
  83. #define ___PASTE(a, b) a##b
  84. #define __PASTE(a, b) ___PASTE(a, b)
  85. #endif /* __ASSEMBLY__ */
  86. /*
  87. * The below symbols may be defined for one or more, but not ALL, of the above
  88. * compilers. We don't consider that to be an error, so set them to nothing.
  89. * For example, some of them are for compiler specific plugins.
  90. */
  91. #ifndef __latent_entropy
  92. #define __latent_entropy
  93. #endif
  94. #if defined(RANDSTRUCT) && !defined(__CHECKER__)
  95. #define __randomize_layout __designated_init __attribute__((randomize_layout))
  96. #define __no_randomize_layout __attribute__((no_randomize_layout))
  97. /* This anon struct can add padding, so only enable it under randstruct. */
  98. #define randomized_struct_fields_start struct {
  99. #define randomized_struct_fields_end \
  100. } \
  101. __randomize_layout;
  102. #else
  103. #define __randomize_layout __designated_init
  104. #define __no_randomize_layout
  105. #define randomized_struct_fields_start
  106. #define randomized_struct_fields_end
  107. #endif
  108. #ifndef __noscs
  109. #define __noscs
  110. #endif
  111. #ifndef __nocfi
  112. #define __nocfi
  113. #endif
  114. #if defined(CC_USING_HOTPATCH)
  115. #define notrace __attribute__((hotpatch(0, 0)))
  116. #elif defined(CC_USING_PATCHABLE_FUNCTION_ENTRY)
  117. #define notrace __attribute__((patchable_function_entry(0, 0)))
  118. #else
  119. #define notrace __attribute__((__no_instrument_function__))
  120. #endif
  121. /*
  122. * gcc provides both __inline__ and __inline as alternate spellings of
  123. * the inline keyword, though the latter is undocumented. New kernel
  124. * code should only use the inline spelling, but some existing code
  125. * uses __inline__. Since we #define inline above, to ensure
  126. * __inline__ has the same semantics, we need this #define.
  127. *
  128. * However, the spelling __inline is strictly reserved for referring
  129. * to the bare keyword.
  130. */
  131. #define __inline__ inline
  132. /*
  133. * GCC does not warn about unused static inline functions for -Wunused-function.
  134. * Suppress the warning in clang as well by using __maybe_unused, but enable it
  135. * for W=1 build. This will allow clang to find unused functions. Remove the
  136. * __inline_maybe_unused entirely after fixing most of -Wunused-function warnings.
  137. */
  138. #ifdef KBUILD_EXTRA_WARN1
  139. #define __inline_maybe_unused
  140. #else
  141. #define __inline_maybe_unused __maybe_unused
  142. #endif
  143. /*
  144. * Rather then using noinline to prevent stack consumption, use
  145. * noinline_for_stack instead. For documentation reasons.
  146. */
  147. #define noinline_for_stack noinline
  148. /*
  149. * Prefer gnu_inline, so that extern inline functions do not emit an
  150. * externally visible function. This makes extern inline behave as per gnu89
  151. * semantics rather than c99. This prevents multiple symbol definition errors
  152. * of extern inline functions at link time.
  153. * A lot of inline functions can cause havoc with function tracing.
  154. */
  155. #define inline inline __gnu_inline __inline_maybe_unused notrace
  156. /*
  157. * Any place that could be marked with the "alloc_size" attribute is also
  158. * a place to be marked with the "malloc" attribute, except those that may
  159. * be performing a _reallocation_, as that may alias the existing pointer.
  160. * For these, use __realloc_size().
  161. */
  162. #ifdef __alloc_size__
  163. #define __alloc_size(x, ...) __alloc_size__(x, ##__VA_ARGS__) __malloc
  164. #define __realloc_size(x, ...) __alloc_size__(x, ##__VA_ARGS__)
  165. #else
  166. #define __alloc_size(x, ...) __malloc
  167. #define __realloc_size(x, ...)
  168. #endif
  169. #ifndef asm_volatile_goto
  170. #define asm_volatile_goto(x...) asm goto(x)
  171. #endif
  172. #ifdef CONFIG_CC_HAS_ASM_INLINE
  173. #define asm_inline asm __inline
  174. #else
  175. #define asm_inline asm
  176. #endif
  177. /* Are two types/vars the same type (ignoring qualifiers)? */
  178. #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
  179. /*
  180. * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
  181. * non-scalar types unchanged.
  182. */
  183. /*
  184. * Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'
  185. * is not type-compatible with 'signed char', and we define a separate case.
  186. */
  187. #define __scalar_type_to_expr_cases(type) \
  188. unsigned type : (unsigned type)0, signed type : (signed type)0
  189. #define __unqual_scalar_typeof(x) \
  190. typeof(_Generic((x), char \
  191. : (char)0, __scalar_type_to_expr_cases(char), \
  192. __scalar_type_to_expr_cases(short), \
  193. __scalar_type_to_expr_cases(int), \
  194. __scalar_type_to_expr_cases(long), \
  195. __scalar_type_to_expr_cases(long long), default \
  196. : (x)))
  197. /* Is this type a native word size -- useful for atomic operations */
  198. #define __native_word(t) \
  199. (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
  200. sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
  201. #ifdef __OPTIMIZE__
  202. #define __compiletime_assert(condition, msg, prefix, suffix) \
  203. do { \
  204. /* \
  205. * __noreturn is needed to give the compiler enough \
  206. * information to avoid certain possibly-uninitialized \
  207. * warnings (regardless of the build failing). \
  208. */ \
  209. __noreturn extern void prefix##suffix(void) \
  210. __compiletime_error(msg); \
  211. if (!(condition)) \
  212. prefix##suffix(); \
  213. } while (0)
  214. #else
  215. #define __compiletime_assert(condition, msg, prefix, suffix) \
  216. do { \
  217. } while (0)
  218. #endif
  219. #define _compiletime_assert(condition, msg, prefix, suffix) \
  220. __compiletime_assert(condition, msg, prefix, suffix)
  221. /**
  222. * compiletime_assert - break build and emit msg if condition is false
  223. * @condition: a compile-time constant condition to check
  224. * @msg: a message to emit if condition is false
  225. *
  226. * In tradition of POSIX assert, this macro will break the build if the
  227. * supplied condition is *false*, emitting the supplied error message if the
  228. * compiler has support to do so.
  229. */
  230. #define compiletime_assert(condition, msg) \
  231. _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
  232. #define compiletime_assert_atomic_type(t) \
  233. compiletime_assert( \
  234. __native_word(t), \
  235. "Need native word sized stores/loads for atomicity.")
  236. /* Helpers for emitting diagnostics in pragmas. */
  237. #ifndef __diag
  238. #define __diag(string)
  239. #endif
  240. #ifndef __diag_GCC
  241. #define __diag_GCC(version, severity, string)
  242. #endif
  243. #define __diag_push() __diag(push)
  244. #define __diag_pop() __diag(pop)
  245. #define __diag_ignore(compiler, version, option, comment) \
  246. __diag_##compiler(version, ignore, option)
  247. #define __diag_warn(compiler, version, option, comment) \
  248. __diag_##compiler(version, warn, option)
  249. #define __diag_error(compiler, version, option, comment) \
  250. __diag_##compiler(version, error, option)
  251. #ifndef __diag_ignore_all
  252. #define __diag_ignore_all(option, comment)
  253. #endif
  254. #endif /* __LINUX_COMPILER_TYPES_H */