percpu-defs.h 5.9 KB

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