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