blob: 7955429f60a192c29d283f03216388c5b03a7000 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From 9b2c282b348dfe966bbba967dc7a45ce817cce50 Mon Sep 17 00:00:00 2001
2From: Tom Rini <trini@konsulko.com>
3Date: Mon, 29 Feb 2016 11:34:15 -0500
4Subject: [PATCH] compiler*.h: sync include/linux/compiler*.h with Linux
5 4.5-rc6
6
7Copy these from Linux v4.5-rc6 tag.
8
9This is needed so that we can keep up with newer gcc versions. Note
10that we don't have the uapi/ hierarchy from the kernel so continue to
11use <linux/types.h>
12
13Signed-off-by: Tom Rini <trini@konsulko.com>
14---
15 include/linux/compiler-gcc.h | 266 ++++++++++++++++++++++++++++++++++------
16 include/linux/compiler-gcc3.h | 21 ----
17 include/linux/compiler-gcc4.h | 63 ----------
18 include/linux/compiler-intel.h | 45 +++++++
19 include/linux/compiler.h | 270 +++++++++++++++++++++++++++++++++++++++--
20 5 files changed, 534 insertions(+), 131 deletions(-)
21 delete mode 100644 include/linux/compiler-gcc3.h
22 delete mode 100644 include/linux/compiler-gcc4.h
23 create mode 100644 include/linux/compiler-intel.h
24
25diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
26index 9896e54..22ab246 100644
27--- a/include/linux/compiler-gcc.h
28+++ b/include/linux/compiler-gcc.h
29@@ -5,11 +5,28 @@
30 /*
31 * Common definitions for all gcc versions go here.
32 */
33-
34+#define GCC_VERSION (__GNUC__ * 10000 \
35+ + __GNUC_MINOR__ * 100 \
36+ + __GNUC_PATCHLEVEL__)
37
38 /* Optimization barrier */
39+
40 /* The "volatile" is due to gcc bugs */
41 #define barrier() __asm__ __volatile__("": : :"memory")
42+/*
43+ * This version is i.e. to prevent dead stores elimination on @ptr
44+ * where gcc and llvm may behave differently when otherwise using
45+ * normal barrier(): while gcc behavior gets along with a normal
46+ * barrier(), llvm needs an explicit input variable to be assumed
47+ * clobbered. The issue is as follows: while the inline asm might
48+ * access any memory it wants, the compiler could have fit all of
49+ * @ptr into memory registers instead, and since @ptr never escaped
50+ * from that, it proofed that the inline asm wasn't touching any of
51+ * it. This version works well with both compilers, i.e. we're telling
52+ * the compiler that the inline asm absolutely may see the contents
53+ * of @ptr. See also: https://llvm.org/bugs/show_bug.cgi?id=15495
54+ */
55+#define barrier_data(ptr) __asm__ __volatile__("": :"r"(ptr) :"memory")
56
57 /*
58 * This macro obfuscates arithmetic on a variable address so that gcc
59@@ -29,41 +46,63 @@
60 * the inline assembly constraint from =g to =r, in this particular
61 * case either is valid.
62 */
63-#define RELOC_HIDE(ptr, off) \
64- ({ unsigned long __ptr; \
65- __asm__ ("" : "=r"(__ptr) : "0"(ptr)); \
66- (typeof(ptr)) (__ptr + (off)); })
67+#define RELOC_HIDE(ptr, off) \
68+({ \
69+ unsigned long __ptr; \
70+ __asm__ ("" : "=r"(__ptr) : "0"(ptr)); \
71+ (typeof(ptr)) (__ptr + (off)); \
72+})
73+
74+/* Make the optimizer believe the variable can be manipulated arbitrarily. */
75+#define OPTIMIZER_HIDE_VAR(var) \
76+ __asm__ ("" : "=r" (var) : "0" (var))
77
78+#ifdef __CHECKER__
79+#define __must_be_array(a) 0
80+#else
81 /* &a[0] degrades to a pointer: a different type from an array */
82-#define __must_be_array(a) \
83- BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
84+#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
85+#endif
86
87 /*
88 * Force always-inline if the user requests it so via the .config,
89 * or if gcc is too old:
90 */
91-#if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) || \
92+#if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) || \
93 !defined(CONFIG_OPTIMIZE_INLINING) || (__GNUC__ < 4)
94-# define inline inline __attribute__((always_inline))
95-# define __inline__ __inline__ __attribute__((always_inline))
96-# define __inline __inline __attribute__((always_inline))
97+#define inline inline __attribute__((always_inline)) notrace
98+#define __inline__ __inline__ __attribute__((always_inline)) notrace
99+#define __inline __inline __attribute__((always_inline)) notrace
100+#else
101+/* A lot of inline functions can cause havoc with function tracing */
102+#define inline inline notrace
103+#define __inline__ __inline__ notrace
104+#define __inline __inline notrace
105 #endif
106
107-#define __deprecated __attribute__((deprecated))
108-#ifndef __packed
109-# define __packed __attribute__((packed))
110-#endif
111-#define __weak __attribute__((weak))
112+#define __always_inline inline __attribute__((always_inline))
113+#define noinline __attribute__((noinline))
114+
115+#define __deprecated __attribute__((deprecated))
116+#define __packed __attribute__((packed))
117+#define __weak __attribute__((weak))
118+#define __alias(symbol) __attribute__((alias(#symbol)))
119
120 /*
121- * it doesn't make sense on ARM (currently the only user of __naked) to trace
122- * naked functions because then mcount is called without stack and frame pointer
123- * being set up and there is no chance to restore the lr register to the value
124- * before mcount was called.
125+ * it doesn't make sense on ARM (currently the only user of __naked)
126+ * to trace naked functions because then mcount is called without
127+ * stack and frame pointer being set up and there is no chance to
128+ * restore the lr register to the value before mcount was called.
129+ *
130+ * The asm() bodies of naked functions often depend on standard calling
131+ * conventions, therefore they must be noinline and noclone.
132+ *
133+ * GCC 4.[56] currently fail to enforce this, so we must do so ourselves.
134+ * See GCC PR44290.
135 */
136-#define __naked __attribute__((naked)) notrace
137+#define __naked __attribute__((naked)) noinline __noclone notrace
138
139-#define __noreturn __attribute__((noreturn))
140+#define __noreturn __attribute__((noreturn))
141
142 /*
143 * From the GCC manual:
144@@ -75,19 +114,170 @@
145 * would be.
146 * [...]
147 */
148-#ifndef __pure
149-# define __pure __attribute__((pure))
150-#endif
151-#ifndef __aligned
152-# define __aligned(x) __attribute__((aligned(x)))
153-#endif
154-#define __printf(a,b) __attribute__((format(printf,a,b)))
155-#define noinline __attribute__((noinline))
156-#define __attribute_const__ __attribute__((__const__))
157-#define __maybe_unused __attribute__((unused))
158-#define __always_unused __attribute__((unused))
159-
160-#define __gcc_header(x) #x
161-#define _gcc_header(x) __gcc_header(linux/compiler-gcc##x.h)
162-#define gcc_header(x) _gcc_header(x)
163-#include gcc_header(__GNUC__)
164+#define __pure __attribute__((pure))
165+#define __aligned(x) __attribute__((aligned(x)))
166+#define __printf(a, b) __attribute__((format(printf, a, b)))
167+#define __scanf(a, b) __attribute__((format(scanf, a, b)))
168+#define __attribute_const__ __attribute__((__const__))
169+#define __maybe_unused __attribute__((unused))
170+#define __always_unused __attribute__((unused))
171+
172+/* gcc version specific checks */
173+
174+#if GCC_VERSION < 30200
175+# error Sorry, your compiler is too old - please upgrade it.
176+#endif
177+
178+#if GCC_VERSION < 30300
179+# define __used __attribute__((__unused__))
180+#else
181+# define __used __attribute__((__used__))
182+#endif
183+
184+#ifdef CONFIG_GCOV_KERNEL
185+# if GCC_VERSION < 30400
186+# error "GCOV profiling support for gcc versions below 3.4 not included"
187+# endif /* __GNUC_MINOR__ */
188+#endif /* CONFIG_GCOV_KERNEL */
189+
190+#if GCC_VERSION >= 30400
191+#define __must_check __attribute__((warn_unused_result))
192+#endif
193+
194+#if GCC_VERSION >= 40000
195+
196+/* GCC 4.1.[01] miscompiles __weak */
197+#ifdef __KERNEL__
198+# if GCC_VERSION >= 40100 && GCC_VERSION <= 40101
199+# error Your version of gcc miscompiles the __weak directive
200+# endif
201+#endif
202+
203+#define __used __attribute__((__used__))
204+#define __compiler_offsetof(a, b) \
205+ __builtin_offsetof(a, b)
206+
207+#if GCC_VERSION >= 40100 && GCC_VERSION < 40600
208+# define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
209+#endif
210+
211+#if GCC_VERSION >= 40300
212+/* Mark functions as cold. gcc will assume any path leading to a call
213+ * to them will be unlikely. This means a lot of manual unlikely()s
214+ * are unnecessary now for any paths leading to the usual suspects
215+ * like BUG(), printk(), panic() etc. [but let's keep them for now for
216+ * older compilers]
217+ *
218+ * Early snapshots of gcc 4.3 don't support this and we can't detect this
219+ * in the preprocessor, but we can live with this because they're unreleased.
220+ * Maketime probing would be overkill here.
221+ *
222+ * gcc also has a __attribute__((__hot__)) to move hot functions into
223+ * a special section, but I don't see any sense in this right now in
224+ * the kernel context
225+ */
226+#define __cold __attribute__((__cold__))
227+
228+#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
229+
230+#ifndef __CHECKER__
231+# define __compiletime_warning(message) __attribute__((warning(message)))
232+# define __compiletime_error(message) __attribute__((error(message)))
233+#endif /* __CHECKER__ */
234+#endif /* GCC_VERSION >= 40300 */
235+
236+#if GCC_VERSION >= 40500
237+/*
238+ * Mark a position in code as unreachable. This can be used to
239+ * suppress control flow warnings after asm blocks that transfer
240+ * control elsewhere.
241+ *
242+ * Early snapshots of gcc 4.5 don't support this and we can't detect
243+ * this in the preprocessor, but we can live with this because they're
244+ * unreleased. Really, we need to have autoconf for the kernel.
245+ */
246+#define unreachable() __builtin_unreachable()
247+
248+/* Mark a function definition as prohibited from being cloned. */
249+#define __noclone __attribute__((__noclone__))
250+
251+#endif /* GCC_VERSION >= 40500 */
252+
253+#if GCC_VERSION >= 40600
254+/*
255+ * When used with Link Time Optimization, gcc can optimize away C functions or
256+ * variables which are referenced only from assembly code. __visible tells the
257+ * optimizer that something else uses this function or variable, thus preventing
258+ * this.
259+ */
260+#define __visible __attribute__((externally_visible))
261+#endif
262+
263+
264+#if GCC_VERSION >= 40900 && !defined(__CHECKER__)
265+/*
266+ * __assume_aligned(n, k): Tell the optimizer that the returned
267+ * pointer can be assumed to be k modulo n. The second argument is
268+ * optional (default 0), so we use a variadic macro to make the
269+ * shorthand.
270+ *
271+ * Beware: Do not apply this to functions which may return
272+ * ERR_PTRs. Also, it is probably unwise to apply it to functions
273+ * returning extra information in the low bits (but in that case the
274+ * compiler should see some alignment anyway, when the return value is
275+ * massaged by 'flags = ptr & 3; ptr &= ~3;').
276+ */
277+#define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
278+#endif
279+
280+/*
281+ * GCC 'asm goto' miscompiles certain code sequences:
282+ *
283+ * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58670
284+ *
285+ * Work it around via a compiler barrier quirk suggested by Jakub Jelinek.
286+ *
287+ * (asm goto is automatically volatile - the naming reflects this.)
288+ */
289+#define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
290+
291+#ifdef CONFIG_ARCH_USE_BUILTIN_BSWAP
292+#if GCC_VERSION >= 40400
293+#define __HAVE_BUILTIN_BSWAP32__
294+#define __HAVE_BUILTIN_BSWAP64__
295+#endif
296+#if GCC_VERSION >= 40800 || (defined(__powerpc__) && GCC_VERSION >= 40600)
297+#define __HAVE_BUILTIN_BSWAP16__
298+#endif
299+#endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
300+
301+#if GCC_VERSION >= 50000
302+#define KASAN_ABI_VERSION 4
303+#elif GCC_VERSION >= 40902
304+#define KASAN_ABI_VERSION 3
305+#endif
306+
307+#if GCC_VERSION >= 40902
308+/*
309+ * Tell the compiler that address safety instrumentation (KASAN)
310+ * should not be applied to that function.
311+ * Conflicts with inlining: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
312+ */
313+#define __no_sanitize_address __attribute__((no_sanitize_address))
314+#endif
315+
316+#endif /* gcc version >= 40000 specific checks */
317+
318+#if !defined(__noclone)
319+#define __noclone /* not needed */
320+#endif
321+
322+#if !defined(__no_sanitize_address)
323+#define __no_sanitize_address
324+#endif
325+
326+/*
327+ * A trick to suppress uninitialized variable warning without generating any
328+ * code
329+ */
330+#define uninitialized_var(x) x = x
331diff --git a/include/linux/compiler-gcc3.h b/include/linux/compiler-gcc3.h
332deleted file mode 100644
333index 2befe65..0000000
334--- a/include/linux/compiler-gcc3.h
335+++ /dev/null
336@@ -1,21 +0,0 @@
337-#ifndef __LINUX_COMPILER_H
338-#error "Please don't include <linux/compiler-gcc3.h> directly, include <linux/compiler.h> instead."
339-#endif
340-
341-#if __GNUC_MINOR__ >= 3
342-# define __used __attribute__((__used__))
343-#else
344-# define __used __attribute__((__unused__))
345-#endif
346-
347-#if __GNUC_MINOR__ >= 4
348-#define __must_check __attribute__((warn_unused_result))
349-#endif
350-
351-/*
352- * A trick to suppress uninitialized variable warning without generating any
353- * code
354- */
355-#define uninitialized_var(x) x = x
356-
357-#define __always_inline inline __attribute__((always_inline))
358diff --git a/include/linux/compiler-gcc4.h b/include/linux/compiler-gcc4.h
359deleted file mode 100644
360index 27d11ca..0000000
361--- a/include/linux/compiler-gcc4.h
362+++ /dev/null
363@@ -1,63 +0,0 @@
364-#ifndef __LINUX_COMPILER_H
365-#error "Please don't include <linux/compiler-gcc4.h> directly, include <linux/compiler.h> instead."
366-#endif
367-
368-/* GCC 4.1.[01] miscompiles __weak */
369-#ifdef __KERNEL__
370-# if __GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ <= 1
371-# error Your version of gcc miscompiles the __weak directive
372-# endif
373-#endif
374-
375-#define __used __attribute__((__used__))
376-#define __must_check __attribute__((warn_unused_result))
377-#define __compiler_offsetof(a,b) __builtin_offsetof(a,b)
378-#ifndef __always_inline
379-# define __always_inline inline __attribute__((always_inline))
380-#endif
381-
382-/*
383- * A trick to suppress uninitialized variable warning without generating any
384- * code
385- */
386-#define uninitialized_var(x) x = x
387-
388-#if __GNUC_MINOR__ >= 3
389-/* Mark functions as cold. gcc will assume any path leading to a call
390- to them will be unlikely. This means a lot of manual unlikely()s
391- are unnecessary now for any paths leading to the usual suspects
392- like BUG(), printk(), panic() etc. [but let's keep them for now for
393- older compilers]
394-
395- Early snapshots of gcc 4.3 don't support this and we can't detect this
396- in the preprocessor, but we can live with this because they're unreleased.
397- Maketime probing would be overkill here.
398-
399- gcc also has a __attribute__((__hot__)) to move hot functions into
400- a special section, but I don't see any sense in this right now in
401- the kernel context */
402-#define __cold __attribute__((__cold__))
403-
404-
405-#if __GNUC_MINOR__ >= 5
406-/*
407- * Mark a position in code as unreachable. This can be used to
408- * suppress control flow warnings after asm blocks that transfer
409- * control elsewhere.
410- *
411- * Early snapshots of gcc 4.5 don't support this and we can't detect
412- * this in the preprocessor, but we can live with this because they're
413- * unreleased. Really, we need to have autoconf for the kernel.
414- */
415-#define unreachable() __builtin_unreachable()
416-#endif
417-
418-#endif
419-
420-#if __GNUC_MINOR__ > 0
421-#define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
422-#endif
423-#if __GNUC_MINOR__ >= 4
424-#define __compiletime_warning(message) __attribute__((warning(message)))
425-#define __compiletime_error(message) __attribute__((error(message)))
426-#endif
427diff --git a/include/linux/compiler-intel.h b/include/linux/compiler-intel.h
428new file mode 100644
429index 0000000..d4c7113
430--- /dev/null
431+++ b/include/linux/compiler-intel.h
432@@ -0,0 +1,45 @@
433+#ifndef __LINUX_COMPILER_H
434+#error "Please don't include <linux/compiler-intel.h> directly, include <linux/compiler.h> instead."
435+#endif
436+
437+#ifdef __ECC
438+
439+/* Some compiler specific definitions are overwritten here
440+ * for Intel ECC compiler
441+ */
442+
443+#include <asm/intrinsics.h>
444+
445+/* Intel ECC compiler doesn't support gcc specific asm stmts.
446+ * It uses intrinsics to do the equivalent things.
447+ */
448+#undef barrier
449+#undef barrier_data
450+#undef RELOC_HIDE
451+#undef OPTIMIZER_HIDE_VAR
452+
453+#define barrier() __memory_barrier()
454+#define barrier_data(ptr) barrier()
455+
456+#define RELOC_HIDE(ptr, off) \
457+ ({ unsigned long __ptr; \
458+ __ptr = (unsigned long) (ptr); \
459+ (typeof(ptr)) (__ptr + (off)); })
460+
461+/* This should act as an optimization barrier on var.
462+ * Given that this compiler does not have inline assembly, a compiler barrier
463+ * is the best we can do.
464+ */
465+#define OPTIMIZER_HIDE_VAR(var) barrier()
466+
467+/* Intel ECC compiler doesn't support __builtin_types_compatible_p() */
468+#define __must_be_array(a) 0
469+
470+#endif
471+
472+#ifndef __HAVE_BUILTIN_BSWAP16__
473+/* icc has this, but it's called _bswap16 */
474+#define __HAVE_BUILTIN_BSWAP16__
475+#define __builtin_bswap16 _bswap16
476+#endif
477+
478diff --git a/include/linux/compiler.h b/include/linux/compiler.h
479index 5be3dab..020ad16 100644
480--- a/include/linux/compiler.h
481+++ b/include/linux/compiler.h
482@@ -5,16 +5,24 @@
483
484 #ifdef __CHECKER__
485 # define __user __attribute__((noderef, address_space(1)))
486-# define __kernel /* default address space */
487+# define __kernel __attribute__((address_space(0)))
488 # define __safe __attribute__((safe))
489 # define __force __attribute__((force))
490 # define __nocast __attribute__((nocast))
491 # define __iomem __attribute__((noderef, address_space(2)))
492+# define __must_hold(x) __attribute__((context(x,1,1)))
493 # define __acquires(x) __attribute__((context(x,0,1)))
494 # define __releases(x) __attribute__((context(x,1,0)))
495 # define __acquire(x) __context__(x,1)
496 # define __release(x) __context__(x,-1)
497 # define __cond_lock(x,c) ((c) ? ({ __acquire(x); 1; }) : 0)
498+# define __percpu __attribute__((noderef, address_space(3)))
499+# define __pmem __attribute__((noderef, address_space(5)))
500+#ifdef CONFIG_SPARSE_RCU_POINTER
501+# define __rcu __attribute__((noderef, address_space(4)))
502+#else
503+# define __rcu
504+#endif
505 extern void __chk_user_ptr(const volatile void __user *);
506 extern void __chk_io_ptr(const volatile void __iomem *);
507 #else
508@@ -27,20 +35,32 @@ extern void __chk_io_ptr(const volatile void __iomem *);
509 # define __chk_user_ptr(x) (void)0
510 # define __chk_io_ptr(x) (void)0
511 # define __builtin_warning(x, y...) (1)
512+# define __must_hold(x)
513 # define __acquires(x)
514 # define __releases(x)
515 # define __acquire(x) (void)0
516 # define __release(x) (void)0
517 # define __cond_lock(x,c) (c)
518+# define __percpu
519+# define __rcu
520+# define __pmem
521 #endif
522
523+/* Indirect macros required for expanded argument pasting, eg. __LINE__. */
524+#define ___PASTE(a,b) a##b
525+#define __PASTE(a,b) ___PASTE(a,b)
526+
527 #ifdef __KERNEL__
528
529 #ifdef __GNUC__
530 #include <linux/compiler-gcc.h>
531 #endif
532
533+#if defined(CC_USING_HOTPATCH) && !defined(__CHECKER__)
534+#define notrace __attribute__((hotpatch(0,0)))
535+#else
536 #define notrace __attribute__((no_instrument_function))
537+#endif
538
539 /* Intel compiler defines __GNUC__. So we will overwrite implementations
540 * coming from above header files here
541@@ -49,6 +69,13 @@ extern void __chk_io_ptr(const volatile void __iomem *);
542 # include <linux/compiler-intel.h>
543 #endif
544
545+/* Clang compiler defines __GNUC__. So we will overwrite implementations
546+ * coming from above header files here
547+ */
548+#ifdef __clang__
549+#include <linux/compiler-clang.h>
550+#endif
551+
552 /*
553 * Generic compiler-dependent macros required for kernel
554 * build go below this comment. Actual compiler/compiler version
555@@ -117,7 +144,7 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
556 */
557 #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
558 #define __trace_if(cond) \
559- if (__builtin_constant_p((cond)) ? !!(cond) : \
560+ if (__builtin_constant_p(!!(cond)) ? !!(cond) : \
561 ({ \
562 int ______r; \
563 static struct ftrace_branch_data \
564@@ -144,6 +171,10 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
565 # define barrier() __memory_barrier()
566 #endif
567
568+#ifndef barrier_data
569+# define barrier_data(ptr) barrier()
570+#endif
571+
572 /* Unreachable code */
573 #ifndef unreachable
574 # define unreachable() do { } while (1)
575@@ -156,6 +187,135 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
576 (typeof(ptr)) (__ptr + (off)); })
577 #endif
578
579+#ifndef OPTIMIZER_HIDE_VAR
580+#define OPTIMIZER_HIDE_VAR(var) barrier()
581+#endif
582+
583+/* Not-quite-unique ID. */
584+#ifndef __UNIQUE_ID
585+# define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
586+#endif
587+
588+#include <linux/types.h>
589+
590+#define __READ_ONCE_SIZE \
591+({ \
592+ switch (size) { \
593+ case 1: *(__u8 *)res = *(volatile __u8 *)p; break; \
594+ case 2: *(__u16 *)res = *(volatile __u16 *)p; break; \
595+ case 4: *(__u32 *)res = *(volatile __u32 *)p; break; \
596+ case 8: *(__u64 *)res = *(volatile __u64 *)p; break; \
597+ default: \
598+ barrier(); \
599+ __builtin_memcpy((void *)res, (const void *)p, size); \
600+ barrier(); \
601+ } \
602+})
603+
604+static __always_inline
605+void __read_once_size(const volatile void *p, void *res, int size)
606+{
607+ __READ_ONCE_SIZE;
608+}
609+
610+#ifdef CONFIG_KASAN
611+/*
612+ * This function is not 'inline' because __no_sanitize_address confilcts
613+ * with inlining. Attempt to inline it may cause a build failure.
614+ * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
615+ * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
616+ */
617+static __no_sanitize_address __maybe_unused
618+void __read_once_size_nocheck(const volatile void *p, void *res, int size)
619+{
620+ __READ_ONCE_SIZE;
621+}
622+#else
623+static __always_inline
624+void __read_once_size_nocheck(const volatile void *p, void *res, int size)
625+{
626+ __READ_ONCE_SIZE;
627+}
628+#endif
629+
630+static __always_inline void __write_once_size(volatile void *p, void *res, int size)
631+{
632+ switch (size) {
633+ case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
634+ case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
635+ case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
636+ case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
637+ default:
638+ barrier();
639+ __builtin_memcpy((void *)p, (const void *)res, size);
640+ barrier();
641+ }
642+}
643+
644+/*
645+ * Prevent the compiler from merging or refetching reads or writes. The
646+ * compiler is also forbidden from reordering successive instances of
647+ * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
648+ * compiler is aware of some particular ordering. One way to make the
649+ * compiler aware of ordering is to put the two invocations of READ_ONCE,
650+ * WRITE_ONCE or ACCESS_ONCE() in different C statements.
651+ *
652+ * In contrast to ACCESS_ONCE these two macros will also work on aggregate
653+ * data types like structs or unions. If the size of the accessed data
654+ * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
655+ * READ_ONCE() and WRITE_ONCE() will fall back to memcpy and print a
656+ * compile-time warning.
657+ *
658+ * Their two major use cases are: (1) Mediating communication between
659+ * process-level code and irq/NMI handlers, all running on the same CPU,
660+ * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
661+ * mutilate accesses that either do not require ordering or that interact
662+ * with an explicit memory barrier or atomic instruction that provides the
663+ * required ordering.
664+ */
665+
666+#define __READ_ONCE(x, check) \
667+({ \
668+ union { typeof(x) __val; char __c[1]; } __u; \
669+ if (check) \
670+ __read_once_size(&(x), __u.__c, sizeof(x)); \
671+ else \
672+ __read_once_size_nocheck(&(x), __u.__c, sizeof(x)); \
673+ __u.__val; \
674+})
675+#define READ_ONCE(x) __READ_ONCE(x, 1)
676+
677+/*
678+ * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
679+ * to hide memory access from KASAN.
680+ */
681+#define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
682+
683+#define WRITE_ONCE(x, val) \
684+({ \
685+ union { typeof(x) __val; char __c[1]; } __u = \
686+ { .__val = (__force typeof(x)) (val) }; \
687+ __write_once_size(&(x), __u.__c, sizeof(x)); \
688+ __u.__val; \
689+})
690+
691+/**
692+ * smp_cond_acquire() - Spin wait for cond with ACQUIRE ordering
693+ * @cond: boolean expression to wait for
694+ *
695+ * Equivalent to using smp_load_acquire() on the condition variable but employs
696+ * the control dependency of the wait to reduce the barrier on many platforms.
697+ *
698+ * The control dependency provides a LOAD->STORE order, the additional RMB
699+ * provides LOAD->LOAD order, together they provide LOAD->{LOAD,STORE} order,
700+ * aka. ACQUIRE.
701+ */
702+#define smp_cond_acquire(cond) do { \
703+ while (!(cond)) \
704+ cpu_relax(); \
705+ smp_rmb(); /* ctrl + rmb := acquire */ \
706+} while (0)
707+
708 #endif /* __KERNEL__ */
709
710 #endif /* __ASSEMBLY__ */
711@@ -228,7 +388,7 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
712
713 /*
714 * Rather then using noinline to prevent stack consumption, use
715- * noinline_for_stack instead. For documentaiton reasons.
716+ * noinline_for_stack instead. For documentation reasons.
717 */
718 #define noinline_for_stack noinline
719
720@@ -270,11 +430,28 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
721 # define __section(S) __attribute__ ((__section__(#S)))
722 #endif
723
724+#ifndef __visible
725+#define __visible
726+#endif
727+
728+/*
729+ * Assume alignment of return value.
730+ */
731+#ifndef __assume_aligned
732+#define __assume_aligned(a, ...)
733+#endif
734+
735+
736 /* Are two types/vars the same type (ignoring qualifiers)? */
737 #ifndef __same_type
738 # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
739 #endif
740
741+/* Is this type a native word size -- useful for atomic operations */
742+#ifndef __native_word
743+# define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
744+#endif
745+
746 /* Compile time object size, -1 for unknown */
747 #ifndef __compiletime_object_size
748 # define __compiletime_object_size(obj) -1
749@@ -284,7 +461,48 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
750 #endif
751 #ifndef __compiletime_error
752 # define __compiletime_error(message)
753+/*
754+ * Sparse complains of variable sized arrays due to the temporary variable in
755+ * __compiletime_assert. Unfortunately we can't just expand it out to make
756+ * sparse see a constant array size without breaking compiletime_assert on old
757+ * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
758+ */
759+# ifndef __CHECKER__
760+# define __compiletime_error_fallback(condition) \
761+ do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
762+# endif
763 #endif
764+#ifndef __compiletime_error_fallback
765+# define __compiletime_error_fallback(condition) do { } while (0)
766+#endif
767+
768+#define __compiletime_assert(condition, msg, prefix, suffix) \
769+ do { \
770+ bool __cond = !(condition); \
771+ extern void prefix ## suffix(void) __compiletime_error(msg); \
772+ if (__cond) \
773+ prefix ## suffix(); \
774+ __compiletime_error_fallback(__cond); \
775+ } while (0)
776+
777+#define _compiletime_assert(condition, msg, prefix, suffix) \
778+ __compiletime_assert(condition, msg, prefix, suffix)
779+
780+/**
781+ * compiletime_assert - break build and emit msg if condition is false
782+ * @condition: a compile-time constant condition to check
783+ * @msg: a message to emit if condition is false
784+ *
785+ * In tradition of POSIX assert, this macro will break the build if the
786+ * supplied condition is *false*, emitting the supplied error message if the
787+ * compiler has support to do so.
788+ */
789+#define compiletime_assert(condition, msg) \
790+ _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
791+
792+#define compiletime_assert_atomic_type(t) \
793+ compiletime_assert(__native_word(t), \
794+ "Need native word sized stores/loads for atomicity.")
795
796 /*
797 * Prevent the compiler from merging or refetching accesses. The compiler
798@@ -293,11 +511,45 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
799 * to make the compiler aware of ordering is to put the two invocations of
800 * ACCESS_ONCE() in different C statements.
801 *
802- * This macro does absolutely -nothing- to prevent the CPU from reordering,
803- * merging, or refetching absolutely anything at any time. Its main intended
804- * use is to mediate communication between process-level code and irq/NMI
805- * handlers, all running on the same CPU.
806+ * ACCESS_ONCE will only work on scalar types. For union types, ACCESS_ONCE
807+ * on a union member will work as long as the size of the member matches the
808+ * size of the union and the size is smaller than word size.
809+ *
810+ * The major use cases of ACCESS_ONCE used to be (1) Mediating communication
811+ * between process-level code and irq/NMI handlers, all running on the same CPU,
812+ * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
813+ * mutilate accesses that either do not require ordering or that interact
814+ * with an explicit memory barrier or atomic instruction that provides the
815+ * required ordering.
816+ *
817+ * If possible use READ_ONCE()/WRITE_ONCE() instead.
818 */
819-#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
820-
821+#define __ACCESS_ONCE(x) ({ \
822+ __maybe_unused typeof(x) __var = (__force typeof(x)) 0; \
823+ (volatile typeof(x) *)&(x); })
824+#define ACCESS_ONCE(x) (*__ACCESS_ONCE(x))
825+
826+/**
827+ * lockless_dereference() - safely load a pointer for later dereference
828+ * @p: The pointer to load
829+ *
830+ * Similar to rcu_dereference(), but for situations where the pointed-to
831+ * object's lifetime is managed by something other than RCU. That
832+ * "something other" might be reference counting or simple immortality.
833+ */
834+#define lockless_dereference(p) \
835+({ \
836+ typeof(p) _________p1 = READ_ONCE(p); \
837+ smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
838+ (_________p1); \
839+})
840+
841+/* Ignore/forbid kprobes attach on very low level functions marked by this attribute: */
842+#ifdef CONFIG_KPROBES
843+# define __kprobes __attribute__((__section__(".kprobes.text")))
844+# define nokprobe_inline __always_inline
845+#else
846+# define __kprobes
847+# define nokprobe_inline inline
848+#endif
849 #endif /* __LINUX_COMPILER_H */
850--
8512.7.4
852