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