blob: 4cdd393e71e1bb70ee0286c16f2aeb08d1458d98 [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
26#else
27#define DEFINE_PER_CPU(type, name) \
28 PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name
29
30#define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
31 DEFINE_PER_CPU(type, name)
32#endif
33
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)
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* Enough to cover all DEFINE_PER_CPUs in kernel, including modules. */
38#ifndef PERCPU_ENOUGH_ROOM
Jeremy Fitzhardingeb00742d2007-05-02 19:27:11 +020039#ifdef CONFIG_MODULES
40#define PERCPU_MODULE_RESERVE 8192
41#else
42#define PERCPU_MODULE_RESERVE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#endif
44
Jeremy Fitzhardingeb00742d2007-05-02 19:27:11 +020045#define PERCPU_ENOUGH_ROOM \
46 (__per_cpu_end - __per_cpu_start + PERCPU_MODULE_RESERVE)
47#endif /* PERCPU_ENOUGH_ROOM */
48
Jan Blunck632bbfe2006-09-25 23:30:53 -070049/*
50 * Must be an lvalue. Since @var must be a simple identifier,
51 * we force a syntax error here if it isn't.
52 */
53#define get_cpu_var(var) (*({ \
Jan Bluncka666ecf2006-10-06 00:43:58 -070054 extern int simple_identifier_##var(void); \
Jan Blunck632bbfe2006-09-25 23:30:53 -070055 preempt_disable(); \
56 &__get_cpu_var(var); }))
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define put_cpu_var(var) preempt_enable()
58
59#ifdef CONFIG_SMP
60
61struct percpu_data {
Eric Dumazetb3242152008-02-06 01:37:01 -080062 void *ptrs[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
Martin Peschke7ff6f082006-09-25 23:31:21 -070065#define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
Martin Peschke7ff6f082006-09-25 23:31:21 -070067 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * probably be combined with get_cpu()/put_cpu().
70 */
Martin Peschke7ff6f082006-09-25 23:31:21 -070071#define percpu_ptr(ptr, cpu) \
72({ \
73 struct percpu_data *__p = __percpu_disguise(ptr); \
74 (__typeof__(ptr))__p->ptrs[(cpu)]; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070075})
76
Martin Peschke7ff6f082006-09-25 23:31:21 -070077extern void *percpu_populate(void *__pdata, size_t size, gfp_t gfp, int cpu);
78extern void percpu_depopulate(void *__pdata, int cpu);
79extern int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
80 cpumask_t *mask);
81extern void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask);
82extern void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask);
83extern void percpu_free(void *__pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#else /* CONFIG_SMP */
86
Martin Peschke7ff6f082006-09-25 23:31:21 -070087#define percpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Martin Peschke7ff6f082006-09-25 23:31:21 -070089static inline void percpu_depopulate(void *__pdata, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
Martin Peschke7ff6f082006-09-25 23:31:21 -070092
93static inline void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask)
94{
95}
96
97static inline void *percpu_populate(void *__pdata, size_t size, gfp_t gfp,
98 int cpu)
99{
100 return percpu_ptr(__pdata, cpu);
101}
102
103static inline int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
104 cpumask_t *mask)
105{
106 return 0;
107}
108
Al Viro0891a8d2006-09-29 01:58:34 -0700109static __always_inline void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask)
Martin Peschke7ff6f082006-09-25 23:31:21 -0700110{
111 return kzalloc(size, gfp);
112}
113
114static inline void percpu_free(void *__pdata)
115{
116 kfree(__pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119#endif /* CONFIG_SMP */
120
Martin Peschke7ff6f082006-09-25 23:31:21 -0700121#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))
127
128#define percpu_alloc(size, gfp) percpu_alloc_mask((size), (gfp), cpu_online_map)
129
130/* (legacy) interface for use without CPU hotplug handling */
131
132#define __alloc_percpu(size) percpu_alloc_mask((size), GFP_KERNEL, \
133 cpu_possible_map)
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))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138#endif /* __LINUX_PERCPU_H */