percpu.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #ifndef __LINUX_PERCPU_H
  2. #define __LINUX_PERCPU_H
  3. #include <linux/preempt.h>
  4. #include <linux/slab.h> /* For kmalloc() */
  5. #include <linux/smp.h>
  6. #include <linux/cpumask.h>
  7. #include <asm/percpu.h>
  8. #ifndef PER_CPU_BASE_SECTION
  9. #ifdef CONFIG_SMP
  10. #define PER_CPU_BASE_SECTION ".data.percpu"
  11. #else
  12. #define PER_CPU_BASE_SECTION ".data"
  13. #endif
  14. #endif
  15. #ifdef CONFIG_SMP
  16. #ifdef MODULE
  17. #define PER_CPU_SHARED_ALIGNED_SECTION ""
  18. #else
  19. #define PER_CPU_SHARED_ALIGNED_SECTION ".shared_aligned"
  20. #endif
  21. #define PER_CPU_FIRST_SECTION ".first"
  22. #else
  23. #define PER_CPU_SHARED_ALIGNED_SECTION ""
  24. #define PER_CPU_FIRST_SECTION ""
  25. #endif
  26. #define DEFINE_PER_CPU_SECTION(type, name, section) \
  27. __attribute__((__section__(PER_CPU_BASE_SECTION section))) \
  28. PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name
  29. #define DEFINE_PER_CPU(type, name) \
  30. DEFINE_PER_CPU_SECTION(type, name, "")
  31. #define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
  32. DEFINE_PER_CPU_SECTION(type, name, PER_CPU_SHARED_ALIGNED_SECTION) \
  33. ____cacheline_aligned_in_smp
  34. #define DEFINE_PER_CPU_PAGE_ALIGNED(type, name) \
  35. DEFINE_PER_CPU_SECTION(type, name, ".page_aligned")
  36. #define DEFINE_PER_CPU_FIRST(type, name) \
  37. DEFINE_PER_CPU_SECTION(type, name, PER_CPU_FIRST_SECTION)
  38. #define EXPORT_PER_CPU_SYMBOL(var) EXPORT_SYMBOL(per_cpu__##var)
  39. #define EXPORT_PER_CPU_SYMBOL_GPL(var) EXPORT_SYMBOL_GPL(per_cpu__##var)
  40. /* Enough to cover all DEFINE_PER_CPUs in kernel, including modules. */
  41. #ifndef PERCPU_ENOUGH_ROOM
  42. #ifdef CONFIG_MODULES
  43. #define PERCPU_MODULE_RESERVE 8192
  44. #else
  45. #define PERCPU_MODULE_RESERVE 0
  46. #endif
  47. #define PERCPU_ENOUGH_ROOM \
  48. (__per_cpu_end - __per_cpu_start + PERCPU_MODULE_RESERVE)
  49. #endif /* PERCPU_ENOUGH_ROOM */
  50. /*
  51. * Must be an lvalue. Since @var must be a simple identifier,
  52. * we force a syntax error here if it isn't.
  53. */
  54. #define get_cpu_var(var) (*({ \
  55. extern int simple_identifier_##var(void); \
  56. preempt_disable(); \
  57. &__get_cpu_var(var); }))
  58. #define put_cpu_var(var) preempt_enable()
  59. #ifdef CONFIG_SMP
  60. #ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
  61. /* minimum unit size, also is the maximum supported allocation size */
  62. #define PCPU_MIN_UNIT_SIZE (16UL << PAGE_SHIFT)
  63. /*
  64. * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
  65. * back on the first chunk if arch is manually allocating and mapping
  66. * it for faster access (as a part of large page mapping for example).
  67. * Note that dynamic percpu allocator covers both static and dynamic
  68. * areas, so these values are bigger than PERCPU_MODULE_RESERVE.
  69. *
  70. * On typical configuration with modules, the following values leave
  71. * about 8k of free space on the first chunk after boot on both x86_32
  72. * and 64 when module support is enabled. When module support is
  73. * disabled, it's much tighter.
  74. */
  75. #ifndef PERCPU_DYNAMIC_RESERVE
  76. # if BITS_PER_LONG > 32
  77. # ifdef CONFIG_MODULES
  78. # define PERCPU_DYNAMIC_RESERVE (6 << PAGE_SHIFT)
  79. # else
  80. # define PERCPU_DYNAMIC_RESERVE (4 << PAGE_SHIFT)
  81. # endif
  82. # else
  83. # ifdef CONFIG_MODULES
  84. # define PERCPU_DYNAMIC_RESERVE (4 << PAGE_SHIFT)
  85. # else
  86. # define PERCPU_DYNAMIC_RESERVE (2 << PAGE_SHIFT)
  87. # endif
  88. # endif
  89. #endif /* PERCPU_DYNAMIC_RESERVE */
  90. extern void *pcpu_base_addr;
  91. typedef struct page * (*pcpu_get_page_fn_t)(unsigned int cpu, int pageno);
  92. typedef void (*pcpu_populate_pte_fn_t)(unsigned long addr);
  93. extern size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
  94. size_t static_size, size_t unit_size,
  95. size_t free_size, void *base_addr,
  96. pcpu_populate_pte_fn_t populate_pte_fn);
  97. /*
  98. * Use this to get to a cpu's version of the per-cpu object
  99. * dynamically allocated. Non-atomic access to the current CPU's
  100. * version should probably be combined with get_cpu()/put_cpu().
  101. */
  102. #define per_cpu_ptr(ptr, cpu) SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)))
  103. #else /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
  104. struct percpu_data {
  105. void *ptrs[1];
  106. };
  107. #define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
  108. #define per_cpu_ptr(ptr, cpu) \
  109. ({ \
  110. struct percpu_data *__p = __percpu_disguise(ptr); \
  111. (__typeof__(ptr))__p->ptrs[(cpu)]; \
  112. })
  113. #endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
  114. extern void *__alloc_percpu(size_t size, size_t align);
  115. extern void free_percpu(void *__pdata);
  116. #else /* CONFIG_SMP */
  117. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
  118. static inline void *__alloc_percpu(size_t size, size_t align)
  119. {
  120. /*
  121. * Can't easily make larger alignment work with kmalloc. WARN
  122. * on it. Larger alignment should only be used for module
  123. * percpu sections on SMP for which this path isn't used.
  124. */
  125. WARN_ON_ONCE(align > SMP_CACHE_BYTES);
  126. return kzalloc(size, GFP_KERNEL);
  127. }
  128. static inline void free_percpu(void *p)
  129. {
  130. kfree(p);
  131. }
  132. #endif /* CONFIG_SMP */
  133. #define alloc_percpu(type) (type *)__alloc_percpu(sizeof(type), \
  134. __alignof__(type))
  135. #endif /* __LINUX_PERCPU_H */