1 #ifndef __LINUX_PERCPU_H
2 #define __LINUX_PERCPU_H
3 #include <linux/preempt.h> /* For preempt_disable() */
4 #include <linux/slab.h> /* For kmalloc_percpu() */
5 #include <asm/percpu.h>
6 /* Must be an lvalue. */
7 #define get_cpu_var(var) (*({ preempt_disable(); &__get_cpu_var(var); }))
8 #define put_cpu_var(var) preempt_enable()
18 * Use this to get to a cpu's version of the per-cpu object allocated using
19 * kmalloc_percpu. If you want to get "this cpu's version", maybe you want
20 * to use get_cpu_ptr...
22 #define per_cpu_ptr(ptr, cpu) \
24 struct percpu_data *__p = (struct percpu_data *)~(unsigned long)(ptr); \
25 (__typeof__(ptr))__p->ptrs[(cpu)]; \
28 extern void *kmalloc_percpu(size_t size, int flags);
29 extern void kfree_percpu(const void *);
30 extern void kmalloc_percpu_init(void);
32 #else /* CONFIG_SMP */
34 #define per_cpu_ptr(ptr, cpu) (ptr)
36 static inline void *kmalloc_percpu(size_t size, int flags)
38 return(kmalloc(size, flags));
40 static inline void kfree_percpu(const void *ptr)
44 static inline void kmalloc_percpu_init(void) { }
46 #endif /* CONFIG_SMP */
49 * Use these with kmalloc_percpu. If
50 * 1. You want to operate on memory allocated by kmalloc_percpu (dereference
51 * and read/modify/write) AND
52 * 2. You want "this cpu's version" of the object AND
53 * 3. You want to do this safely since:
54 * a. On multiprocessors, you don't want to switch between cpus after
55 * you've read the current processor id due to preemption -- this would
56 * take away the implicit advantage to not have any kind of traditional
57 * serialization for per-cpu data
58 * b. On uniprocessors, you don't want another kernel thread messing
59 * up with the same per-cpu data due to preemption
61 * So, Use get_cpu_ptr to disable preemption and get pointer to the
62 * local cpu version of the per-cpu object. Use put_cpu_ptr to enable
63 * preemption. Operations on per-cpu data between get_ and put_ is
64 * then considered to be safe. And ofcourse, "Thou shalt not sleep between
65 * get_cpu_ptr and put_cpu_ptr"
67 #define get_cpu_ptr(ptr) per_cpu_ptr(ptr, get_cpu())
68 #define put_cpu_ptr(ptr) put_cpu()
70 #endif /* __LINUX_PERCPU_H */