Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_PERCPU_H |
| 2 | #define __LINUX_PERCPU_H |
Martin Peschke | 7ff6f08 | 2006-09-25 23:31:21 -0700 | [diff] [blame] | 3 | |
Robert P. J. Day | 0a3021f | 2007-07-15 23:39:57 -0700 | [diff] [blame] | 4 | #include <linux/preempt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/slab.h> /* For kmalloc() */ |
| 6 | #include <linux/smp.h> |
| 7 | #include <linux/string.h> /* For memset() */ |
Martin Peschke | 7ff6f08 | 2006-09-25 23:31:21 -0700 | [diff] [blame] | 8 | #include <linux/cpumask.h> |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <asm/percpu.h> |
| 11 | |
travis@sgi.com | 5280e00 | 2008-01-30 13:32:52 +0100 | [diff] [blame] | 12 | #ifdef CONFIG_SMP |
| 13 | #define DEFINE_PER_CPU(type, name) \ |
| 14 | __attribute__((__section__(".data.percpu"))) \ |
| 15 | PER_CPU_ATTRIBUTES __typeof__(type) per_cpu__##name |
| 16 | |
| 17 | #define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \ |
| 18 | __attribute__((__section__(".data.percpu.shared_aligned"))) \ |
| 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 | |
| 25 | #define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \ |
| 26 | DEFINE_PER_CPU(type, name) |
| 27 | #endif |
| 28 | |
| 29 | #define EXPORT_PER_CPU_SYMBOL(var) EXPORT_SYMBOL(per_cpu__##var) |
| 30 | #define EXPORT_PER_CPU_SYMBOL_GPL(var) EXPORT_SYMBOL_GPL(per_cpu__##var) |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* Enough to cover all DEFINE_PER_CPUs in kernel, including modules. */ |
| 33 | #ifndef PERCPU_ENOUGH_ROOM |
Jeremy Fitzhardinge | b00742d3 | 2007-05-02 19:27:11 +0200 | [diff] [blame] | 34 | #ifdef CONFIG_MODULES |
| 35 | #define PERCPU_MODULE_RESERVE 8192 |
| 36 | #else |
| 37 | #define PERCPU_MODULE_RESERVE 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #endif |
| 39 | |
Jeremy Fitzhardinge | b00742d3 | 2007-05-02 19:27:11 +0200 | [diff] [blame] | 40 | #define PERCPU_ENOUGH_ROOM \ |
| 41 | (__per_cpu_end - __per_cpu_start + PERCPU_MODULE_RESERVE) |
| 42 | #endif /* PERCPU_ENOUGH_ROOM */ |
| 43 | |
Jan Blunck | 632bbfe | 2006-09-25 23:30:53 -0700 | [diff] [blame] | 44 | /* |
| 45 | * Must be an lvalue. Since @var must be a simple identifier, |
| 46 | * we force a syntax error here if it isn't. |
| 47 | */ |
| 48 | #define get_cpu_var(var) (*({ \ |
Jan Blunck | a666ecf | 2006-10-06 00:43:58 -0700 | [diff] [blame] | 49 | extern int simple_identifier_##var(void); \ |
Jan Blunck | 632bbfe | 2006-09-25 23:30:53 -0700 | [diff] [blame] | 50 | preempt_disable(); \ |
| 51 | &__get_cpu_var(var); })) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define put_cpu_var(var) preempt_enable() |
| 53 | |
| 54 | #ifdef CONFIG_SMP |
| 55 | |
| 56 | struct percpu_data { |
Eric Dumazet | b324215 | 2008-02-06 01:37:01 -0800 | [diff] [blame] | 57 | void *ptrs[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Martin Peschke | 7ff6f08 | 2006-09-25 23:31:21 -0700 | [diff] [blame] | 60 | #define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* |
Martin Peschke | 7ff6f08 | 2006-09-25 23:31:21 -0700 | [diff] [blame] | 62 | * Use this to get to a cpu's version of the per-cpu object dynamically |
| 63 | * allocated. Non-atomic access to the current CPU's version should |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | * probably be combined with get_cpu()/put_cpu(). |
| 65 | */ |
Martin Peschke | 7ff6f08 | 2006-09-25 23:31:21 -0700 | [diff] [blame] | 66 | #define percpu_ptr(ptr, cpu) \ |
| 67 | ({ \ |
| 68 | struct percpu_data *__p = __percpu_disguise(ptr); \ |
| 69 | (__typeof__(ptr))__p->ptrs[(cpu)]; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | }) |
| 71 | |
Martin Peschke | 7ff6f08 | 2006-09-25 23:31:21 -0700 | [diff] [blame] | 72 | extern void *percpu_populate(void *__pdata, size_t size, gfp_t gfp, int cpu); |
| 73 | extern void percpu_depopulate(void *__pdata, int cpu); |
| 74 | extern int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp, |
| 75 | cpumask_t *mask); |
| 76 | extern void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask); |
| 77 | extern void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask); |
| 78 | extern void percpu_free(void *__pdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | #else /* CONFIG_SMP */ |
| 81 | |
Martin Peschke | 7ff6f08 | 2006-09-25 23:31:21 -0700 | [diff] [blame] | 82 | #define percpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Martin Peschke | 7ff6f08 | 2006-09-25 23:31:21 -0700 | [diff] [blame] | 84 | static inline void percpu_depopulate(void *__pdata, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
Martin Peschke | 7ff6f08 | 2006-09-25 23:31:21 -0700 | [diff] [blame] | 87 | |
| 88 | static inline void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask) |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | static inline void *percpu_populate(void *__pdata, size_t size, gfp_t gfp, |
| 93 | int cpu) |
| 94 | { |
| 95 | return percpu_ptr(__pdata, cpu); |
| 96 | } |
| 97 | |
| 98 | static inline int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp, |
| 99 | cpumask_t *mask) |
| 100 | { |
| 101 | return 0; |
| 102 | } |
| 103 | |
Al Viro | 0891a8d | 2006-09-29 01:58:34 -0700 | [diff] [blame] | 104 | static __always_inline void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask) |
Martin Peschke | 7ff6f08 | 2006-09-25 23:31:21 -0700 | [diff] [blame] | 105 | { |
| 106 | return kzalloc(size, gfp); |
| 107 | } |
| 108 | |
| 109 | static inline void percpu_free(void *__pdata) |
| 110 | { |
| 111 | kfree(__pdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | #endif /* CONFIG_SMP */ |
| 115 | |
Martin Peschke | 7ff6f08 | 2006-09-25 23:31:21 -0700 | [diff] [blame] | 116 | #define percpu_populate_mask(__pdata, size, gfp, mask) \ |
| 117 | __percpu_populate_mask((__pdata), (size), (gfp), &(mask)) |
| 118 | #define percpu_depopulate_mask(__pdata, mask) \ |
| 119 | __percpu_depopulate_mask((__pdata), &(mask)) |
| 120 | #define percpu_alloc_mask(size, gfp, mask) \ |
| 121 | __percpu_alloc_mask((size), (gfp), &(mask)) |
| 122 | |
| 123 | #define percpu_alloc(size, gfp) percpu_alloc_mask((size), (gfp), cpu_online_map) |
| 124 | |
| 125 | /* (legacy) interface for use without CPU hotplug handling */ |
| 126 | |
| 127 | #define __alloc_percpu(size) percpu_alloc_mask((size), GFP_KERNEL, \ |
| 128 | cpu_possible_map) |
| 129 | #define alloc_percpu(type) (type *)__alloc_percpu(sizeof(type)) |
| 130 | #define free_percpu(ptr) percpu_free((ptr)) |
| 131 | #define per_cpu_ptr(ptr, cpu) percpu_ptr((ptr), (cpu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | #endif /* __LINUX_PERCPU_H */ |