percpu.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. /* enough to cover all DEFINE_PER_CPUs in modules */
  10. #ifdef CONFIG_MODULES
  11. #define PERCPU_MODULE_RESERVE (8 << 10)
  12. #else
  13. #define PERCPU_MODULE_RESERVE 0
  14. #endif
  15. #ifndef PERCPU_ENOUGH_ROOM
  16. #define PERCPU_ENOUGH_ROOM \
  17. (ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES) + \
  18. PERCPU_MODULE_RESERVE)
  19. #endif
  20. /*
  21. * Must be an lvalue. Since @var must be a simple identifier,
  22. * we force a syntax error here if it isn't.
  23. */
  24. #define get_cpu_var(var) (*({ \
  25. extern int simple_identifier_##var(void); \
  26. preempt_disable(); \
  27. &__get_cpu_var(var); }))
  28. #define put_cpu_var(var) preempt_enable()
  29. #ifdef CONFIG_SMP
  30. #ifndef CONFIG_HAVE_LEGACY_PER_CPU_AREA
  31. /* minimum unit size, also is the maximum supported allocation size */
  32. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(64 << 10)
  33. /*
  34. * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
  35. * back on the first chunk for dynamic percpu allocation if arch is
  36. * manually allocating and mapping it for faster access (as a part of
  37. * large page mapping for example).
  38. *
  39. * The following values give between one and two pages of free space
  40. * after typical minimal boot (2-way SMP, single disk and NIC) with
  41. * both defconfig and a distro config on x86_64 and 32. More
  42. * intelligent way to determine this would be nice.
  43. */
  44. #if BITS_PER_LONG > 32
  45. #define PERCPU_DYNAMIC_RESERVE (20 << 10)
  46. #else
  47. #define PERCPU_DYNAMIC_RESERVE (12 << 10)
  48. #endif
  49. extern void *pcpu_base_addr;
  50. extern const int *pcpu_unit_map;
  51. typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size);
  52. typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
  53. typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
  54. typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
  55. typedef void (*pcpu_fc_map_fn_t)(void *ptr, size_t size, void *addr);
  56. extern size_t __init pcpu_setup_first_chunk(
  57. size_t static_size, size_t reserved_size,
  58. ssize_t dyn_size, size_t unit_size,
  59. void *base_addr, const int *unit_map);
  60. extern ssize_t __init pcpu_embed_first_chunk(
  61. size_t static_size, size_t reserved_size,
  62. ssize_t dyn_size);
  63. extern ssize_t __init pcpu_4k_first_chunk(
  64. size_t static_size, size_t reserved_size,
  65. pcpu_fc_alloc_fn_t alloc_fn,
  66. pcpu_fc_free_fn_t free_fn,
  67. pcpu_fc_populate_pte_fn_t populate_pte_fn);
  68. #ifdef CONFIG_NEED_MULTIPLE_NODES
  69. extern int __init pcpu_lpage_build_unit_map(
  70. size_t static_size, size_t reserved_size,
  71. ssize_t *dyn_sizep, size_t *unit_sizep,
  72. size_t lpage_size, int *unit_map,
  73. pcpu_fc_cpu_distance_fn_t cpu_distance_fn);
  74. extern ssize_t __init pcpu_lpage_first_chunk(
  75. size_t static_size, size_t reserved_size,
  76. size_t dyn_size, size_t unit_size,
  77. size_t lpage_size, const int *unit_map,
  78. int nr_units,
  79. pcpu_fc_alloc_fn_t alloc_fn,
  80. pcpu_fc_free_fn_t free_fn,
  81. pcpu_fc_map_fn_t map_fn);
  82. extern void *pcpu_lpage_remapped(void *kaddr);
  83. #else
  84. static inline int pcpu_lpage_build_unit_map(
  85. size_t static_size, size_t reserved_size,
  86. ssize_t *dyn_sizep, size_t *unit_sizep,
  87. size_t lpage_size, int *unit_map,
  88. pcpu_fc_cpu_distance_fn_t cpu_distance_fn)
  89. {
  90. return -EINVAL;
  91. }
  92. static inline ssize_t __init pcpu_lpage_first_chunk(
  93. size_t static_size, size_t reserved_size,
  94. size_t dyn_size, size_t unit_size,
  95. size_t lpage_size, const int *unit_map,
  96. int nr_units,
  97. pcpu_fc_alloc_fn_t alloc_fn,
  98. pcpu_fc_free_fn_t free_fn,
  99. pcpu_fc_map_fn_t map_fn)
  100. {
  101. return -EINVAL;
  102. }
  103. static inline void *pcpu_lpage_remapped(void *kaddr)
  104. {
  105. return NULL;
  106. }
  107. #endif
  108. /*
  109. * Use this to get to a cpu's version of the per-cpu object
  110. * dynamically allocated. Non-atomic access to the current CPU's
  111. * version should probably be combined with get_cpu()/put_cpu().
  112. */
  113. #define per_cpu_ptr(ptr, cpu) SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)))
  114. extern void *__alloc_reserved_percpu(size_t size, size_t align);
  115. #else /* CONFIG_HAVE_LEGACY_PER_CPU_AREA */
  116. struct percpu_data {
  117. void *ptrs[1];
  118. };
  119. /* pointer disguising messes up the kmemleak objects tracking */
  120. #ifndef CONFIG_DEBUG_KMEMLEAK
  121. #define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
  122. #else
  123. #define __percpu_disguise(pdata) (struct percpu_data *)(pdata)
  124. #endif
  125. #define per_cpu_ptr(ptr, cpu) \
  126. ({ \
  127. struct percpu_data *__p = __percpu_disguise(ptr); \
  128. (__typeof__(ptr))__p->ptrs[(cpu)]; \
  129. })
  130. #endif /* CONFIG_HAVE_LEGACY_PER_CPU_AREA */
  131. extern void *__alloc_percpu(size_t size, size_t align);
  132. extern void free_percpu(void *__pdata);
  133. #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
  134. extern void __init setup_per_cpu_areas(void);
  135. #endif
  136. #else /* CONFIG_SMP */
  137. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
  138. static inline void *__alloc_percpu(size_t size, size_t align)
  139. {
  140. /*
  141. * Can't easily make larger alignment work with kmalloc. WARN
  142. * on it. Larger alignment should only be used for module
  143. * percpu sections on SMP for which this path isn't used.
  144. */
  145. WARN_ON_ONCE(align > SMP_CACHE_BYTES);
  146. return kzalloc(size, GFP_KERNEL);
  147. }
  148. static inline void free_percpu(void *p)
  149. {
  150. kfree(p);
  151. }
  152. static inline void __init setup_per_cpu_areas(void) { }
  153. static inline void *pcpu_lpage_remapped(void *kaddr)
  154. {
  155. return NULL;
  156. }
  157. #endif /* CONFIG_SMP */
  158. #define alloc_percpu(type) (type *)__alloc_percpu(sizeof(type), \
  159. __alignof__(type))
  160. /*
  161. * Optional methods for optimized non-lvalue per-cpu variable access.
  162. *
  163. * @var can be a percpu variable or a field of it and its size should
  164. * equal char, int or long. percpu_read() evaluates to a lvalue and
  165. * all others to void.
  166. *
  167. * These operations are guaranteed to be atomic w.r.t. preemption.
  168. * The generic versions use plain get/put_cpu_var(). Archs are
  169. * encouraged to implement single-instruction alternatives which don't
  170. * require preemption protection.
  171. */
  172. #ifndef percpu_read
  173. # define percpu_read(var) \
  174. ({ \
  175. typeof(per_cpu_var(var)) __tmp_var__; \
  176. __tmp_var__ = get_cpu_var(var); \
  177. put_cpu_var(var); \
  178. __tmp_var__; \
  179. })
  180. #endif
  181. #define __percpu_generic_to_op(var, val, op) \
  182. do { \
  183. get_cpu_var(var) op val; \
  184. put_cpu_var(var); \
  185. } while (0)
  186. #ifndef percpu_write
  187. # define percpu_write(var, val) __percpu_generic_to_op(var, (val), =)
  188. #endif
  189. #ifndef percpu_add
  190. # define percpu_add(var, val) __percpu_generic_to_op(var, (val), +=)
  191. #endif
  192. #ifndef percpu_sub
  193. # define percpu_sub(var, val) __percpu_generic_to_op(var, (val), -=)
  194. #endif
  195. #ifndef percpu_and
  196. # define percpu_and(var, val) __percpu_generic_to_op(var, (val), &=)
  197. #endif
  198. #ifndef percpu_or
  199. # define percpu_or(var, val) __percpu_generic_to_op(var, (val), |=)
  200. #endif
  201. #ifndef percpu_xor
  202. # define percpu_xor(var, val) __percpu_generic_to_op(var, (val), ^=)
  203. #endif
  204. #endif /* __LINUX_PERCPU_H */