percpu-defs.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. __PCPU_DUMMY_ATTRS char __pcpu_unique_##name; \
  59. __PCPU_ATTRS(sec) PER_CPU_DEF_ATTRIBUTES __weak \
  60. __typeof__(type) per_cpu__##name
  61. #else
  62. /*
  63. * Normal declaration and definition macros.
  64. */
  65. #define DECLARE_PER_CPU_SECTION(type, name, sec) \
  66. extern __PCPU_ATTRS(sec) __typeof__(type) per_cpu__##name
  67. #define DEFINE_PER_CPU_SECTION(type, name, sec) \
  68. __PCPU_ATTRS(sec) PER_CPU_DEF_ATTRIBUTES \
  69. __typeof__(type) per_cpu__##name
  70. #endif
  71. /*
  72. * Variant on the per-CPU variable declaration/definition theme used for
  73. * ordinary per-CPU variables.
  74. */
  75. #define DECLARE_PER_CPU(type, name) \
  76. DECLARE_PER_CPU_SECTION(type, name, "")
  77. #define DEFINE_PER_CPU(type, name) \
  78. DEFINE_PER_CPU_SECTION(type, name, "")
  79. /*
  80. * Declaration/definition used for per-CPU variables that must come first in
  81. * the set of variables.
  82. */
  83. #define DECLARE_PER_CPU_FIRST(type, name) \
  84. DECLARE_PER_CPU_SECTION(type, name, PER_CPU_FIRST_SECTION)
  85. #define DEFINE_PER_CPU_FIRST(type, name) \
  86. DEFINE_PER_CPU_SECTION(type, name, PER_CPU_FIRST_SECTION)
  87. /*
  88. * Declaration/definition used for per-CPU variables that must be cacheline
  89. * aligned under SMP conditions so that, whilst a particular instance of the
  90. * data corresponds to a particular CPU, inefficiencies due to direct access by
  91. * other CPUs are reduced by preventing the data from unnecessarily spanning
  92. * cachelines.
  93. *
  94. * An example of this would be statistical data, where each CPU's set of data
  95. * is updated by that CPU alone, but the data from across all CPUs is collated
  96. * by a CPU processing a read from a proc file.
  97. */
  98. #define DECLARE_PER_CPU_SHARED_ALIGNED(type, name) \
  99. DECLARE_PER_CPU_SECTION(type, name, PER_CPU_SHARED_ALIGNED_SECTION) \
  100. ____cacheline_aligned_in_smp
  101. #define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
  102. DEFINE_PER_CPU_SECTION(type, name, PER_CPU_SHARED_ALIGNED_SECTION) \
  103. ____cacheline_aligned_in_smp
  104. #define DECLARE_PER_CPU_ALIGNED(type, name) \
  105. DECLARE_PER_CPU_SECTION(type, name, PER_CPU_ALIGNED_SECTION) \
  106. ____cacheline_aligned
  107. #define DEFINE_PER_CPU_ALIGNED(type, name) \
  108. DEFINE_PER_CPU_SECTION(type, name, PER_CPU_ALIGNED_SECTION) \
  109. ____cacheline_aligned
  110. /*
  111. * Declaration/definition used for per-CPU variables that must be page aligned.
  112. */
  113. #define DECLARE_PER_CPU_PAGE_ALIGNED(type, name) \
  114. DECLARE_PER_CPU_SECTION(type, name, ".page_aligned") \
  115. __aligned(PAGE_SIZE)
  116. #define DEFINE_PER_CPU_PAGE_ALIGNED(type, name) \
  117. DEFINE_PER_CPU_SECTION(type, name, ".page_aligned") \
  118. __aligned(PAGE_SIZE)
  119. /*
  120. * Intermodule exports for per-CPU variables.
  121. */
  122. #define EXPORT_PER_CPU_SYMBOL(var) EXPORT_SYMBOL(per_cpu__##var)
  123. #define EXPORT_PER_CPU_SYMBOL_GPL(var) EXPORT_SYMBOL_GPL(per_cpu__##var)
  124. #endif /* _LINUX_PERCPU_DEFS_H */