percpu_counter.c 3.2 KB

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