percpu.h 3.3 KB

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