percpu_counter.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Fast batching percpu counters.
  3. */
  4. #include <linux/percpu_counter.h>
  5. #include <linux/notifier.h>
  6. #include <linux/mutex.h>
  7. #include <linux/init.h>
  8. #include <linux/cpu.h>
  9. #include <linux/module.h>
  10. #ifdef CONFIG_HOTPLUG_CPU
  11. static LIST_HEAD(percpu_counters);
  12. static DEFINE_MUTEX(percpu_counters_lock);
  13. #endif
  14. void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
  15. {
  16. int cpu;
  17. spin_lock(&fbc->lock);
  18. for_each_possible_cpu(cpu) {
  19. s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
  20. *pcount = 0;
  21. }
  22. fbc->count = amount;
  23. spin_unlock(&fbc->lock);
  24. }
  25. EXPORT_SYMBOL(percpu_counter_set);
  26. void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
  27. {
  28. s64 count;
  29. s32 *pcount;
  30. int cpu = get_cpu();
  31. pcount = per_cpu_ptr(fbc->counters, cpu);
  32. count = *pcount + amount;
  33. if (count >= batch || count <= -batch) {
  34. spin_lock(&fbc->lock);
  35. fbc->count += count;
  36. *pcount = 0;
  37. spin_unlock(&fbc->lock);
  38. } else {
  39. *pcount = count;
  40. }
  41. put_cpu();
  42. }
  43. EXPORT_SYMBOL(__percpu_counter_add);
  44. /*
  45. * Add up all the per-cpu counts, return the result. This is a more accurate
  46. * but much slower version of percpu_counter_read_positive()
  47. */
  48. s64 __percpu_counter_sum(struct percpu_counter *fbc)
  49. {
  50. s64 ret;
  51. int cpu;
  52. spin_lock(&fbc->lock);
  53. ret = fbc->count;
  54. for_each_online_cpu(cpu) {
  55. s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
  56. ret += *pcount;
  57. }
  58. spin_unlock(&fbc->lock);
  59. return ret;
  60. }
  61. EXPORT_SYMBOL(__percpu_counter_sum);
  62. static struct lock_class_key percpu_counter_irqsafe;
  63. int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
  64. {
  65. spin_lock_init(&fbc->lock);
  66. fbc->count = amount;
  67. fbc->counters = alloc_percpu(s32);
  68. if (!fbc->counters)
  69. return -ENOMEM;
  70. #ifdef CONFIG_HOTPLUG_CPU
  71. mutex_lock(&percpu_counters_lock);
  72. list_add(&fbc->list, &percpu_counters);
  73. mutex_unlock(&percpu_counters_lock);
  74. #endif
  75. return 0;
  76. }
  77. EXPORT_SYMBOL(percpu_counter_init);
  78. int percpu_counter_init_irq(struct percpu_counter *fbc, s64 amount)
  79. {
  80. int err;
  81. err = percpu_counter_init(fbc, amount);
  82. if (!err)
  83. lockdep_set_class(&fbc->lock, &percpu_counter_irqsafe);
  84. return err;
  85. }
  86. void percpu_counter_destroy(struct percpu_counter *fbc)
  87. {
  88. if (!fbc->counters)
  89. return;
  90. free_percpu(fbc->counters);
  91. #ifdef CONFIG_HOTPLUG_CPU
  92. mutex_lock(&percpu_counters_lock);
  93. list_del(&fbc->list);
  94. mutex_unlock(&percpu_counters_lock);
  95. #endif
  96. }
  97. EXPORT_SYMBOL(percpu_counter_destroy);
  98. #ifdef CONFIG_HOTPLUG_CPU
  99. static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
  100. unsigned long action, void *hcpu)
  101. {
  102. unsigned int cpu;
  103. struct percpu_counter *fbc;
  104. if (action != CPU_DEAD)
  105. return NOTIFY_OK;
  106. cpu = (unsigned long)hcpu;
  107. mutex_lock(&percpu_counters_lock);
  108. list_for_each_entry(fbc, &percpu_counters, list) {
  109. s32 *pcount;
  110. unsigned long flags;
  111. spin_lock_irqsave(&fbc->lock, flags);
  112. pcount = per_cpu_ptr(fbc->counters, cpu);
  113. fbc->count += *pcount;
  114. *pcount = 0;
  115. spin_unlock_irqrestore(&fbc->lock, flags);
  116. }
  117. mutex_unlock(&percpu_counters_lock);
  118. return NOTIFY_OK;
  119. }
  120. static int __init percpu_counter_startup(void)
  121. {
  122. hotcpu_notifier(percpu_counter_hotcpu_callback, 0);
  123. return 0;
  124. }
  125. module_init(percpu_counter_startup);
  126. #endif