blob: 00412bb494c40b100bc1e996983b72ba877ccf69 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __LINUX_PERCPU_H
2#define __LINUX_PERCPU_H
Martin Peschke7ff6f082006-09-25 23:31:21 -07003
Robert P. J. Day0a3021f2007-07-15 23:39:57 -07004#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/slab.h> /* For kmalloc() */
6#include <linux/smp.h>
7#include <linux/string.h> /* For memset() */
Martin Peschke7ff6f082006-09-25 23:31:21 -07008#include <linux/cpumask.h>
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <asm/percpu.h>
11
travis@sgi.com5280e002008-01-30 13:32:52 +010012#ifndef PER_CPU_ATTRIBUTES
13#define PER_CPU_ATTRIBUTES
14#endif
15
16#ifdef CONFIG_SMP
17#define DEFINE_PER_CPU(type, name) \
18 __attribute__((__section__(".data.percpu"))) \
19 PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name
20
21#define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
22 __attribute__((__section__(".data.percpu.shared_aligned"))) \
23 PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name \
24 ____cacheline_aligned_in_smp
25#else
26#define DEFINE_PER_CPU(type, name) \
27 PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name
28
29#define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
30 DEFINE_PER_CPU(type, name)
31#endif
32
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* Enough to cover all DEFINE_PER_CPUs in kernel, including modules. */
37#ifndef PERCPU_ENOUGH_ROOM
Jeremy Fitzhardingeb00742d2007-05-02 19:27:11 +020038#ifdef CONFIG_MODULES
39#define PERCPU_MODULE_RESERVE 8192
40#else
41#define PERCPU_MODULE_RESERVE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#endif
43
Jeremy Fitzhardingeb00742d2007-05-02 19:27:11 +020044#define PERCPU_ENOUGH_ROOM \
45 (__per_cpu_end - __per_cpu_start + PERCPU_MODULE_RESERVE)
46#endif /* PERCPU_ENOUGH_ROOM */
47
Jan Blunck632bbfe2006-09-25 23:30:53 -070048/*
49 * Must be an lvalue. Since @var must be a simple identifier,
50 * we force a syntax error here if it isn't.
51 */
52#define get_cpu_var(var) (*({ \
Jan Bluncka666ecf2006-10-06 00:43:58 -070053 extern int simple_identifier_##var(void); \
Jan Blunck632bbfe2006-09-25 23:30:53 -070054 preempt_disable(); \
55 &__get_cpu_var(var); }))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define put_cpu_var(var) preempt_enable()
57
58#ifdef CONFIG_SMP
59
60struct percpu_data {
61 void *ptrs[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Martin Peschke7ff6f082006-09-25 23:31:21 -070064#define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
Martin Peschke7ff6f082006-09-25 23:31:21 -070066 * Use this to get to a cpu's version of the per-cpu object dynamically
67 * allocated. Non-atomic access to the current CPU's version should
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * probably be combined with get_cpu()/put_cpu().
69 */
Martin Peschke7ff6f082006-09-25 23:31:21 -070070#define percpu_ptr(ptr, cpu) \
71({ \
72 struct percpu_data *__p = __percpu_disguise(ptr); \
73 (__typeof__(ptr))__p->ptrs[(cpu)]; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070074})
75
Martin Peschke7ff6f082006-09-25 23:31:21 -070076extern void *percpu_populate(void *__pdata, size_t size, gfp_t gfp, int cpu);
77extern void percpu_depopulate(void *__pdata, int cpu);
78extern int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
79 cpumask_t *mask);
80extern void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask);
81extern void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask);
82extern void percpu_free(void *__pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84#else /* CONFIG_SMP */
85
Martin Peschke7ff6f082006-09-25 23:31:21 -070086#define percpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Martin Peschke7ff6f082006-09-25 23:31:21 -070088static inline void percpu_depopulate(void *__pdata, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
Martin Peschke7ff6f082006-09-25 23:31:21 -070091
92static inline void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask)
93{
94}
95
96static inline void *percpu_populate(void *__pdata, size_t size, gfp_t gfp,
97 int cpu)
98{
99 return percpu_ptr(__pdata, cpu);
100}
101
102static inline int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
103 cpumask_t *mask)
104{
105 return 0;
106}
107
Al Viro0891a8d2006-09-29 01:58:34 -0700108static __always_inline void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask)
Martin Peschke7ff6f082006-09-25 23:31:21 -0700109{
110 return kzalloc(size, gfp);
111}
112
113static inline void percpu_free(void *__pdata)
114{
115 kfree(__pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118#endif /* CONFIG_SMP */
119
Martin Peschke7ff6f082006-09-25 23:31:21 -0700120#define percpu_populate_mask(__pdata, size, gfp, mask) \
121 __percpu_populate_mask((__pdata), (size), (gfp), &(mask))
122#define percpu_depopulate_mask(__pdata, mask) \
123 __percpu_depopulate_mask((__pdata), &(mask))
124#define percpu_alloc_mask(size, gfp, mask) \
125 __percpu_alloc_mask((size), (gfp), &(mask))
126
127#define percpu_alloc(size, gfp) percpu_alloc_mask((size), (gfp), cpu_online_map)
128
129/* (legacy) interface for use without CPU hotplug handling */
130
131#define __alloc_percpu(size) percpu_alloc_mask((size), GFP_KERNEL, \
132 cpu_possible_map)
133#define alloc_percpu(type) (type *)__alloc_percpu(sizeof(type))
134#define free_percpu(ptr) percpu_free((ptr))
135#define per_cpu_ptr(ptr, cpu) percpu_ptr((ptr), (cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137#endif /* __LINUX_PERCPU_H */