percpu.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. /* Enough to cover all DEFINE_PER_CPUs in kernel, including modules. */
  9. #ifndef PERCPU_ENOUGH_ROOM
  10. #define PERCPU_ENOUGH_ROOM 32768
  11. #endif
  12. /* Must be an lvalue. */
  13. #define get_cpu_var(var) (*({ preempt_disable(); &__get_cpu_var(var); }))
  14. #define put_cpu_var(var) preempt_enable()
  15. #ifdef CONFIG_SMP
  16. struct percpu_data {
  17. void *ptrs[NR_CPUS];
  18. };
  19. /*
  20. * Use this to get to a cpu's version of the per-cpu object allocated using
  21. * alloc_percpu. Non-atomic access to the current CPU's version should
  22. * probably be combined with get_cpu()/put_cpu().
  23. */
  24. #define per_cpu_ptr(ptr, cpu) \
  25. ({ \
  26. struct percpu_data *__p = (struct percpu_data *)~(unsigned long)(ptr); \
  27. (__typeof__(ptr))__p->ptrs[(cpu)]; \
  28. })
  29. extern void *__alloc_percpu(size_t size);
  30. extern void free_percpu(const void *);
  31. #else /* CONFIG_SMP */
  32. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
  33. static inline void *__alloc_percpu(size_t size)
  34. {
  35. void *ret = kmalloc(size, GFP_KERNEL);
  36. if (ret)
  37. memset(ret, 0, size);
  38. return ret;
  39. }
  40. static inline void free_percpu(const void *ptr)
  41. {
  42. kfree(ptr);
  43. }
  44. #endif /* CONFIG_SMP */
  45. /* Simple wrapper for the common case: zeros memory. */
  46. #define alloc_percpu(type) ((type *)(__alloc_percpu(sizeof(type))))
  47. #endif /* __LINUX_PERCPU_H */