percpu-defs.h 4.9 KB

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