Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_PERCPU_H |
| 2 | #define __LINUX_PERCPU_H |
| 3 | #include <linux/spinlock.h> /* For preempt_disable() */ |
| 4 | #include <linux/slab.h> /* For kmalloc() */ |
| 5 | #include <linux/smp.h> |
| 6 | #include <linux/string.h> /* For memset() */ |
| 7 | #include <asm/percpu.h> |
| 8 | |
| 9 | /* Enough to cover all DEFINE_PER_CPUs in kernel, including modules. */ |
| 10 | #ifndef PERCPU_ENOUGH_ROOM |
| 11 | #define PERCPU_ENOUGH_ROOM 32768 |
| 12 | #endif |
| 13 | |
Jan Blunck | 632bbfe | 2006-09-25 23:30:53 -0700 | [diff] [blame^] | 14 | /* |
| 15 | * Must be an lvalue. Since @var must be a simple identifier, |
| 16 | * we force a syntax error here if it isn't. |
| 17 | */ |
| 18 | #define get_cpu_var(var) (*({ \ |
| 19 | extern int simple_indentifier_##var(void); \ |
| 20 | preempt_disable(); \ |
| 21 | &__get_cpu_var(var); })) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #define put_cpu_var(var) preempt_enable() |
| 23 | |
| 24 | #ifdef CONFIG_SMP |
| 25 | |
| 26 | struct percpu_data { |
| 27 | void *ptrs[NR_CPUS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | /* |
| 31 | * Use this to get to a cpu's version of the per-cpu object allocated using |
| 32 | * alloc_percpu. Non-atomic access to the current CPU's version should |
| 33 | * probably be combined with get_cpu()/put_cpu(). |
| 34 | */ |
| 35 | #define per_cpu_ptr(ptr, cpu) \ |
| 36 | ({ \ |
| 37 | struct percpu_data *__p = (struct percpu_data *)~(unsigned long)(ptr); \ |
| 38 | (__typeof__(ptr))__p->ptrs[(cpu)]; \ |
| 39 | }) |
| 40 | |
Pekka Enberg | f9f7500 | 2006-01-08 01:00:33 -0800 | [diff] [blame] | 41 | extern void *__alloc_percpu(size_t size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | extern void free_percpu(const void *); |
| 43 | |
| 44 | #else /* CONFIG_SMP */ |
| 45 | |
Paul Mundt | 66341a9 | 2005-11-13 16:07:21 -0800 | [diff] [blame] | 46 | #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Pekka Enberg | f9f7500 | 2006-01-08 01:00:33 -0800 | [diff] [blame] | 48 | static inline void *__alloc_percpu(size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
| 50 | void *ret = kmalloc(size, GFP_KERNEL); |
| 51 | if (ret) |
| 52 | memset(ret, 0, size); |
| 53 | return ret; |
| 54 | } |
| 55 | static inline void free_percpu(const void *ptr) |
| 56 | { |
| 57 | kfree(ptr); |
| 58 | } |
| 59 | |
| 60 | #endif /* CONFIG_SMP */ |
| 61 | |
| 62 | /* Simple wrapper for the common case: zeros memory. */ |
Pekka Enberg | f9f7500 | 2006-01-08 01:00:33 -0800 | [diff] [blame] | 63 | #define alloc_percpu(type) ((type *)(__alloc_percpu(sizeof(type)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | #endif /* __LINUX_PERCPU_H */ |