percpu_counter.c 5.0 KB

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