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