1 #ifndef __LINUX_PERCPU_H
2 #define __LINUX_PERCPU_H
4 #include <linux/preempt.h>
5 #include <linux/slab.h> /* For kmalloc() */
7 #include <linux/cpumask.h>
9 #include <asm/percpu.h>
12 #define DEFINE_PER_CPU(type, name) \
13 __attribute__((__section__(".data.percpu"))) \
14 PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name
17 #define SHARED_ALIGNED_SECTION ".data.percpu"
19 #define SHARED_ALIGNED_SECTION ".data.percpu.shared_aligned"
22 #define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
23 __attribute__((__section__(SHARED_ALIGNED_SECTION))) \
24 PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name \
25 ____cacheline_aligned_in_smp
27 #define DEFINE_PER_CPU(type, name) \
28 PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name
30 #define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
31 DEFINE_PER_CPU(type, name)
34 #define EXPORT_PER_CPU_SYMBOL(var) EXPORT_SYMBOL(per_cpu__##var)
35 #define EXPORT_PER_CPU_SYMBOL_GPL(var) EXPORT_SYMBOL_GPL(per_cpu__##var)
37 /* Enough to cover all DEFINE_PER_CPUs in kernel, including modules. */
38 #ifndef PERCPU_ENOUGH_ROOM
40 #define PERCPU_MODULE_RESERVE 8192
42 #define PERCPU_MODULE_RESERVE 0
45 #define PERCPU_ENOUGH_ROOM \
46 (__per_cpu_end - __per_cpu_start + PERCPU_MODULE_RESERVE)
47 #endif /* PERCPU_ENOUGH_ROOM */
50 * Must be an lvalue. Since @var must be a simple identifier,
51 * we force a syntax error here if it isn't.
53 #define get_cpu_var(var) (*({ \
54 extern int simple_identifier_##var(void); \
56 &__get_cpu_var(var); }))
57 #define put_cpu_var(var) preempt_enable()
65 #define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
67 * Use this to get to a cpu's version of the per-cpu object dynamically
68 * allocated. Non-atomic access to the current CPU's version should
69 * probably be combined with get_cpu()/put_cpu().
71 #define percpu_ptr(ptr, cpu) \
73 struct percpu_data *__p = __percpu_disguise(ptr); \
74 (__typeof__(ptr))__p->ptrs[(cpu)]; \
77 extern void *percpu_populate(void *__pdata
, size_t size
, gfp_t gfp
, int cpu
);
78 extern void percpu_depopulate(void *__pdata
, int cpu
);
79 extern int __percpu_populate_mask(void *__pdata
, size_t size
, gfp_t gfp
,
81 extern void __percpu_depopulate_mask(void *__pdata
, cpumask_t
*mask
);
82 extern void *__percpu_alloc_mask(size_t size
, gfp_t gfp
, cpumask_t
*mask
);
83 extern void percpu_free(void *__pdata
);
85 #else /* CONFIG_SMP */
87 #define percpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
89 static inline void percpu_depopulate(void *__pdata
, int cpu
)
93 static inline void __percpu_depopulate_mask(void *__pdata
, cpumask_t
*mask
)
97 static inline void *percpu_populate(void *__pdata
, size_t size
, gfp_t gfp
,
100 return percpu_ptr(__pdata
, cpu
);
103 static inline int __percpu_populate_mask(void *__pdata
, size_t size
, gfp_t gfp
,
109 static __always_inline
void *__percpu_alloc_mask(size_t size
, gfp_t gfp
, cpumask_t
*mask
)
111 return kzalloc(size
, gfp
);
114 static inline void percpu_free(void *__pdata
)
119 #endif /* CONFIG_SMP */
121 #define percpu_populate_mask(__pdata, size, gfp, mask) \
122 __percpu_populate_mask((__pdata), (size), (gfp), &(mask))
123 #define percpu_depopulate_mask(__pdata, mask) \
124 __percpu_depopulate_mask((__pdata), &(mask))
125 #define percpu_alloc_mask(size, gfp, mask) \
126 __percpu_alloc_mask((size), (gfp), &(mask))
128 #define percpu_alloc(size, gfp) percpu_alloc_mask((size), (gfp), cpu_online_map)
130 /* (legacy) interface for use without CPU hotplug handling */
132 #define __alloc_percpu(size) percpu_alloc_mask((size), GFP_KERNEL, \
134 #define alloc_percpu(type) (type *)__alloc_percpu(sizeof(type))
135 #define free_percpu(ptr) percpu_free((ptr))
136 #define per_cpu_ptr(ptr, cpu) percpu_ptr((ptr), (cpu))
138 #endif /* __LINUX_PERCPU_H */