percpu.h 4.8 KB

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