percpu_counter.c 3.3 KB

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