percpu_counter.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. *pcount = 0;
  58. }
  59. fbc->count = ret;
  60. spin_unlock(&fbc->lock);
  61. return ret;
  62. }
  63. EXPORT_SYMBOL(__percpu_counter_sum);
  64. static struct lock_class_key percpu_counter_irqsafe;
  65. int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
  66. {
  67. spin_lock_init(&fbc->lock);
  68. fbc->count = amount;
  69. fbc->counters = alloc_percpu(s32);
  70. if (!fbc->counters)
  71. return -ENOMEM;
  72. #ifdef CONFIG_HOTPLUG_CPU
  73. mutex_lock(&percpu_counters_lock);
  74. list_add(&fbc->list, &percpu_counters);
  75. mutex_unlock(&percpu_counters_lock);
  76. #endif
  77. return 0;
  78. }
  79. EXPORT_SYMBOL(percpu_counter_init);
  80. int percpu_counter_init_irq(struct percpu_counter *fbc, s64 amount)
  81. {
  82. int err;
  83. err = percpu_counter_init(fbc, amount);
  84. if (!err)
  85. lockdep_set_class(&fbc->lock, &percpu_counter_irqsafe);
  86. return err;
  87. }
  88. void percpu_counter_destroy(struct percpu_counter *fbc)
  89. {
  90. if (!fbc->counters)
  91. return;
  92. free_percpu(fbc->counters);
  93. fbc->counters = NULL;
  94. #ifdef CONFIG_HOTPLUG_CPU
  95. mutex_lock(&percpu_counters_lock);
  96. list_del(&fbc->list);
  97. mutex_unlock(&percpu_counters_lock);
  98. #endif
  99. }
  100. EXPORT_SYMBOL(percpu_counter_destroy);
  101. #ifdef CONFIG_HOTPLUG_CPU
  102. static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
  103. unsigned long action, void *hcpu)
  104. {
  105. unsigned int cpu;
  106. struct percpu_counter *fbc;
  107. if (action != CPU_DEAD)
  108. return NOTIFY_OK;
  109. cpu = (unsigned long)hcpu;
  110. mutex_lock(&percpu_counters_lock);
  111. list_for_each_entry(fbc, &percpu_counters, list) {
  112. s32 *pcount;
  113. unsigned long flags;
  114. spin_lock_irqsave(&fbc->lock, flags);
  115. pcount = per_cpu_ptr(fbc->counters, cpu);
  116. fbc->count += *pcount;
  117. *pcount = 0;
  118. spin_unlock_irqrestore(&fbc->lock, flags);
  119. }
  120. mutex_unlock(&percpu_counters_lock);
  121. return NOTIFY_OK;
  122. }
  123. static int __init percpu_counter_startup(void)
  124. {
  125. hotcpu_notifier(percpu_counter_hotcpu_callback, 0);
  126. return 0;
  127. }
  128. module_init(percpu_counter_startup);
  129. #endif