percpu.h 3.4 KB

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