rwonce.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020 Google LLC.
  4. */
  5. #ifndef __ASM_RWONCE_H
  6. #define __ASM_RWONCE_H
  7. #if defined(CONFIG_LTO) && !defined(__ASSEMBLY__)
  8. #include <linux/compiler_types.h>
  9. #include <asm/alternative-macros.h>
  10. #ifndef BUILD_VDSO
  11. #ifdef CONFIG_AS_HAS_LDAPR
  12. #define __LOAD_RCPC(sfx, regs...) \
  13. ALTERNATIVE( \
  14. "ldar" #sfx "\t" #regs, \
  15. ".arch_extension rcpc\n" \
  16. "ldapr" #sfx "\t" #regs, \
  17. ARM64_HAS_LDAPR)
  18. #else
  19. #define __LOAD_RCPC(sfx, regs...) "ldar" #sfx "\t" #regs
  20. #endif /* CONFIG_AS_HAS_LDAPR */
  21. /*
  22. * When building with LTO, there is an increased risk of the compiler
  23. * converting an address dependency headed by a READ_ONCE() invocation
  24. * into a control dependency and consequently allowing for harmful
  25. * reordering by the CPU.
  26. *
  27. * Ensure that such transformations are harmless by overriding the generic
  28. * READ_ONCE() definition with one that provides RCpc acquire semantics
  29. * when building with LTO.
  30. */
  31. #define __READ_ONCE(x) \
  32. ({ \
  33. typeof(&(x)) __x = &(x); \
  34. int atomic = 1; \
  35. union { __unqual_scalar_typeof(*__x) __val; char __c[1]; } __u; \
  36. switch (sizeof(x)) { \
  37. case 1: \
  38. asm volatile(__LOAD_RCPC(b, %w0, %1) \
  39. : "=r" (*(__u8 *)__u.__c) \
  40. : "Q" (*__x) : "memory"); \
  41. break; \
  42. case 2: \
  43. asm volatile(__LOAD_RCPC(h, %w0, %1) \
  44. : "=r" (*(__u16 *)__u.__c) \
  45. : "Q" (*__x) : "memory"); \
  46. break; \
  47. case 4: \
  48. asm volatile(__LOAD_RCPC(, %w0, %1) \
  49. : "=r" (*(__u32 *)__u.__c) \
  50. : "Q" (*__x) : "memory"); \
  51. break; \
  52. case 8: \
  53. asm volatile(__LOAD_RCPC(, %0, %1) \
  54. : "=r" (*(__u64 *)__u.__c) \
  55. : "Q" (*__x) : "memory"); \
  56. break; \
  57. default: \
  58. atomic = 0; \
  59. } \
  60. atomic ? (typeof(*__x))__u.__val : (*(volatile typeof(__x))__x);\
  61. })
  62. #endif /* !BUILD_VDSO */
  63. #endif /* CONFIG_LTO && !__ASSEMBLY__ */
  64. #include <asm-generic/rwonce.h>
  65. #endif /* __ASM_RWONCE_H */