percpu.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 for dynamic percpu allocation if arch is
  68. * manually allocating and mapping it for faster access (as a part of
  69. * large page mapping for example).
  70. *
  71. * The following values give between one and two pages of free space
  72. * after typical minimal boot (2-way SMP, single disk and NIC) with
  73. * both defconfig and a distro config on x86_64 and 32. More
  74. * intelligent way to determine this would be nice.
  75. */
  76. #if BITS_PER_LONG > 32
  77. #define PERCPU_DYNAMIC_RESERVE (20 << 10)
  78. #else
  79. #define PERCPU_DYNAMIC_RESERVE (12 << 10)
  80. #endif
  81. extern void *pcpu_base_addr;
  82. typedef struct page * (*pcpu_get_page_fn_t)(unsigned int cpu, int pageno);
  83. typedef void (*pcpu_populate_pte_fn_t)(unsigned long addr);
  84. extern size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
  85. size_t static_size, size_t reserved_size,
  86. ssize_t dyn_size, ssize_t unit_size,
  87. void *base_addr,
  88. pcpu_populate_pte_fn_t populate_pte_fn);
  89. extern ssize_t __init pcpu_embed_first_chunk(
  90. size_t static_size, size_t reserved_size,
  91. ssize_t dyn_size, ssize_t unit_size);
  92. /*
  93. * Use this to get to a cpu's version of the per-cpu object
  94. * dynamically allocated. Non-atomic access to the current CPU's
  95. * version should probably be combined with get_cpu()/put_cpu().
  96. */
  97. #define per_cpu_ptr(ptr, cpu) SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)))
  98. extern void *__alloc_reserved_percpu(size_t size, size_t align);
  99. #else /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
  100. struct percpu_data {
  101. void *ptrs[1];
  102. };
  103. #define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
  104. #define per_cpu_ptr(ptr, cpu) \
  105. ({ \
  106. struct percpu_data *__p = __percpu_disguise(ptr); \
  107. (__typeof__(ptr))__p->ptrs[(cpu)]; \
  108. })
  109. #endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
  110. extern void *__alloc_percpu(size_t size, size_t align);
  111. extern void free_percpu(void *__pdata);
  112. #else /* CONFIG_SMP */
  113. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
  114. static inline void *__alloc_percpu(size_t size, size_t align)
  115. {
  116. /*
  117. * Can't easily make larger alignment work with kmalloc. WARN
  118. * on it. Larger alignment should only be used for module
  119. * percpu sections on SMP for which this path isn't used.
  120. */
  121. WARN_ON_ONCE(align > SMP_CACHE_BYTES);
  122. return kzalloc(size, GFP_KERNEL);
  123. }
  124. static inline void free_percpu(void *p)
  125. {
  126. kfree(p);
  127. }
  128. #endif /* CONFIG_SMP */
  129. #define alloc_percpu(type) (type *)__alloc_percpu(sizeof(type), \
  130. __alignof__(type))
  131. #endif /* __LINUX_PERCPU_H */