blob: 2edacc8e6b8b887e6a2eed2ab0a1aeb9644ec62d [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>
Martin Peschke7ff6f082006-09-25 23:31:21 -07007#include <linux/cpumask.h>
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/percpu.h>
10
travis@sgi.com5280e002008-01-30 13:32:52 +010011#ifdef CONFIG_SMP
12#define DEFINE_PER_CPU(type, name) \
13 __attribute__((__section__(".data.percpu"))) \
14 PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name
15
Eric Dumazet44c81432008-05-14 16:05:51 -070016#ifdef MODULE
17#define SHARED_ALIGNED_SECTION ".data.percpu"
18#else
19#define SHARED_ALIGNED_SECTION ".data.percpu.shared_aligned"
20#endif
21
travis@sgi.com5280e002008-01-30 13:32:52 +010022#define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
Eric Dumazet44c81432008-05-14 16:05:51 -070023 __attribute__((__section__(SHARED_ALIGNED_SECTION))) \
travis@sgi.com5280e002008-01-30 13:32:52 +010024 PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name \
25 ____cacheline_aligned_in_smp
Eric Dumazet63cc8c72008-05-12 15:44:40 +020026
27#define DEFINE_PER_CPU_PAGE_ALIGNED(type, name) \
28 __attribute__((__section__(".data.percpu.page_aligned"))) \
29 PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name
travis@sgi.com5280e002008-01-30 13:32:52 +010030#else
31#define DEFINE_PER_CPU(type, name) \
32 PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name
33
34#define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
35 DEFINE_PER_CPU(type, name)
Eric Dumazet63cc8c72008-05-12 15:44:40 +020036
37#define DEFINE_PER_CPU_PAGE_ALIGNED(type, name) \
38 DEFINE_PER_CPU(type, name)
travis@sgi.com5280e002008-01-30 13:32:52 +010039#endif
40
41#define EXPORT_PER_CPU_SYMBOL(var) EXPORT_SYMBOL(per_cpu__##var)
42#define EXPORT_PER_CPU_SYMBOL_GPL(var) EXPORT_SYMBOL_GPL(per_cpu__##var)
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* Enough to cover all DEFINE_PER_CPUs in kernel, including modules. */
45#ifndef PERCPU_ENOUGH_ROOM
Jeremy Fitzhardingeb00742d2007-05-02 19:27:11 +020046#ifdef CONFIG_MODULES
47#define PERCPU_MODULE_RESERVE 8192
48#else
49#define PERCPU_MODULE_RESERVE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#endif
51
Jeremy Fitzhardingeb00742d2007-05-02 19:27:11 +020052#define PERCPU_ENOUGH_ROOM \
53 (__per_cpu_end - __per_cpu_start + PERCPU_MODULE_RESERVE)
54#endif /* PERCPU_ENOUGH_ROOM */
55
Jan Blunck632bbfe2006-09-25 23:30:53 -070056/*
57 * Must be an lvalue. Since @var must be a simple identifier,
58 * we force a syntax error here if it isn't.
59 */
60#define get_cpu_var(var) (*({ \
Jan Bluncka666ecf2006-10-06 00:43:58 -070061 extern int simple_identifier_##var(void); \
Jan Blunck632bbfe2006-09-25 23:30:53 -070062 preempt_disable(); \
63 &__get_cpu_var(var); }))
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define put_cpu_var(var) preempt_enable()
65
66#ifdef CONFIG_SMP
67
68struct percpu_data {
Eric Dumazetb3242152008-02-06 01:37:01 -080069 void *ptrs[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Martin Peschke7ff6f082006-09-25 23:31:21 -070072#define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
Martin Peschke7ff6f082006-09-25 23:31:21 -070074 * Use this to get to a cpu's version of the per-cpu object dynamically
75 * allocated. Non-atomic access to the current CPU's version should
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * probably be combined with get_cpu()/put_cpu().
77 */
Martin Peschke7ff6f082006-09-25 23:31:21 -070078#define percpu_ptr(ptr, cpu) \
79({ \
80 struct percpu_data *__p = __percpu_disguise(ptr); \
81 (__typeof__(ptr))__p->ptrs[(cpu)]; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070082})
83
Martin Peschke7ff6f082006-09-25 23:31:21 -070084extern void *percpu_populate(void *__pdata, size_t size, gfp_t gfp, int cpu);
85extern void percpu_depopulate(void *__pdata, int cpu);
86extern int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
87 cpumask_t *mask);
88extern void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask);
89extern void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask);
90extern void percpu_free(void *__pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#else /* CONFIG_SMP */
93
Martin Peschke7ff6f082006-09-25 23:31:21 -070094#define percpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Martin Peschke7ff6f082006-09-25 23:31:21 -070096static inline void percpu_depopulate(void *__pdata, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
Martin Peschke7ff6f082006-09-25 23:31:21 -070099
100static inline void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask)
101{
102}
103
104static inline void *percpu_populate(void *__pdata, size_t size, gfp_t gfp,
105 int cpu)
106{
107 return percpu_ptr(__pdata, cpu);
108}
109
110static inline int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
111 cpumask_t *mask)
112{
113 return 0;
114}
115
Al Viro0891a8d2006-09-29 01:58:34 -0700116static __always_inline void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask)
Martin Peschke7ff6f082006-09-25 23:31:21 -0700117{
118 return kzalloc(size, gfp);
119}
120
121static inline void percpu_free(void *__pdata)
122{
123 kfree(__pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126#endif /* CONFIG_SMP */
127
Martin Peschke7ff6f082006-09-25 23:31:21 -0700128#define percpu_populate_mask(__pdata, size, gfp, mask) \
129 __percpu_populate_mask((__pdata), (size), (gfp), &(mask))
130#define percpu_depopulate_mask(__pdata, mask) \
131 __percpu_depopulate_mask((__pdata), &(mask))
132#define percpu_alloc_mask(size, gfp, mask) \
133 __percpu_alloc_mask((size), (gfp), &(mask))
134
135#define percpu_alloc(size, gfp) percpu_alloc_mask((size), (gfp), cpu_online_map)
136
137/* (legacy) interface for use without CPU hotplug handling */
138
139#define __alloc_percpu(size) percpu_alloc_mask((size), GFP_KERNEL, \
140 cpu_possible_map)
141#define alloc_percpu(type) (type *)__alloc_percpu(sizeof(type))
142#define free_percpu(ptr) percpu_free((ptr))
143#define per_cpu_ptr(ptr, cpu) percpu_ptr((ptr), (cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#endif /* __LINUX_PERCPU_H */