percpu.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. typedef struct page * (*pcpu_get_page_fn_t)(unsigned int cpu, int pageno);
  51. typedef void (*pcpu_populate_pte_fn_t)(unsigned long addr);
  52. extern size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
  53. size_t static_size, size_t reserved_size,
  54. ssize_t dyn_size, ssize_t unit_size,
  55. void *base_addr,
  56. pcpu_populate_pte_fn_t populate_pte_fn);
  57. extern ssize_t __init pcpu_embed_first_chunk(
  58. size_t static_size, size_t reserved_size,
  59. ssize_t dyn_size, ssize_t unit_size);
  60. /*
  61. * Use this to get to a cpu's version of the per-cpu object
  62. * dynamically allocated. Non-atomic access to the current CPU's
  63. * version should probably be combined with get_cpu()/put_cpu().
  64. */
  65. #define per_cpu_ptr(ptr, cpu) SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)))
  66. extern void *__alloc_reserved_percpu(size_t size, size_t align);
  67. #else /* CONFIG_HAVE_LEGACY_PER_CPU_AREA */
  68. struct percpu_data {
  69. void *ptrs[1];
  70. };
  71. /* pointer disguising messes up the kmemleak objects tracking */
  72. #ifndef CONFIG_DEBUG_KMEMLEAK
  73. #define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
  74. #else
  75. #define __percpu_disguise(pdata) (struct percpu_data *)(pdata)
  76. #endif
  77. #define per_cpu_ptr(ptr, cpu) \
  78. ({ \
  79. struct percpu_data *__p = __percpu_disguise(ptr); \
  80. (__typeof__(ptr))__p->ptrs[(cpu)]; \
  81. })
  82. #endif /* CONFIG_HAVE_LEGACY_PER_CPU_AREA */
  83. extern void *__alloc_percpu(size_t size, size_t align);
  84. extern void free_percpu(void *__pdata);
  85. #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
  86. extern void __init setup_per_cpu_areas(void);
  87. #endif
  88. #else /* CONFIG_SMP */
  89. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
  90. static inline void *__alloc_percpu(size_t size, size_t align)
  91. {
  92. /*
  93. * Can't easily make larger alignment work with kmalloc. WARN
  94. * on it. Larger alignment should only be used for module
  95. * percpu sections on SMP for which this path isn't used.
  96. */
  97. WARN_ON_ONCE(align > SMP_CACHE_BYTES);
  98. return kzalloc(size, GFP_KERNEL);
  99. }
  100. static inline void free_percpu(void *p)
  101. {
  102. kfree(p);
  103. }
  104. static inline void __init setup_per_cpu_areas(void) { }
  105. #endif /* CONFIG_SMP */
  106. #define alloc_percpu(type) (type *)__alloc_percpu(sizeof(type), \
  107. __alignof__(type))
  108. /*
  109. * Optional methods for optimized non-lvalue per-cpu variable access.
  110. *
  111. * @var can be a percpu variable or a field of it and its size should
  112. * equal char, int or long. percpu_read() evaluates to a lvalue and
  113. * all others to void.
  114. *
  115. * These operations are guaranteed to be atomic w.r.t. preemption.
  116. * The generic versions use plain get/put_cpu_var(). Archs are
  117. * encouraged to implement single-instruction alternatives which don't
  118. * require preemption protection.
  119. */
  120. #ifndef percpu_read
  121. # define percpu_read(var) \
  122. ({ \
  123. typeof(per_cpu_var(var)) __tmp_var__; \
  124. __tmp_var__ = get_cpu_var(var); \
  125. put_cpu_var(var); \
  126. __tmp_var__; \
  127. })
  128. #endif
  129. #define __percpu_generic_to_op(var, val, op) \
  130. do { \
  131. get_cpu_var(var) op val; \
  132. put_cpu_var(var); \
  133. } while (0)
  134. #ifndef percpu_write
  135. # define percpu_write(var, val) __percpu_generic_to_op(var, (val), =)
  136. #endif
  137. #ifndef percpu_add
  138. # define percpu_add(var, val) __percpu_generic_to_op(var, (val), +=)
  139. #endif
  140. #ifndef percpu_sub
  141. # define percpu_sub(var, val) __percpu_generic_to_op(var, (val), -=)
  142. #endif
  143. #ifndef percpu_and
  144. # define percpu_and(var, val) __percpu_generic_to_op(var, (val), &=)
  145. #endif
  146. #ifndef percpu_or
  147. # define percpu_or(var, val) __percpu_generic_to_op(var, (val), |=)
  148. #endif
  149. #ifndef percpu_xor
  150. # define percpu_xor(var, val) __percpu_generic_to_op(var, (val), ^=)
  151. #endif
  152. #endif /* __LINUX_PERCPU_H */