percpu.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask);
  64. extern void percpu_free(void *__pdata);
  65. #else /* CONFIG_SMP */
  66. #define percpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
  67. static __always_inline void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask)
  68. {
  69. return kzalloc(size, gfp);
  70. }
  71. static inline void percpu_free(void *__pdata)
  72. {
  73. kfree(__pdata);
  74. }
  75. #endif /* CONFIG_SMP */
  76. #define percpu_alloc_mask(size, gfp, mask) \
  77. __percpu_alloc_mask((size), (gfp), &(mask))
  78. #define percpu_alloc(size, gfp) percpu_alloc_mask((size), (gfp), cpu_online_map)
  79. /* (legacy) interface for use without CPU hotplug handling */
  80. #define __alloc_percpu(size) percpu_alloc_mask((size), GFP_KERNEL, \
  81. cpu_possible_map)
  82. #define alloc_percpu(type) (type *)__alloc_percpu(sizeof(type))
  83. #define free_percpu(ptr) percpu_free((ptr))
  84. #define per_cpu_ptr(ptr, cpu) percpu_ptr((ptr), (cpu))
  85. #endif /* __LINUX_PERCPU_H */