xref: /linux-6.15/include/linux/compiler_types.h (revision 6cea5ae7)
166dbeef9SMiguel Ojeda /* SPDX-License-Identifier: GPL-2.0 */
2d1515582SWill Deacon #ifndef __LINUX_COMPILER_TYPES_H
3d1515582SWill Deacon #define __LINUX_COMPILER_TYPES_H
4d1515582SWill Deacon 
526dd68d2SPrzemek Kitszel /*
626dd68d2SPrzemek Kitszel  * __has_builtin is supported on gcc >= 10, clang >= 3 and icc >= 21.
726dd68d2SPrzemek Kitszel  * In the meantime, to support gcc < 10, we implement __has_builtin
826dd68d2SPrzemek Kitszel  * by hand.
926dd68d2SPrzemek Kitszel  */
1026dd68d2SPrzemek Kitszel #ifndef __has_builtin
1126dd68d2SPrzemek Kitszel #define __has_builtin(x) (0)
1226dd68d2SPrzemek Kitszel #endif
1326dd68d2SPrzemek Kitszel 
14d1515582SWill Deacon #ifndef __ASSEMBLY__
15d1515582SWill Deacon 
162f7ab126SMiguel Ojeda /*
172f7ab126SMiguel Ojeda  * Skipped when running bindgen due to a libclang issue;
182f7ab126SMiguel Ojeda  * see https://github.com/rust-lang/rust-bindgen/issues/2244.
192f7ab126SMiguel Ojeda  */
206789ab96SHao Luo #if defined(CONFIG_DEBUG_INFO_BTF) && defined(CONFIG_PAHOLE_HAS_BTF_TAG) && \
212f7ab126SMiguel Ojeda 	__has_attribute(btf_type_tag) && !defined(__BINDGEN__)
226789ab96SHao Luo # define BTF_TYPE_TAG(value) __attribute__((btf_type_tag(#value)))
236789ab96SHao Luo #else
246789ab96SHao Luo # define BTF_TYPE_TAG(value) /* nothing */
256789ab96SHao Luo #endif
266789ab96SHao Luo 
27179fd6baSBjorn Helgaas /* sparse defines __CHECKER__; see Documentation/dev-tools/sparse.rst */
28d1515582SWill Deacon #ifdef __CHECKER__
2925fd529cSLuc Van Oostenryck /* address spaces */
30d1515582SWill Deacon # define __kernel	__attribute__((address_space(0)))
31670d0a4bSLuc Van Oostenryck # define __user		__attribute__((noderef, address_space(__user)))
32670d0a4bSLuc Van Oostenryck # define __iomem	__attribute__((noderef, address_space(__iomem)))
3325fd529cSLuc Van Oostenryck # define __percpu	__attribute__((noderef, address_space(__percpu)))
3425fd529cSLuc Van Oostenryck # define __rcu		__attribute__((noderef, address_space(__rcu)))
__chk_user_ptr(const volatile void __user * ptr)35e5fc436fSLuc Van Oostenryck static inline void __chk_user_ptr(const volatile void __user *ptr) { }
__chk_io_ptr(const volatile void __iomem * ptr)36e5fc436fSLuc Van Oostenryck static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
3725fd529cSLuc Van Oostenryck /* context/locking */
38d1515582SWill Deacon # define __must_hold(x)	__attribute__((context(x,1,1)))
39d1515582SWill Deacon # define __acquires(x)	__attribute__((context(x,0,1)))
404a557a5dSLinus Torvalds # define __cond_acquires(x) __attribute__((context(x,0,-1)))
41d1515582SWill Deacon # define __releases(x)	__attribute__((context(x,1,0)))
42d1515582SWill Deacon # define __acquire(x)	__context__(x,1)
43d1515582SWill Deacon # define __release(x)	__context__(x,-1)
44d1515582SWill Deacon # define __cond_lock(x,c)	((c) ? ({ __acquire(x); 1; }) : 0)
4525fd529cSLuc Van Oostenryck /* other */
4625fd529cSLuc Van Oostenryck # define __force	__attribute__((force))
4725fd529cSLuc Van Oostenryck # define __nocast	__attribute__((nocast))
4825fd529cSLuc Van Oostenryck # define __safe		__attribute__((safe))
49d1515582SWill Deacon # define __private	__attribute__((noderef))
50d1515582SWill Deacon # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member))
51d1515582SWill Deacon #else /* __CHECKER__ */
5225fd529cSLuc Van Oostenryck /* address spaces */
5325fd529cSLuc Van Oostenryck # define __kernel
54d1515582SWill Deacon # ifdef STRUCTLEAK_PLUGIN
55d1515582SWill Deacon #  define __user	__attribute__((user))
56d1515582SWill Deacon # else
576789ab96SHao Luo #  define __user	BTF_TYPE_TAG(user)
58d1515582SWill Deacon # endif
59d1515582SWill Deacon # define __iomem
60*6cea5ae7SUros Bizjak # define __percpu	__percpu_qual BTF_TYPE_TAG(percpu)
615a0f663fSYonghong Song # define __rcu		BTF_TYPE_TAG(rcu)
625a0f663fSYonghong Song 
63d1515582SWill Deacon # define __chk_user_ptr(x)	(void)0
64d1515582SWill Deacon # define __chk_io_ptr(x)	(void)0
6525fd529cSLuc Van Oostenryck /* context/locking */
66d1515582SWill Deacon # define __must_hold(x)
67d1515582SWill Deacon # define __acquires(x)
684a557a5dSLinus Torvalds # define __cond_acquires(x)
69d1515582SWill Deacon # define __releases(x)
70d1515582SWill Deacon # define __acquire(x)	(void)0
71d1515582SWill Deacon # define __release(x)	(void)0
72d1515582SWill Deacon # define __cond_lock(x,c) (c)
7325fd529cSLuc Van Oostenryck /* other */
7425fd529cSLuc Van Oostenryck # define __force
7525fd529cSLuc Van Oostenryck # define __nocast
7625fd529cSLuc Van Oostenryck # define __safe
77d1515582SWill Deacon # define __private
78d1515582SWill Deacon # define ACCESS_PRIVATE(p, member) ((p)->member)
7925fd529cSLuc Van Oostenryck # define __builtin_warning(x, y...) (1)
80d1515582SWill Deacon #endif /* __CHECKER__ */
81d1515582SWill Deacon 
82d1515582SWill Deacon /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
83d1515582SWill Deacon #define ___PASTE(a,b) a##b
84d1515582SWill Deacon #define __PASTE(a,b) ___PASTE(a,b)
85d1515582SWill Deacon 
86d1515582SWill Deacon #ifdef __KERNEL__
87d1515582SWill Deacon 
88a3f8a30fSMiguel Ojeda /* Attributes */
89a3f8a30fSMiguel Ojeda #include <linux/compiler_attributes.h>
90a3f8a30fSMiguel Ojeda 
91c27cd083SMark Rutland #if CONFIG_FUNCTION_ALIGNMENT > 0
92c27cd083SMark Rutland #define __function_aligned		__aligned(CONFIG_FUNCTION_ALIGNMENT)
93c27cd083SMark Rutland #else
94c27cd083SMark Rutland #define __function_aligned
95c27cd083SMark Rutland #endif
96c27cd083SMark Rutland 
97c27cd083SMark Rutland /*
98c27cd083SMark Rutland  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-cold-function-attribute
99c27cd083SMark Rutland  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
100c27cd083SMark Rutland  *
101c27cd083SMark Rutland  * When -falign-functions=N is in use, we must avoid the cold attribute as
1025270316cSPetr Pavlu  * GCC drops the alignment for cold functions. Worse, GCC can implicitly mark
1035270316cSPetr Pavlu  * callees of cold functions as cold themselves, so it's not sufficient to add
1045270316cSPetr Pavlu  * __function_aligned here as that will not ensure that callees are correctly
1055270316cSPetr Pavlu  * aligned.
106c27cd083SMark Rutland  *
107c27cd083SMark Rutland  * See:
108c27cd083SMark Rutland  *
109c27cd083SMark Rutland  *   https://lore.kernel.org/lkml/Y77%2FqVgvaJidFpYt@FVFF77S0Q05N
110c27cd083SMark Rutland  *   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c9
111c27cd083SMark Rutland  */
1125270316cSPetr Pavlu #if defined(CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT) || (CONFIG_FUNCTION_ALIGNMENT == 0)
113c27cd083SMark Rutland #define __cold				__attribute__((__cold__))
114c27cd083SMark Rutland #else
115c27cd083SMark Rutland #define __cold
116c27cd083SMark Rutland #endif
117c27cd083SMark Rutland 
1187a0fd5e1SMarco Elver /*
1197a0fd5e1SMarco Elver  * On x86-64 and arm64 targets, __preserve_most changes the calling convention
1207a0fd5e1SMarco Elver  * of a function to make the code in the caller as unintrusive as possible. This
1217a0fd5e1SMarco Elver  * convention behaves identically to the C calling convention on how arguments
1227a0fd5e1SMarco Elver  * and return values are passed, but uses a different set of caller- and callee-
1237a0fd5e1SMarco Elver  * saved registers.
1247a0fd5e1SMarco Elver  *
1257a0fd5e1SMarco Elver  * The purpose is to alleviates the burden of saving and recovering a large
1267a0fd5e1SMarco Elver  * register set before and after the call in the caller.  This is beneficial for
1277a0fd5e1SMarco Elver  * rarely taken slow paths, such as error-reporting functions that may be called
1287a0fd5e1SMarco Elver  * from hot paths.
1297a0fd5e1SMarco Elver  *
1307a0fd5e1SMarco Elver  * Note: This may conflict with instrumentation inserted on function entry which
1317a0fd5e1SMarco Elver  * does not use __preserve_most or equivalent convention (if in assembly). Since
1327a0fd5e1SMarco Elver  * function tracing assumes the normal C calling convention, where the attribute
1337a0fd5e1SMarco Elver  * is supported, __preserve_most implies notrace.  It is recommended to restrict
1347a0fd5e1SMarco Elver  * use of the attribute to functions that should or already disable tracing.
1357a0fd5e1SMarco Elver  *
1367a0fd5e1SMarco Elver  * Optional: not supported by gcc.
1377a0fd5e1SMarco Elver  *
1387a0fd5e1SMarco Elver  * clang: https://clang.llvm.org/docs/AttributeReference.html#preserve-most
1397a0fd5e1SMarco Elver  */
1407a0fd5e1SMarco Elver #if __has_attribute(__preserve_most__) && (defined(CONFIG_X86_64) || defined(CONFIG_ARM64))
1417a0fd5e1SMarco Elver # define __preserve_most notrace __attribute__((__preserve_most__))
1427a0fd5e1SMarco Elver #else
1437a0fd5e1SMarco Elver # define __preserve_most
1447a0fd5e1SMarco Elver #endif
1457a0fd5e1SMarco Elver 
1460a5d3258STony Ambardar /*
1470a5d3258STony Ambardar  * Annotating a function/variable with __retain tells the compiler to place
1480a5d3258STony Ambardar  * the object in its own section and set the flag SHF_GNU_RETAIN. This flag
1490a5d3258STony Ambardar  * instructs the linker to retain the object during garbage-cleanup or LTO
1500a5d3258STony Ambardar  * phases.
1510a5d3258STony Ambardar  *
1520a5d3258STony Ambardar  * Note that the __used macro is also used to prevent functions or data
1530a5d3258STony Ambardar  * being optimized out, but operates at the compiler/IR-level and may still
1540a5d3258STony Ambardar  * allow unintended removal of objects during linking.
1550a5d3258STony Ambardar  *
1560a5d3258STony Ambardar  * Optional: only supported since gcc >= 11, clang >= 13
1570a5d3258STony Ambardar  *
1580a5d3258STony Ambardar  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-retain-function-attribute
1590a5d3258STony Ambardar  * clang: https://clang.llvm.org/docs/AttributeReference.html#retain
1600a5d3258STony Ambardar  */
1610a5d3258STony Ambardar #if __has_attribute(__retain__) && \
1620a5d3258STony Ambardar 	(defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || \
1630a5d3258STony Ambardar 	 defined(CONFIG_LTO_CLANG))
1640a5d3258STony Ambardar # define __retain			__attribute__((__retain__))
1650a5d3258STony Ambardar #else
1660a5d3258STony Ambardar # define __retain
1670a5d3258STony Ambardar #endif
1680a5d3258STony Ambardar 
169815f0ddbSNick Desaulniers /* Compiler specific macros. */
170d1515582SWill Deacon #ifdef __clang__
171d1515582SWill Deacon #include <linux/compiler-clang.h>
172815f0ddbSNick Desaulniers #elif defined(__GNUC__)
173815f0ddbSNick Desaulniers /* The above compilers also define __GNUC__, so order is important here. */
174815f0ddbSNick Desaulniers #include <linux/compiler-gcc.h>
175815f0ddbSNick Desaulniers #else
176815f0ddbSNick Desaulniers #error "Unknown compiler"
177d1515582SWill Deacon #endif
178d1515582SWill Deacon 
179d1515582SWill Deacon /*
18004f264d3SPaul Burton  * Some architectures need to provide custom definitions of macros provided
18104f264d3SPaul Burton  * by linux/compiler-*.h, and can do so using asm/compiler.h. We include that
18204f264d3SPaul Burton  * conditionally rather than using an asm-generic wrapper in order to avoid
18304f264d3SPaul Burton  * build failures if any C compilation, which will include this file via an
18404f264d3SPaul Burton  * -include argument in c_flags, occurs prior to the asm-generic wrappers being
18504f264d3SPaul Burton  * generated.
18604f264d3SPaul Burton  */
18704f264d3SPaul Burton #ifdef CONFIG_HAVE_ARCH_COMPILER_H
18804f264d3SPaul Burton #include <asm/compiler.h>
18904f264d3SPaul Burton #endif
19004f264d3SPaul Burton 
191d1515582SWill Deacon struct ftrace_branch_data {
192d1515582SWill Deacon 	const char *func;
193d1515582SWill Deacon 	const char *file;
194d1515582SWill Deacon 	unsigned line;
195d1515582SWill Deacon 	union {
196d1515582SWill Deacon 		struct {
197d1515582SWill Deacon 			unsigned long correct;
198d1515582SWill Deacon 			unsigned long incorrect;
199d1515582SWill Deacon 		};
200d1515582SWill Deacon 		struct {
201d1515582SWill Deacon 			unsigned long miss;
202d1515582SWill Deacon 			unsigned long hit;
203d1515582SWill Deacon 		};
204d1515582SWill Deacon 		unsigned long miss_hit[2];
205d1515582SWill Deacon 	};
206d1515582SWill Deacon };
207d1515582SWill Deacon 
208d1515582SWill Deacon struct ftrace_likely_data {
209d1515582SWill Deacon 	struct ftrace_branch_data	data;
210d1515582SWill Deacon 	unsigned long			constant;
211d1515582SWill Deacon };
212d1515582SWill Deacon 
21371391bddSXiaozhou Liu #if defined(CC_USING_HOTPATCH)
21471391bddSXiaozhou Liu #define notrace			__attribute__((hotpatch(0, 0)))
2152809b392SSven Schnelle #elif defined(CC_USING_PATCHABLE_FUNCTION_ENTRY)
2162809b392SSven Schnelle #define notrace			__attribute__((patchable_function_entry(0, 0)))
21771391bddSXiaozhou Liu #else
21871391bddSXiaozhou Liu #define notrace			__attribute__((__no_instrument_function__))
21971391bddSXiaozhou Liu #endif
22071391bddSXiaozhou Liu 
22171391bddSXiaozhou Liu /*
22271391bddSXiaozhou Liu  * it doesn't make sense on ARM (currently the only user of __naked)
22371391bddSXiaozhou Liu  * to trace naked functions because then mcount is called without
22471391bddSXiaozhou Liu  * stack and frame pointer being set up and there is no chance to
22571391bddSXiaozhou Liu  * restore the lr register to the value before mcount was called.
22671391bddSXiaozhou Liu  */
22771391bddSXiaozhou Liu #define __naked			__attribute__((__naked__)) notrace
22871391bddSXiaozhou Liu 
22971391bddSXiaozhou Liu /*
23071391bddSXiaozhou Liu  * Prefer gnu_inline, so that extern inline functions do not emit an
23171391bddSXiaozhou Liu  * externally visible function. This makes extern inline behave as per gnu89
23271391bddSXiaozhou Liu  * semantics rather than c99. This prevents multiple symbol definition errors
23371391bddSXiaozhou Liu  * of extern inline functions at link time.
23471391bddSXiaozhou Liu  * A lot of inline functions can cause havoc with function tracing.
23571391bddSXiaozhou Liu  */
236889b3c12SMasahiro Yamada #define inline inline __gnu_inline __inline_maybe_unused notrace
23771391bddSXiaozhou Liu 
238c30724e9SRasmus Villemoes /*
239c30724e9SRasmus Villemoes  * gcc provides both __inline__ and __inline as alternate spellings of
240c30724e9SRasmus Villemoes  * the inline keyword, though the latter is undocumented. New kernel
241c30724e9SRasmus Villemoes  * code should only use the inline spelling, but some existing code
242c30724e9SRasmus Villemoes  * uses __inline__. Since we #define inline above, to ensure
243c30724e9SRasmus Villemoes  * __inline__ has the same semantics, we need this #define.
244c30724e9SRasmus Villemoes  *
245c30724e9SRasmus Villemoes  * However, the spelling __inline is strictly reserved for referring
246c30724e9SRasmus Villemoes  * to the bare keyword.
247c30724e9SRasmus Villemoes  */
24871391bddSXiaozhou Liu #define __inline__ inline
24971391bddSXiaozhou Liu 
25071391bddSXiaozhou Liu /*
2516863f564SMasahiro Yamada  * GCC does not warn about unused static inline functions for -Wunused-function.
2526863f564SMasahiro Yamada  * Suppress the warning in clang as well by using __maybe_unused, but enable it
2536863f564SMasahiro Yamada  * for W=1 build. This will allow clang to find unused functions. Remove the
2546863f564SMasahiro Yamada  * __inline_maybe_unused entirely after fixing most of -Wunused-function warnings.
2556863f564SMasahiro Yamada  */
2566863f564SMasahiro Yamada #ifdef KBUILD_EXTRA_WARN1
2576863f564SMasahiro Yamada #define __inline_maybe_unused
2586863f564SMasahiro Yamada #else
2596863f564SMasahiro Yamada #define __inline_maybe_unused __maybe_unused
2606863f564SMasahiro Yamada #endif
2616863f564SMasahiro Yamada 
2626863f564SMasahiro Yamada /*
26371391bddSXiaozhou Liu  * Rather then using noinline to prevent stack consumption, use
26471391bddSXiaozhou Liu  * noinline_for_stack instead.  For documentation reasons.
26571391bddSXiaozhou Liu  */
26671391bddSXiaozhou Liu #define noinline_for_stack noinline
26771391bddSXiaozhou Liu 
2681f44328eSMarco Elver /*
269a8f80673SYafang Shao  * Use noinline_for_tracing for functions that should not be inlined.
270a8f80673SYafang Shao  * For tracing reasons.
271a8f80673SYafang Shao  */
272a8f80673SYafang Shao #define noinline_for_tracing noinline
273a8f80673SYafang Shao 
274a8f80673SYafang Shao /*
2751f44328eSMarco Elver  * Sanitizer helper attributes: Because using __always_inline and
2761f44328eSMarco Elver  * __no_sanitize_* conflict, provide helper attributes that will either expand
2771f44328eSMarco Elver  * to __no_sanitize_* in compilation units where instrumentation is enabled
2781f44328eSMarco Elver  * (__SANITIZE_*__), or __always_inline in compilation units without
2791f44328eSMarco Elver  * instrumentation (__SANITIZE_*__ undefined).
2801f44328eSMarco Elver  */
2811f44328eSMarco Elver #ifdef __SANITIZE_ADDRESS__
282eb73876cSMarco Elver /*
283eb73876cSMarco Elver  * We can't declare function 'inline' because __no_sanitize_address conflicts
284eb73876cSMarco Elver  * with inlining. Attempt to inline it may cause a build failure.
285eb73876cSMarco Elver  *     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
286eb73876cSMarco Elver  * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
287eb73876cSMarco Elver  */
288eb73876cSMarco Elver # define __no_kasan_or_inline __no_sanitize_address notrace __maybe_unused
289eb73876cSMarco Elver # define __no_sanitize_or_inline __no_kasan_or_inline
290eb73876cSMarco Elver #else
291eb73876cSMarco Elver # define __no_kasan_or_inline __always_inline
292eb73876cSMarco Elver #endif
293eb73876cSMarco Elver 
294eb73876cSMarco Elver #ifdef __SANITIZE_THREAD__
295bd3d5bd1SMarco Elver /*
296bd3d5bd1SMarco Elver  * Clang still emits instrumentation for __tsan_func_{entry,exit}() and builtin
297bd3d5bd1SMarco Elver  * atomics even with __no_sanitize_thread (to avoid false positives in userspace
298bd3d5bd1SMarco Elver  * ThreadSanitizer). The kernel's requirements are stricter and we really do not
299bd3d5bd1SMarco Elver  * want any instrumentation with __no_kcsan.
300bd3d5bd1SMarco Elver  *
301bd3d5bd1SMarco Elver  * Therefore we add __disable_sanitizer_instrumentation where available to
302bd3d5bd1SMarco Elver  * disable all instrumentation. See Kconfig.kcsan where this is mandatory.
303bd3d5bd1SMarco Elver  */
304bd3d5bd1SMarco Elver # define __no_kcsan __no_sanitize_thread __disable_sanitizer_instrumentation
30531f605a3SMarco Elver /*
30631f605a3SMarco Elver  * Type qualifier to mark variables where all data-racy accesses should be
30731f605a3SMarco Elver  * ignored by KCSAN. Note, the implementation simply marks these variables as
30831f605a3SMarco Elver  * volatile, since KCSAN will treat such accesses as "marked".
30931f605a3SMarco Elver  */
31031f605a3SMarco Elver # define __data_racy volatile
311e79302aeSPeter Zijlstra # define __no_sanitize_or_inline __no_kcsan notrace __maybe_unused
312bd3d5bd1SMarco Elver #else
313bd3d5bd1SMarco Elver # define __no_kcsan
31431f605a3SMarco Elver # define __data_racy
315eb73876cSMarco Elver #endif
316eb73876cSMarco Elver 
31790d1f14cSAlexander Potapenko #ifdef __SANITIZE_MEMORY__
31890d1f14cSAlexander Potapenko /*
31990d1f14cSAlexander Potapenko  * Similarly to KASAN and KCSAN, KMSAN loses function attributes of inlined
32090d1f14cSAlexander Potapenko  * functions, therefore disabling KMSAN checks also requires disabling inlining.
32190d1f14cSAlexander Potapenko  *
32290d1f14cSAlexander Potapenko  * __no_sanitize_or_inline effectively prevents KMSAN from reporting errors
32390d1f14cSAlexander Potapenko  * within the function and marks all its outputs as initialized.
32490d1f14cSAlexander Potapenko  */
32590d1f14cSAlexander Potapenko # define __no_sanitize_or_inline __no_kmsan_checks notrace __maybe_unused
32690d1f14cSAlexander Potapenko #endif
32790d1f14cSAlexander Potapenko 
328eb73876cSMarco Elver #ifndef __no_sanitize_or_inline
329eb73876cSMarco Elver #define __no_sanitize_or_inline __always_inline
330eb73876cSMarco Elver #endif
331eb73876cSMarco Elver 
332ca7e324eSAlexander Lobakin /*
333f06e108aSJan Hendrik Farr  * Optional: only supported since gcc >= 15
334f06e108aSJan Hendrik Farr  * Optional: only supported since clang >= 18
335f06e108aSJan Hendrik Farr  *
336f06e108aSJan Hendrik Farr  *   gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
337f06e108aSJan Hendrik Farr  * clang: https://github.com/llvm/llvm-project/pull/76348
338f06e108aSJan Hendrik Farr  *
339f06e108aSJan Hendrik Farr  * __bdos on clang < 19.1.2 can erroneously return 0:
340f06e108aSJan Hendrik Farr  * https://github.com/llvm/llvm-project/pull/110497
341f06e108aSJan Hendrik Farr  *
342f06e108aSJan Hendrik Farr  * __bdos on clang < 19.1.3 can be off by 4:
343f06e108aSJan Hendrik Farr  * https://github.com/llvm/llvm-project/pull/112636
344f06e108aSJan Hendrik Farr  */
345f06e108aSJan Hendrik Farr #ifdef CONFIG_CC_HAS_COUNTED_BY
346f06e108aSJan Hendrik Farr # define __counted_by(member)		__attribute__((__counted_by__(member)))
347f06e108aSJan Hendrik Farr #else
348f06e108aSJan Hendrik Farr # define __counted_by(member)
349f06e108aSJan Hendrik Farr #endif
350f06e108aSJan Hendrik Farr 
351f06e108aSJan Hendrik Farr /*
352ca7e324eSAlexander Lobakin  * Optional: only supported since gcc >= 15
353ca7e324eSAlexander Lobakin  * Optional: not supported by Clang
354ca7e324eSAlexander Lobakin  *
355ca7e324eSAlexander Lobakin  * gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178
356ca7e324eSAlexander Lobakin  */
357ca7e324eSAlexander Lobakin #ifdef CONFIG_CC_HAS_MULTIDIMENSIONAL_NONSTRING
358ca7e324eSAlexander Lobakin # define __nonstring_array		__attribute__((__nonstring__))
359ca7e324eSAlexander Lobakin #else
360ca7e324eSAlexander Lobakin # define __nonstring_array
361ca7e324eSAlexander Lobakin #endif
362557f8c58SKees Cook 
363557f8c58SKees Cook /*
364557f8c58SKees Cook  * Apply __counted_by() when the Endianness matches to increase test coverage.
365557f8c58SKees Cook  */
366557f8c58SKees Cook #ifdef __LITTLE_ENDIAN
367557f8c58SKees Cook #define __counted_by_le(member)	__counted_by(member)
368557f8c58SKees Cook #define __counted_by_be(member)
3695ddbc408SPeter Zijlstra #else
3702b5a0e42SPeter Zijlstra #define __counted_by_le(member)
3712b5a0e42SPeter Zijlstra #define __counted_by_be(member)	__counted_by(member)
3725de0ce85SAlexander Potapenko #endif
373557f8c58SKees Cook 
3745ddbc408SPeter Zijlstra /* Do not trap wrapping arithmetic within an annotated function. */
3752b5a0e42SPeter Zijlstra #ifdef CONFIG_UBSAN_INTEGER_WRAP
3762b5a0e42SPeter Zijlstra # define __signed_wrap __attribute__((no_sanitize("signed-integer-overflow")))
3770e985e9dSPeter Zijlstra #else
3780e985e9dSPeter Zijlstra # define __signed_wrap
3790e985e9dSPeter Zijlstra #endif
3800e985e9dSPeter Zijlstra 
3810e985e9dSPeter Zijlstra /* Section for code which can't be instrumented at all */
3820e985e9dSPeter Zijlstra #define __noinstr_section(section)					\
3830e985e9dSPeter Zijlstra 	noinline notrace __attribute((__section__(section)))		\
3840e985e9dSPeter Zijlstra 	__no_kcsan __no_sanitize_address __no_profile __no_sanitize_coverage \
3850e985e9dSPeter Zijlstra 	__no_sanitize_memory __signed_wrap
3860e985e9dSPeter Zijlstra 
3872b5a0e42SPeter Zijlstra #define noinstr __noinstr_section(".noinstr.text")
3882b5a0e42SPeter Zijlstra 
389d1515582SWill Deacon /*
390d1515582SWill Deacon  * The __cpuidle section is used twofold:
391815f0ddbSNick Desaulniers  *
392d1515582SWill Deacon  *  1) the original use -- identifying if a CPU is 'stuck' in idle state based
393815f0ddbSNick Desaulniers  *     on it's instruction pointer. See cpu_in_idle().
394815f0ddbSNick Desaulniers  *
395815f0ddbSNick Desaulniers  *  2) supressing instrumentation around where cpuidle disables RCU; where the
396815f0ddbSNick Desaulniers  *     function isn't strictly required for #1, this is interchangeable with
397815f0ddbSNick Desaulniers  *     noinstr.
398d1515582SWill Deacon  */
399d1515582SWill Deacon #define __cpuidle __noinstr_section(".cpuidle.text")
400d1515582SWill Deacon 
401d1515582SWill Deacon #endif /* __KERNEL__ */
402595b893eSKees Cook 
403595b893eSKees Cook #endif /* __ASSEMBLY__ */
404595b893eSKees Cook 
405595b893eSKees Cook /*
406595b893eSKees Cook  * The below symbols may be defined for one or more, but not ALL, of the above
407595b893eSKees Cook  * compilers. We don't consider that to be an error, so set them to nothing.
408595b893eSKees Cook  * For example, some of them are for compiler specific plugins.
409d1515582SWill Deacon  */
410d1515582SWill Deacon #ifndef __latent_entropy
411d1515582SWill Deacon # define __latent_entropy
412d1515582SWill Deacon #endif
413d1515582SWill Deacon 
414d1515582SWill Deacon #if defined(RANDSTRUCT) && !defined(__CHECKER__)
415d08b9f0cSSami Tolvanen # define __randomize_layout __designated_init __attribute__((randomize_layout))
416d08b9f0cSSami Tolvanen # define __no_randomize_layout __attribute__((no_randomize_layout))
417d08b9f0cSSami Tolvanen /* This anon struct can add padding, so only enable it under randstruct. */
418d08b9f0cSSami Tolvanen # define randomized_struct_fields_start	struct {
419cf68fffbSSami Tolvanen # define randomized_struct_fields_end	} __randomize_layout;
420cf68fffbSSami Tolvanen #else
421cf68fffbSSami Tolvanen # define __randomize_layout __designated_init
422cf68fffbSSami Tolvanen # define __no_randomize_layout
42386cffecdSKees Cook # define randomized_struct_fields_start
42486cffecdSKees Cook # define randomized_struct_fields_end
4259ed9cac1SKees Cook #endif
4269ed9cac1SKees Cook 
4279ed9cac1SKees Cook #ifndef __noscs
42886cffecdSKees Cook # define __noscs
42986cffecdSKees Cook #endif
43086cffecdSKees Cook 
4319ed9cac1SKees Cook #ifndef __nocfi
43286cffecdSKees Cook # define __nocfi
43386cffecdSKees Cook #endif
4349ed9cac1SKees Cook 
43586cffecdSKees Cook /*
43686cffecdSKees Cook  * Any place that could be marked with the "alloc_size" attribute is also
43726dd68d2SPrzemek Kitszel  * a place to be marked with the "malloc" attribute, except those that may
43826dd68d2SPrzemek Kitszel  * be performing a _reallocation_, as that may alias the existing pointer.
43926dd68d2SPrzemek Kitszel  * For these, use __realloc_size().
44026dd68d2SPrzemek Kitszel  */
44126dd68d2SPrzemek Kitszel #ifdef __alloc_size__
44226dd68d2SPrzemek Kitszel # define __alloc_size(x, ...)	__alloc_size__(x, ## __VA_ARGS__) __malloc
44326dd68d2SPrzemek Kitszel # define __realloc_size(x, ...)	__alloc_size__(x, ## __VA_ARGS__)
44426dd68d2SPrzemek Kitszel #else
44526dd68d2SPrzemek Kitszel # define __alloc_size(x, ...)	__malloc
44626dd68d2SPrzemek Kitszel # define __realloc_size(x, ...)
44726dd68d2SPrzemek Kitszel #endif
44826dd68d2SPrzemek Kitszel 
449559048d1SKees Cook /*
450559048d1SKees Cook  * When the size of an allocated object is needed, use the best available
451559048d1SKees Cook  * mechanism to find it. (For cases where sizeof() cannot be used.)
452559048d1SKees Cook  */
453559048d1SKees Cook #if __has_builtin(__builtin_dynamic_object_size)
454559048d1SKees Cook #define __struct_size(p)	__builtin_dynamic_object_size(p, 0)
455559048d1SKees Cook #define __member_size(p)	__builtin_dynamic_object_size(p, 1)
45668fb3ca0SLinus Torvalds #else
45768fb3ca0SLinus Torvalds #define __struct_size(p)	__builtin_object_size(p, 0)
45868fb3ca0SLinus Torvalds #define __member_size(p)	__builtin_object_size(p, 1)
45968fb3ca0SLinus Torvalds #endif
46068fb3ca0SLinus Torvalds 
46168fb3ca0SLinus Torvalds /*
46268fb3ca0SLinus Torvalds  * Determine if an attribute has been applied to a variable.
4634356e9f8SLinus Torvalds  * Using __annotated needs to check for __annotated being available,
46468fb3ca0SLinus Torvalds  * or negative tests may fail when annotation cannot be checked. For
4658bd66d14S[email protected]  * example, see the definition of __is_cstr().
4668bd66d14S[email protected]  */
467dbaaabd6SLinus Torvalds #if __has_builtin(__builtin_has_attribute)
468dbaaabd6SLinus Torvalds #define __annotated(var, attr)	__builtin_has_attribute(var, attr)
469dbaaabd6SLinus Torvalds #endif
470dbaaabd6SLinus Torvalds 
471dbaaabd6SLinus Torvalds /*
472dbaaabd6SLinus Torvalds  * Some versions of gcc do not mark 'asm goto' volatile:
473dbaaabd6SLinus Torvalds  *
474dbaaabd6SLinus Torvalds  *  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103979
475dbaaabd6SLinus Torvalds  *
476eb111869SRasmus Villemoes  * We do it here by hand, because it doesn't hurt.
477eb111869SRasmus Villemoes  */
478eb111869SRasmus Villemoes #ifndef asm_goto_output
479eb111869SRasmus Villemoes #define asm_goto_output(x...) asm volatile goto(x)
480eb111869SRasmus Villemoes #endif
481eb111869SRasmus Villemoes 
482d1515582SWill Deacon /*
483d1515582SWill Deacon  * Clang has trouble with constraints with multiple
484d1515582SWill Deacon  * alternative behaviors (mainly "g" and "rm").
485dee081bfSWill Deacon  */
486dee081bfSWill Deacon #ifndef ASM_INPUT_G
487dee081bfSWill Deacon   #define ASM_INPUT_G "g"
4881fd76043SMarco Elver   #define ASM_INPUT_RM "rm"
4891fd76043SMarco Elver #endif
4906ec4476aSLinus Torvalds 
4911fd76043SMarco Elver #ifdef CONFIG_CC_HAS_ASM_INLINE
4921fd76043SMarco Elver #define asm_inline asm __inline
4931fd76043SMarco Elver #else
4941fd76043SMarco Elver #define asm_inline asm
4951fd76043SMarco Elver #endif
4961fd76043SMarco Elver 
4971fd76043SMarco Elver /* Are two types/vars the same type (ignoring qualifiers)? */
4981fd76043SMarco Elver #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
4991fd76043SMarco Elver 
5001fd76043SMarco Elver /*
5011fd76043SMarco Elver  * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
5021fd76043SMarco Elver  *			       non-scalar types unchanged.
5031fd76043SMarco Elver  */
5041fd76043SMarco Elver /*
5051fd76043SMarco Elver  * Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'
506dee081bfSWill Deacon  * is not type-compatible with 'signed char', and we define a separate case.
507d1515582SWill Deacon  */
508815f0ddbSNick Desaulniers #define __scalar_type_to_expr_cases(type)				\
509815f0ddbSNick Desaulniers 		unsigned type:	(unsigned type)0,			\
510815f0ddbSNick Desaulniers 		signed type:	(signed type)0
511815f0ddbSNick Desaulniers 
512eb5c2d4bSWill Deacon #define __unqual_scalar_typeof(x) typeof(				\
513eb5c2d4bSWill Deacon 		_Generic((x),						\
514eb5c2d4bSWill Deacon 			 char:	(char)0,				\
5157c00621dSMiguel Ojeda 			 __scalar_type_to_expr_cases(char),		\
5167c00621dSMiguel Ojeda 			 __scalar_type_to_expr_cases(short),		\
5177c00621dSMiguel Ojeda 			 __scalar_type_to_expr_cases(int),		\
5187c00621dSMiguel Ojeda 			 __scalar_type_to_expr_cases(long),		\
5197c00621dSMiguel Ojeda 			 __scalar_type_to_expr_cases(long long),	\
5207c00621dSMiguel Ojeda 			 default: (x)))
5217c00621dSMiguel Ojeda 
522eb5c2d4bSWill Deacon /* Is this type a native word size -- useful for atomic operations */
523eb5c2d4bSWill Deacon #define __native_word(t) \
524eb5c2d4bSWill Deacon 	(sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
525eb5c2d4bSWill Deacon 	 sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
526eb5c2d4bSWill Deacon 
527eb5c2d4bSWill Deacon #ifdef __OPTIMIZE__
528eb5c2d4bSWill Deacon # define __compiletime_assert(condition, msg, prefix, suffix)		\
529eb5c2d4bSWill Deacon 	do {								\
530eb5c2d4bSWill Deacon 		/*							\
531eb5c2d4bSWill Deacon 		 * __noreturn is needed to give the compiler enough	\
532eb5c2d4bSWill Deacon 		 * information to avoid certain possibly-uninitialized	\
533eb5c2d4bSWill Deacon 		 * warnings (regardless of the build failing).		\
534eb5c2d4bSWill Deacon 		 */							\
535eb5c2d4bSWill Deacon 		__noreturn extern void prefix ## suffix(void)		\
536eb5c2d4bSWill Deacon 			__compiletime_error(msg);			\
537eb5c2d4bSWill Deacon 		if (!(condition))					\
538eb5c2d4bSWill Deacon 			prefix ## suffix();				\
539eb5c2d4bSWill Deacon 	} while (0)
540eb5c2d4bSWill Deacon #else
541eb5c2d4bSWill Deacon # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
542eb5c2d4bSWill Deacon #endif
543eb5c2d4bSWill Deacon 
544eb5c2d4bSWill Deacon #define _compiletime_assert(condition, msg, prefix, suffix) \
545eb5c2d4bSWill Deacon 	__compiletime_assert(condition, msg, prefix, suffix)
546eb5c2d4bSWill Deacon 
547eb5c2d4bSWill Deacon /**
548815f0ddbSNick Desaulniers  * compiletime_assert - break build and emit msg if condition is false
5498793bb7fSArnd Bergmann  * @condition: a compile-time constant condition to check
5508793bb7fSArnd Bergmann  * @msg:       a message to emit if condition is false
5518793bb7fSArnd Bergmann  *
5528793bb7fSArnd Bergmann  * In tradition of POSIX assert, this macro will break the build if the
5538793bb7fSArnd Bergmann  * supplied condition is *false*, emitting the supplied error message if the
5548793bb7fSArnd Bergmann  * compiler has support to do so.
5558793bb7fSArnd Bergmann  */
5568793bb7fSArnd Bergmann #define compiletime_assert(condition, msg) \
5578793bb7fSArnd Bergmann 	_compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
5588793bb7fSArnd Bergmann 
5598793bb7fSArnd Bergmann #define compiletime_assert_atomic_type(t)				\
5608793bb7fSArnd Bergmann 	compiletime_assert(__native_word(t),				\
5618793bb7fSArnd Bergmann 		"Need native word sized stores/loads for atomicity.")
5628793bb7fSArnd Bergmann 
5638793bb7fSArnd Bergmann /* Helpers for emitting diagnostics in pragmas. */
5648793bb7fSArnd Bergmann #ifndef __diag
5658793bb7fSArnd Bergmann #define __diag(string)
5668793bb7fSArnd Bergmann #endif
5674d1ea705SKumar Kartikeya Dwivedi 
5684d1ea705SKumar Kartikeya Dwivedi #ifndef __diag_GCC
5694d1ea705SKumar Kartikeya Dwivedi #define __diag_GCC(version, severity, string)
5704d1ea705SKumar Kartikeya Dwivedi #endif
571d1515582SWill Deacon 
572 #define __diag_push()	__diag(push)
573 #define __diag_pop()	__diag(pop)
574 
575 #define __diag_ignore(compiler, version, option, comment) \
576 	__diag_ ## compiler(version, ignore, option)
577 #define __diag_warn(compiler, version, option, comment) \
578 	__diag_ ## compiler(version, warn, option)
579 #define __diag_error(compiler, version, option, comment) \
580 	__diag_ ## compiler(version, error, option)
581 
582 #ifndef __diag_ignore_all
583 #define __diag_ignore_all(option, comment)
584 #endif
585 
586 #endif /* __LINUX_COMPILER_TYPES_H */
587