percpu-defs.h 5.1 KB

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