percpu_counter.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include <linux/debugobjects.h>
  11. #ifdef CONFIG_HOTPLUG_CPU
  12. static LIST_HEAD(percpu_counters);
  13. static DEFINE_MUTEX(percpu_counters_lock);
  14. #endif
  15. #ifdef CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER
  16. static struct debug_obj_descr percpu_counter_debug_descr;
  17. static int percpu_counter_fixup_free(void *addr, enum debug_obj_state state)
  18. {
  19. struct percpu_counter *fbc = addr;
  20. switch (state) {
  21. case ODEBUG_STATE_ACTIVE:
  22. percpu_counter_destroy(fbc);
  23. debug_object_free(fbc, &percpu_counter_debug_descr);
  24. return 1;
  25. default:
  26. return 0;
  27. }
  28. }
  29. static struct debug_obj_descr percpu_counter_debug_descr = {
  30. .name = "percpu_counter",
  31. .fixup_free = percpu_counter_fixup_free,
  32. };
  33. static inline void debug_percpu_counter_activate(struct percpu_counter *fbc)
  34. {
  35. debug_object_init(fbc, &percpu_counter_debug_descr);
  36. debug_object_activate(fbc, &percpu_counter_debug_descr);
  37. }
  38. static inline void debug_percpu_counter_deactivate(struct percpu_counter *fbc)
  39. {
  40. debug_object_deactivate(fbc, &percpu_counter_debug_descr);
  41. debug_object_free(fbc, &percpu_counter_debug_descr);
  42. }
  43. #else /* CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER */
  44. static inline void debug_percpu_counter_activate(struct percpu_counter *fbc)
  45. { }
  46. static inline void debug_percpu_counter_deactivate(struct percpu_counter *fbc)
  47. { }
  48. #endif /* CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER */
  49. void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
  50. {
  51. int cpu;
  52. raw_spin_lock(&fbc->lock);
  53. for_each_possible_cpu(cpu) {
  54. s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
  55. *pcount = 0;
  56. }
  57. fbc->count = amount;
  58. raw_spin_unlock(&fbc->lock);
  59. }
  60. EXPORT_SYMBOL(percpu_counter_set);
  61. void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
  62. {
  63. s64 count;
  64. preempt_disable();
  65. count = __this_cpu_read(*fbc->counters) + amount;
  66. if (count >= batch || count <= -batch) {
  67. raw_spin_lock(&fbc->lock);
  68. fbc->count += count;
  69. __this_cpu_write(*fbc->counters, 0);
  70. raw_spin_unlock(&fbc->lock);
  71. } else {
  72. __this_cpu_write(*fbc->counters, count);
  73. }
  74. preempt_enable();
  75. }
  76. EXPORT_SYMBOL(__percpu_counter_add);
  77. /*
  78. * Add up all the per-cpu counts, return the result. This is a more accurate
  79. * but much slower version of percpu_counter_read_positive()
  80. */
  81. s64 __percpu_counter_sum(struct percpu_counter *fbc)
  82. {
  83. s64 ret;
  84. int cpu;
  85. raw_spin_lock(&fbc->lock);
  86. ret = fbc->count;
  87. for_each_online_cpu(cpu) {
  88. s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
  89. ret += *pcount;
  90. }
  91. raw_spin_unlock(&fbc->lock);
  92. return ret;
  93. }
  94. EXPORT_SYMBOL(__percpu_counter_sum);
  95. int __percpu_counter_init(struct percpu_counter *fbc, s64 amount,
  96. struct lock_class_key *key)
  97. {
  98. raw_spin_lock_init(&fbc->lock);
  99. lockdep_set_class(&fbc->lock, key);
  100. fbc->count = amount;
  101. fbc->counters = alloc_percpu(s32);
  102. if (!fbc->counters)
  103. return -ENOMEM;
  104. debug_percpu_counter_activate(fbc);
  105. #ifdef CONFIG_HOTPLUG_CPU
  106. INIT_LIST_HEAD(&fbc->list);
  107. mutex_lock(&percpu_counters_lock);
  108. list_add(&fbc->list, &percpu_counters);
  109. mutex_unlock(&percpu_counters_lock);
  110. #endif
  111. return 0;
  112. }
  113. EXPORT_SYMBOL(__percpu_counter_init);
  114. void percpu_counter_destroy(struct percpu_counter *fbc)
  115. {
  116. if (!fbc->counters)
  117. return;
  118. debug_percpu_counter_deactivate(fbc);
  119. #ifdef CONFIG_HOTPLUG_CPU
  120. mutex_lock(&percpu_counters_lock);
  121. list_del(&fbc->list);
  122. mutex_unlock(&percpu_counters_lock);
  123. #endif
  124. free_percpu(fbc->counters);
  125. fbc->counters = NULL;
  126. }
  127. EXPORT_SYMBOL(percpu_counter_destroy);
  128. int percpu_counter_batch __read_mostly = 32;
  129. EXPORT_SYMBOL(percpu_counter_batch);
  130. static void compute_batch_value(void)
  131. {
  132. int nr = num_online_cpus();
  133. percpu_counter_batch = max(32, nr*2);
  134. }
  135. static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
  136. unsigned long action, void *hcpu)
  137. {
  138. #ifdef CONFIG_HOTPLUG_CPU
  139. unsigned int cpu;
  140. struct percpu_counter *fbc;
  141. compute_batch_value();
  142. if (action != CPU_DEAD)
  143. return NOTIFY_OK;
  144. cpu = (unsigned long)hcpu;
  145. mutex_lock(&percpu_counters_lock);
  146. list_for_each_entry(fbc, &percpu_counters, list) {
  147. s32 *pcount;
  148. unsigned long flags;
  149. raw_spin_lock_irqsave(&fbc->lock, flags);
  150. pcount = per_cpu_ptr(fbc->counters, cpu);
  151. fbc->count += *pcount;
  152. *pcount = 0;
  153. raw_spin_unlock_irqrestore(&fbc->lock, flags);
  154. }
  155. mutex_unlock(&percpu_counters_lock);
  156. #endif
  157. return NOTIFY_OK;
  158. }
  159. /*
  160. * Compare counter against given value.
  161. * Return 1 if greater, 0 if equal and -1 if less
  162. */
  163. int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
  164. {
  165. s64 count;
  166. count = percpu_counter_read(fbc);
  167. /* Check to see if rough count will be sufficient for comparison */
  168. if (abs(count - rhs) > (percpu_counter_batch*num_online_cpus())) {
  169. if (count > rhs)
  170. return 1;
  171. else
  172. return -1;
  173. }
  174. /* Need to use precise count */
  175. count = percpu_counter_sum(fbc);
  176. if (count > rhs)
  177. return 1;
  178. else if (count < rhs)
  179. return -1;
  180. else
  181. return 0;
  182. }
  183. EXPORT_SYMBOL(percpu_counter_compare);
  184. static int __init percpu_counter_startup(void)
  185. {
  186. compute_batch_value();
  187. hotcpu_notifier(percpu_counter_hotcpu_callback, 0);
  188. return 0;
  189. }
  190. module_init(percpu_counter_startup);