1
0

200-compiler-support.patch 25 KB

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