percpu.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifdef CONFIG_SMP
  9. #define DEFINE_PER_CPU(type, name) \
  10. __attribute__((__section__(".data.percpu"))) \
  11. PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name
  12. #ifdef MODULE
  13. #define SHARED_ALIGNED_SECTION ".data.percpu"
  14. #else
  15. #define SHARED_ALIGNED_SECTION ".data.percpu.shared_aligned"
  16. #endif
  17. #define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
  18. __attribute__((__section__(SHARED_ALIGNED_SECTION))) \
  19. PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name \
  20. ____cacheline_aligned_in_smp
  21. #else
  22. #define DEFINE_PER_CPU(type, name) \
  23. PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name
  24. #define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
  25. DEFINE_PER_CPU(type, name)
  26. #endif
  27. #define EXPORT_PER_CPU_SYMBOL(var) EXPORT_SYMBOL(per_cpu__##var)
  28. #define EXPORT_PER_CPU_SYMBOL_GPL(var) EXPORT_SYMBOL_GPL(per_cpu__##var)
  29. /* Enough to cover all DEFINE_PER_CPUs in kernel, including modules. */
  30. #ifndef PERCPU_ENOUGH_ROOM
  31. #ifdef CONFIG_MODULES
  32. #define PERCPU_MODULE_RESERVE 8192
  33. #else
  34. #define PERCPU_MODULE_RESERVE 0
  35. #endif
  36. #define PERCPU_ENOUGH_ROOM \
  37. (__per_cpu_end - __per_cpu_start + PERCPU_MODULE_RESERVE)
  38. #endif /* PERCPU_ENOUGH_ROOM */
  39. /*
  40. * Must be an lvalue. Since @var must be a simple identifier,
  41. * we force a syntax error here if it isn't.
  42. */
  43. #define get_cpu_var(var) (*({ \
  44. extern int simple_identifier_##var(void); \
  45. preempt_disable(); \
  46. &__get_cpu_var(var); }))
  47. #define put_cpu_var(var) preempt_enable()
  48. #ifdef CONFIG_SMP
  49. struct percpu_data {
  50. void *ptrs[1];
  51. };
  52. #define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
  53. /*
  54. * Use this to get to a cpu's version of the per-cpu object dynamically
  55. * allocated. Non-atomic access to the current CPU's version should
  56. * probably be combined with get_cpu()/put_cpu().
  57. */
  58. #define percpu_ptr(ptr, cpu) \
  59. ({ \
  60. struct percpu_data *__p = __percpu_disguise(ptr); \
  61. (__typeof__(ptr))__p->ptrs[(cpu)]; \
  62. })
  63. extern void *percpu_populate(void *__pdata, size_t size, gfp_t gfp, int cpu);
  64. extern void percpu_depopulate(void *__pdata, int cpu);
  65. extern int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
  66. cpumask_t *mask);
  67. extern void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask);
  68. extern void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask);
  69. extern void percpu_free(void *__pdata);
  70. #else /* CONFIG_SMP */
  71. #define percpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
  72. static inline void percpu_depopulate(void *__pdata, int cpu)
  73. {
  74. }
  75. static inline void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask)
  76. {
  77. }
  78. static inline void *percpu_populate(void *__pdata, size_t size, gfp_t gfp,
  79. int cpu)
  80. {
  81. return percpu_ptr(__pdata, cpu);
  82. }
  83. static inline int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
  84. cpumask_t *mask)
  85. {
  86. return 0;
  87. }
  88. static __always_inline void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask)
  89. {
  90. return kzalloc(size, gfp);
  91. }
  92. static inline void percpu_free(void *__pdata)
  93. {
  94. kfree(__pdata);
  95. }
  96. #endif /* CONFIG_SMP */
  97. #define percpu_populate_mask(__pdata, size, gfp, mask) \
  98. __percpu_populate_mask((__pdata), (size), (gfp), &(mask))
  99. #define percpu_depopulate_mask(__pdata, mask) \
  100. __percpu_depopulate_mask((__pdata), &(mask))
  101. #define percpu_alloc_mask(size, gfp, mask) \
  102. __percpu_alloc_mask((size), (gfp), &(mask))
  103. #define percpu_alloc(size, gfp) percpu_alloc_mask((size), (gfp), cpu_online_map)
  104. /* (legacy) interface for use without CPU hotplug handling */
  105. #define __alloc_percpu(size) percpu_alloc_mask((size), GFP_KERNEL, \
  106. cpu_possible_map)
  107. #define alloc_percpu(type) (type *)__alloc_percpu(sizeof(type))
  108. #define free_percpu(ptr) percpu_free((ptr))
  109. #define per_cpu_ptr(ptr, cpu) percpu_ptr((ptr), (cpu))
  110. #endif /* __LINUX_PERCPU_H */