Rev 422 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
422 | giacomo | 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 | |||
14 | /* Must be an lvalue. */ |
||
15 | #define get_cpu_var(var) (*({ preempt_disable(); &__get_cpu_var(var); })) |
||
16 | #define put_cpu_var(var) preempt_enable() |
||
17 | |||
18 | #ifdef CONFIG_SMP |
||
19 | |||
20 | struct percpu_data { |
||
21 | void *ptrs[NR_CPUS]; |
||
22 | void *blkp; |
||
23 | }; |
||
24 | |||
25 | /* |
||
26 | * Use this to get to a cpu's version of the per-cpu object allocated using |
||
27 | * alloc_percpu. If you want to get "this cpu's version", maybe you want |
||
28 | * to use get_cpu_ptr... |
||
29 | */ |
||
30 | #define per_cpu_ptr(ptr, cpu) \ |
||
31 | ({ \ |
||
32 | struct percpu_data *__p = (struct percpu_data *)~(unsigned long)(ptr); \ |
||
33 | (__typeof__(ptr))__p->ptrs[(cpu)]; \ |
||
34 | }) |
||
35 | |||
36 | extern void *__alloc_percpu(size_t size, size_t align); |
||
37 | extern void free_percpu(const void *); |
||
38 | extern void kmalloc_percpu_init(void); |
||
39 | |||
40 | #else /* CONFIG_SMP */ |
||
41 | |||
42 | #define per_cpu_ptr(ptr, cpu) (ptr) |
||
43 | |||
44 | static inline void *__alloc_percpu(size_t size, size_t align) |
||
45 | { |
||
46 | void *ret = kmalloc(size, GFP_KERNEL); |
||
47 | if (ret) |
||
48 | memset(ret, 0, size); |
||
49 | return ret; |
||
50 | } |
||
51 | static inline void free_percpu(const void *ptr) |
||
52 | { |
||
53 | kfree(ptr); |
||
54 | } |
||
55 | static inline void kmalloc_percpu_init(void) { } |
||
56 | |||
57 | #endif /* CONFIG_SMP */ |
||
58 | |||
59 | /* Simple wrapper for the common case: zeros memory. */ |
||
60 | #define alloc_percpu(type) \ |
||
61 | ((type *)(__alloc_percpu(sizeof(type), __alignof__(type)))) |
||
62 | |||
63 | /* |
||
64 | * Use these with alloc_percpu. If |
||
65 | * 1. You want to operate on memory allocated by alloc_percpu (dereference |
||
66 | * and read/modify/write) AND |
||
67 | * 2. You want "this cpu's version" of the object AND |
||
68 | * 3. You want to do this safely since: |
||
69 | * a. On multiprocessors, you don't want to switch between cpus after |
||
70 | * you've read the current processor id due to preemption -- this would |
||
71 | * take away the implicit advantage to not have any kind of traditional |
||
72 | * serialization for per-cpu data |
||
73 | * b. On uniprocessors, you don't want another kernel thread messing |
||
74 | * up with the same per-cpu data due to preemption |
||
75 | * |
||
76 | * So, Use get_cpu_ptr to disable preemption and get pointer to the |
||
77 | * local cpu version of the per-cpu object. Use put_cpu_ptr to enable |
||
78 | * preemption. Operations on per-cpu data between get_ and put_ is |
||
79 | * then considered to be safe. And ofcourse, "Thou shalt not sleep between |
||
80 | * get_cpu_ptr and put_cpu_ptr" |
||
81 | */ |
||
82 | #define get_cpu_ptr(ptr) per_cpu_ptr(ptr, get_cpu()) |
||
83 | #define put_cpu_ptr(ptr) put_cpu() |
||
84 | |||
85 | #endif /* __LINUX_PERCPU_H */ |