0028-gcc-compat.patch 29 KB

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