include/linux/poison.h: remove obsolete comment
[linux-block.git] / include / linux / compiler_types.h
CommitLineData
66dbeef9 1/* SPDX-License-Identifier: GPL-2.0 */
d1515582
WD
2#ifndef __LINUX_COMPILER_TYPES_H
3#define __LINUX_COMPILER_TYPES_H
4
5#ifndef __ASSEMBLY__
6
7#ifdef __CHECKER__
d1515582 8# define __kernel __attribute__((address_space(0)))
670d0a4b 9# define __user __attribute__((noderef, address_space(__user)))
d1515582
WD
10# define __safe __attribute__((safe))
11# define __force __attribute__((force))
12# define __nocast __attribute__((nocast))
670d0a4b 13# define __iomem __attribute__((noderef, address_space(__iomem)))
d1515582
WD
14# define __must_hold(x) __attribute__((context(x,1,1)))
15# define __acquires(x) __attribute__((context(x,0,1)))
16# define __releases(x) __attribute__((context(x,1,0)))
17# define __acquire(x) __context__(x,1)
18# define __release(x) __context__(x,-1)
19# define __cond_lock(x,c) ((c) ? ({ __acquire(x); 1; }) : 0)
670d0a4b
LVO
20# define __percpu __attribute__((noderef, address_space(__percpu)))
21# define __rcu __attribute__((noderef, address_space(__rcu)))
d1515582
WD
22# define __private __attribute__((noderef))
23extern void __chk_user_ptr(const volatile void __user *);
24extern void __chk_io_ptr(const volatile void __iomem *);
25# define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member))
26#else /* __CHECKER__ */
27# ifdef STRUCTLEAK_PLUGIN
28# define __user __attribute__((user))
29# else
30# define __user
31# endif
32# define __kernel
33# define __safe
34# define __force
35# define __nocast
36# define __iomem
37# define __chk_user_ptr(x) (void)0
38# define __chk_io_ptr(x) (void)0
39# define __builtin_warning(x, y...) (1)
40# define __must_hold(x)
41# define __acquires(x)
42# define __releases(x)
43# define __acquire(x) (void)0
44# define __release(x) (void)0
45# define __cond_lock(x,c) (c)
46# define __percpu
47# define __rcu
48# define __private
49# define ACCESS_PRIVATE(p, member) ((p)->member)
50#endif /* __CHECKER__ */
51
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
56#ifdef __KERNEL__
57
a3f8a30f
MO
58/* Attributes */
59#include <linux/compiler_attributes.h>
60
815f0ddb 61/* Compiler specific macros. */
d1515582
WD
62#ifdef __clang__
63#include <linux/compiler-clang.h>
815f0ddb
ND
64#elif defined(__INTEL_COMPILER)
65#include <linux/compiler-intel.h>
66#elif defined(__GNUC__)
67/* The above compilers also define __GNUC__, so order is important here. */
68#include <linux/compiler-gcc.h>
69#else
70#error "Unknown compiler"
d1515582
WD
71#endif
72
04f264d3
PB
73/*
74 * Some architectures need to provide custom definitions of macros provided
75 * by linux/compiler-*.h, and can do so using asm/compiler.h. We include that
76 * conditionally rather than using an asm-generic wrapper in order to avoid
77 * build failures if any C compilation, which will include this file via an
78 * -include argument in c_flags, occurs prior to the asm-generic wrappers being
79 * generated.
80 */
81#ifdef CONFIG_HAVE_ARCH_COMPILER_H
82#include <asm/compiler.h>
83#endif
84
d1515582
WD
85struct ftrace_branch_data {
86 const char *func;
87 const char *file;
88 unsigned line;
89 union {
90 struct {
91 unsigned long correct;
92 unsigned long incorrect;
93 };
94 struct {
95 unsigned long miss;
96 unsigned long hit;
97 };
98 unsigned long miss_hit[2];
99 };
100};
101
102struct ftrace_likely_data {
103 struct ftrace_branch_data data;
104 unsigned long constant;
105};
106
71391bdd
XL
107#ifdef CONFIG_ENABLE_MUST_CHECK
108#define __must_check __attribute__((__warn_unused_result__))
109#else
110#define __must_check
111#endif
112
113#if defined(CC_USING_HOTPATCH)
114#define notrace __attribute__((hotpatch(0, 0)))
2809b392
SS
115#elif defined(CC_USING_PATCHABLE_FUNCTION_ENTRY)
116#define notrace __attribute__((patchable_function_entry(0, 0)))
71391bdd
XL
117#else
118#define notrace __attribute__((__no_instrument_function__))
119#endif
120
121/*
122 * it doesn't make sense on ARM (currently the only user of __naked)
123 * to trace naked functions because then mcount is called without
124 * stack and frame pointer being set up and there is no chance to
125 * restore the lr register to the value before mcount was called.
126 */
127#define __naked __attribute__((__naked__)) notrace
128
129#define __compiler_offsetof(a, b) __builtin_offsetof(a, b)
130
131/*
71391bdd
XL
132 * Prefer gnu_inline, so that extern inline functions do not emit an
133 * externally visible function. This makes extern inline behave as per gnu89
134 * semantics rather than c99. This prevents multiple symbol definition errors
135 * of extern inline functions at link time.
136 * A lot of inline functions can cause havoc with function tracing.
71391bdd 137 */
889b3c12 138#define inline inline __gnu_inline __inline_maybe_unused notrace
71391bdd 139
c30724e9
RV
140/*
141 * gcc provides both __inline__ and __inline as alternate spellings of
142 * the inline keyword, though the latter is undocumented. New kernel
143 * code should only use the inline spelling, but some existing code
144 * uses __inline__. Since we #define inline above, to ensure
145 * __inline__ has the same semantics, we need this #define.
146 *
147 * However, the spelling __inline is strictly reserved for referring
148 * to the bare keyword.
149 */
71391bdd 150#define __inline__ inline
71391bdd 151
6863f564
MY
152/*
153 * GCC does not warn about unused static inline functions for -Wunused-function.
154 * Suppress the warning in clang as well by using __maybe_unused, but enable it
155 * for W=1 build. This will allow clang to find unused functions. Remove the
156 * __inline_maybe_unused entirely after fixing most of -Wunused-function warnings.
157 */
158#ifdef KBUILD_EXTRA_WARN1
159#define __inline_maybe_unused
160#else
161#define __inline_maybe_unused __maybe_unused
162#endif
163
71391bdd
XL
164/*
165 * Rather then using noinline to prevent stack consumption, use
166 * noinline_for_stack instead. For documentation reasons.
167 */
168#define noinline_for_stack noinline
169
1f44328e
ME
170/*
171 * Sanitizer helper attributes: Because using __always_inline and
172 * __no_sanitize_* conflict, provide helper attributes that will either expand
173 * to __no_sanitize_* in compilation units where instrumentation is enabled
174 * (__SANITIZE_*__), or __always_inline in compilation units without
175 * instrumentation (__SANITIZE_*__ undefined).
176 */
177#ifdef __SANITIZE_ADDRESS__
eb73876c
ME
178/*
179 * We can't declare function 'inline' because __no_sanitize_address conflicts
180 * with inlining. Attempt to inline it may cause a build failure.
181 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
182 * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
183 */
184# define __no_kasan_or_inline __no_sanitize_address notrace __maybe_unused
185# define __no_sanitize_or_inline __no_kasan_or_inline
186#else
187# define __no_kasan_or_inline __always_inline
188#endif
189
190#define __no_kcsan __no_sanitize_thread
191#ifdef __SANITIZE_THREAD__
e79302ae 192# define __no_sanitize_or_inline __no_kcsan notrace __maybe_unused
eb73876c
ME
193#endif
194
195#ifndef __no_sanitize_or_inline
196#define __no_sanitize_or_inline __always_inline
197#endif
198
5ddbc408
PZ
199/* Section for code which can't be instrumented at all */
200#define noinstr \
5144f8a8 201 noinline notrace __attribute((__section__(".noinstr.text"))) \
6b643a07 202 __no_kcsan __no_sanitize_address
5ddbc408 203
d1515582
WD
204#endif /* __KERNEL__ */
205
815f0ddb
ND
206#endif /* __ASSEMBLY__ */
207
d1515582 208/*
815f0ddb
ND
209 * The below symbols may be defined for one or more, but not ALL, of the above
210 * compilers. We don't consider that to be an error, so set them to nothing.
211 * For example, some of them are for compiler specific plugins.
d1515582 212 */
d1515582
WD
213#ifndef __latent_entropy
214# define __latent_entropy
215#endif
216
217#ifndef __randomize_layout
218# define __randomize_layout __designated_init
219#endif
220
221#ifndef __no_randomize_layout
222# define __no_randomize_layout
223#endif
224
225#ifndef randomized_struct_fields_start
226# define randomized_struct_fields_start
227# define randomized_struct_fields_end
228#endif
229
d08b9f0c
ST
230#ifndef __noscs
231# define __noscs
232#endif
233
8bd66d14 234#ifndef asm_volatile_goto
235#define asm_volatile_goto(x...) asm goto(x)
236#endif
237
eb111869
RV
238#ifdef CONFIG_CC_HAS_ASM_INLINE
239#define asm_inline asm __inline
240#else
241#define asm_inline asm
242#endif
243
3193c083
JP
244#ifndef __no_fgcse
245# define __no_fgcse
246#endif
247
d1515582 248/* Are two types/vars the same type (ignoring qualifiers)? */
815f0ddb 249#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
d1515582 250
dee081bf
WD
251/*
252 * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
253 * non-scalar types unchanged.
1fd76043 254 */
1fd76043 255/*
6ec4476a 256 * Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'
1fd76043
ME
257 * is not type-compatible with 'signed char', and we define a separate case.
258 */
259#define __scalar_type_to_expr_cases(type) \
260 unsigned type: (unsigned type)0, \
261 signed type: (signed type)0
262
263#define __unqual_scalar_typeof(x) typeof( \
264 _Generic((x), \
265 char: (char)0, \
266 __scalar_type_to_expr_cases(char), \
267 __scalar_type_to_expr_cases(short), \
268 __scalar_type_to_expr_cases(int), \
269 __scalar_type_to_expr_cases(long), \
270 __scalar_type_to_expr_cases(long long), \
271 default: (x)))
dee081bf 272
d1515582 273/* Is this type a native word size -- useful for atomic operations */
815f0ddb
ND
274#define __native_word(t) \
275 (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
276 sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
277
eb5c2d4b
WD
278/* Compile time object size, -1 for unknown */
279#ifndef __compiletime_object_size
280# define __compiletime_object_size(obj) -1
281#endif
282#ifndef __compiletime_warning
283# define __compiletime_warning(message)
284#endif
285#ifndef __compiletime_error
286# define __compiletime_error(message)
287#endif
288
289#ifdef __OPTIMIZE__
290# define __compiletime_assert(condition, msg, prefix, suffix) \
291 do { \
292 extern void prefix ## suffix(void) __compiletime_error(msg); \
293 if (!(condition)) \
294 prefix ## suffix(); \
295 } while (0)
296#else
297# define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
298#endif
299
300#define _compiletime_assert(condition, msg, prefix, suffix) \
301 __compiletime_assert(condition, msg, prefix, suffix)
302
303/**
304 * compiletime_assert - break build and emit msg if condition is false
305 * @condition: a compile-time constant condition to check
306 * @msg: a message to emit if condition is false
307 *
308 * In tradition of POSIX assert, this macro will break the build if the
309 * supplied condition is *false*, emitting the supplied error message if the
310 * compiler has support to do so.
311 */
312#define compiletime_assert(condition, msg) \
313 _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
314
315#define compiletime_assert_atomic_type(t) \
316 compiletime_assert(__native_word(t), \
317 "Need native word sized stores/loads for atomicity.")
318
815f0ddb 319/* Helpers for emitting diagnostics in pragmas. */
8793bb7f
AB
320#ifndef __diag
321#define __diag(string)
322#endif
323
324#ifndef __diag_GCC
325#define __diag_GCC(version, severity, string)
326#endif
327
328#define __diag_push() __diag(push)
329#define __diag_pop() __diag(pop)
330
331#define __diag_ignore(compiler, version, option, comment) \
332 __diag_ ## compiler(version, ignore, option)
333#define __diag_warn(compiler, version, option, comment) \
334 __diag_ ## compiler(version, warn, option)
335#define __diag_error(compiler, version, option, comment) \
336 __diag_ ## compiler(version, error, option)
337
d1515582 338#endif /* __LINUX_COMPILER_TYPES_H */