percpu-defs.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef _LINUX_PERCPU_DEFS_H
  2. #define _LINUX_PERCPU_DEFS_H
  3. /*
  4. * Base implementations of per-CPU variable declarations and definitions, where
  5. * the section in which the variable is to be placed is provided by the
  6. * 'sec' argument. This may be used to affect the parameters governing the
  7. * variable's storage.
  8. *
  9. * NOTE! The sections for the DECLARE and for the DEFINE must match, lest
  10. * linkage errors occur due the compiler generating the wrong code to access
  11. * that section.
  12. */
  13. #define __PCPU_ATTRS(sec) \
  14. __percpu __attribute__((section(PER_CPU_BASE_SECTION sec))) \
  15. PER_CPU_ATTRIBUTES
  16. #define __PCPU_DUMMY_ATTRS \
  17. __attribute__((section(".discard"), unused))
  18. /*
  19. * Macro which verifies @ptr is a percpu pointer without evaluating
  20. * @ptr. This is to be used in percpu accessors to verify that the
  21. * input parameter is a percpu pointer.
  22. */
  23. #define __verify_pcpu_ptr(ptr) do { \
  24. const void __percpu *__vpp_verify = (typeof(ptr))NULL; \
  25. (void)__vpp_verify; \
  26. } while (0)
  27. /*
  28. * s390 and alpha modules require percpu variables to be defined as
  29. * weak to force the compiler to generate GOT based external
  30. * references for them. This is necessary because percpu sections
  31. * will be located outside of the usually addressable area.
  32. *
  33. * This definition puts the following two extra restrictions when
  34. * defining percpu variables.
  35. *
  36. * 1. The symbol must be globally unique, even the static ones.
  37. * 2. Static percpu variables cannot be defined inside a function.
  38. *
  39. * Archs which need weak percpu definitions should define
  40. * ARCH_NEEDS_WEAK_PER_CPU in asm/percpu.h when necessary.
  41. *
  42. * To ensure that the generic code observes the above two
  43. * restrictions, if CONFIG_DEBUG_FORCE_WEAK_PER_CPU is set weak
  44. * definition is used for all cases.
  45. */
  46. #if defined(ARCH_NEEDS_WEAK_PER_CPU) || defined(CONFIG_DEBUG_FORCE_WEAK_PER_CPU)
  47. /*
  48. * __pcpu_scope_* dummy variable is used to enforce scope. It
  49. * receives the static modifier when it's used in front of
  50. * DEFINE_PER_CPU() and will trigger build failure if
  51. * DECLARE_PER_CPU() is used for the same variable.
  52. *
  53. * __pcpu_unique_* dummy variable is used to enforce symbol uniqueness
  54. * such that hidden weak symbol collision, which will cause unrelated
  55. * variables to share the same address, can be detected during build.
  56. */
  57. #define DECLARE_PER_CPU_SECTION(type, name, sec) \
  58. extern __PCPU_DUMMY_ATTRS char __pcpu_scope_##name; \
  59. extern __PCPU_ATTRS(sec) __typeof__(type) name
  60. #define DEFINE_PER_CPU_SECTION(type, name, sec) \
  61. __PCPU_DUMMY_ATTRS char __pcpu_scope_##name; \
  62. extern __PCPU_DUMMY_ATTRS char __pcpu_unique_##name; \
  63. __PCPU_DUMMY_ATTRS char __pcpu_unique_##name; \
  64. __PCPU_ATTRS(sec) PER_CPU_DEF_ATTRIBUTES __weak \
  65. __typeof__(type) name
  66. #else
  67. /*
  68. * Normal declaration and definition macros.
  69. */
  70. #define DECLARE_PER_CPU_SECTION(type, name, sec) \
  71. extern __PCPU_ATTRS(sec) __typeof__(type) name
  72. #define DEFINE_PER_CPU_SECTION(type, name, sec) \
  73. __PCPU_ATTRS(sec) PER_CPU_DEF_ATTRIBUTES \
  74. __typeof__(type) name
  75. #endif
  76. /*
  77. * Variant on the per-CPU variable declaration/definition theme used for
  78. * ordinary per-CPU variables.
  79. */
  80. #define DECLARE_PER_CPU(type, name) \
  81. DECLARE_PER_CPU_SECTION(type, name, "")
  82. #define DEFINE_PER_CPU(type, name) \
  83. DEFINE_PER_CPU_SECTION(type, name, "")
  84. /*
  85. * Declaration/definition used for per-CPU variables that must come first in
  86. * the set of variables.
  87. */
  88. #define DECLARE_PER_CPU_FIRST(type, name) \
  89. DECLARE_PER_CPU_SECTION(type, name, PER_CPU_FIRST_SECTION)
  90. #define DEFINE_PER_CPU_FIRST(type, name) \
  91. DEFINE_PER_CPU_SECTION(type, name, PER_CPU_FIRST_SECTION)
  92. /*
  93. * Declaration/definition used for per-CPU variables that must be cacheline
  94. * aligned under SMP conditions so that, whilst a particular instance of the
  95. * data corresponds to a particular CPU, inefficiencies due to direct access by
  96. * other CPUs are reduced by preventing the data from unnecessarily spanning
  97. * cachelines.
  98. *
  99. * An example of this would be statistical data, where each CPU's set of data
  100. * is updated by that CPU alone, but the data from across all CPUs is collated
  101. * by a CPU processing a read from a proc file.
  102. */
  103. #define DECLARE_PER_CPU_SHARED_ALIGNED(type, name) \
  104. DECLARE_PER_CPU_SECTION(type, name, PER_CPU_SHARED_ALIGNED_SECTION) \
  105. ____cacheline_aligned_in_smp
  106. #define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
  107. DEFINE_PER_CPU_SECTION(type, name, PER_CPU_SHARED_ALIGNED_SECTION) \
  108. ____cacheline_aligned_in_smp
  109. #define DECLARE_PER_CPU_ALIGNED(type, name) \
  110. DECLARE_PER_CPU_SECTION(type, name, PER_CPU_ALIGNED_SECTION) \
  111. ____cacheline_aligned
  112. #define DEFINE_PER_CPU_ALIGNED(type, name) \
  113. DEFINE_PER_CPU_SECTION(type, name, PER_CPU_ALIGNED_SECTION) \
  114. ____cacheline_aligned
  115. /*
  116. * Declaration/definition used for per-CPU variables that must be page aligned.
  117. */
  118. #define DECLARE_PER_CPU_PAGE_ALIGNED(type, name) \
  119. DECLARE_PER_CPU_SECTION(type, name, "..page_aligned") \
  120. __aligned(PAGE_SIZE)
  121. #define DEFINE_PER_CPU_PAGE_ALIGNED(type, name) \
  122. DEFINE_PER_CPU_SECTION(type, name, "..page_aligned") \
  123. __aligned(PAGE_SIZE)
  124. /*
  125. * Declaration/definition used for per-CPU variables that must be read mostly.
  126. */
  127. #define DECLARE_PER_CPU_READ_MOSTLY(type, name) \
  128. DECLARE_PER_CPU_SECTION(type, name, "..readmostly")
  129. #define DEFINE_PER_CPU_READ_MOSTLY(type, name) \
  130. DEFINE_PER_CPU_SECTION(type, name, "..readmostly")
  131. /*
  132. * Intermodule exports for per-CPU variables. sparse forgets about
  133. * address space across EXPORT_SYMBOL(), change EXPORT_SYMBOL() to
  134. * noop if __CHECKER__.
  135. */
  136. #ifndef __CHECKER__
  137. #define EXPORT_PER_CPU_SYMBOL(var) EXPORT_SYMBOL(var)
  138. #define EXPORT_PER_CPU_SYMBOL_GPL(var) EXPORT_SYMBOL_GPL(var)
  139. #else
  140. #define EXPORT_PER_CPU_SYMBOL(var)
  141. #define EXPORT_PER_CPU_SYMBOL_GPL(var)
  142. #endif
  143. #endif /* _LINUX_PERCPU_DEFS_H */