percpu_counter.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_SPINLOCK(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. unsigned long flags;
  53. raw_spin_lock_irqsave(&fbc->lock, flags);
  54. for_each_possible_cpu(cpu) {
  55. s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
  56. *pcount = 0;
  57. }
  58. fbc->count = amount;
  59. raw_spin_unlock_irqrestore(&fbc->lock, flags);
  60. }
  61. EXPORT_SYMBOL(percpu_counter_set);
  62. void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
  63. {
  64. s64 count;
  65. preempt_disable();
  66. count = __this_cpu_read(*fbc->counters) + amount;
  67. if (count >= batch || count <= -batch) {
  68. unsigned long flags;
  69. raw_spin_lock_irqsave(&fbc->lock, flags);
  70. fbc->count += count;
  71. raw_spin_unlock_irqrestore(&fbc->lock, flags);
  72. __this_cpu_write(*fbc->counters, 0);
  73. } else {
  74. __this_cpu_write(*fbc->counters, count);
  75. }
  76. preempt_enable();
  77. }
  78. EXPORT_SYMBOL(__percpu_counter_add);
  79. /*
  80. * Add up all the per-cpu counts, return the result. This is a more accurate
  81. * but much slower version of percpu_counter_read_positive()
  82. */
  83. s64 __percpu_counter_sum(struct percpu_counter *fbc)
  84. {
  85. s64 ret;
  86. int cpu;
  87. unsigned long flags;
  88. raw_spin_lock_irqsave(&fbc->lock, flags);
  89. ret = fbc->count;
  90. for_each_online_cpu(cpu) {
  91. s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
  92. ret += *pcount;
  93. }
  94. raw_spin_unlock_irqrestore(&fbc->lock, flags);
  95. return ret;
  96. }
  97. EXPORT_SYMBOL(__percpu_counter_sum);
  98. int __percpu_counter_init(struct percpu_counter *fbc, s64 amount,
  99. struct lock_class_key *key)
  100. {
  101. raw_spin_lock_init(&fbc->lock);
  102. lockdep_set_class(&fbc->lock, key);
  103. fbc->count = amount;
  104. fbc->counters = alloc_percpu(s32);
  105. if (!fbc->counters)
  106. return -ENOMEM;
  107. debug_percpu_counter_activate(fbc);
  108. #ifdef CONFIG_HOTPLUG_CPU
  109. INIT_LIST_HEAD(&fbc->list);
  110. spin_lock(&percpu_counters_lock);
  111. list_add(&fbc->list, &percpu_counters);
  112. spin_unlock(&percpu_counters_lock);
  113. #endif
  114. return 0;
  115. }
  116. EXPORT_SYMBOL(__percpu_counter_init);
  117. void percpu_counter_destroy(struct percpu_counter *fbc)
  118. {
  119. if (!fbc->counters)
  120. return;
  121. debug_percpu_counter_deactivate(fbc);
  122. #ifdef CONFIG_HOTPLUG_CPU
  123. spin_lock(&percpu_counters_lock);
  124. list_del(&fbc->list);
  125. spin_unlock(&percpu_counters_lock);
  126. #endif
  127. free_percpu(fbc->counters);
  128. fbc->counters = NULL;
  129. }
  130. EXPORT_SYMBOL(percpu_counter_destroy);
  131. int percpu_counter_batch __read_mostly = 32;
  132. EXPORT_SYMBOL(percpu_counter_batch);
  133. static void compute_batch_value(void)
  134. {
  135. int nr = num_online_cpus();
  136. percpu_counter_batch = max(32, nr*2);
  137. }
  138. static int percpu_counter_hotcpu_callback(struct notifier_block *nb,
  139. unsigned long action, void *hcpu)
  140. {
  141. #ifdef CONFIG_HOTPLUG_CPU
  142. unsigned int cpu;
  143. struct percpu_counter *fbc;
  144. compute_batch_value();
  145. if (action != CPU_DEAD)
  146. return NOTIFY_OK;
  147. cpu = (unsigned long)hcpu;
  148. spin_lock(&percpu_counters_lock);
  149. list_for_each_entry(fbc, &percpu_counters, list) {
  150. s32 *pcount;
  151. unsigned long flags;
  152. raw_spin_lock_irqsave(&fbc->lock, flags);
  153. pcount = per_cpu_ptr(fbc->counters, cpu);
  154. fbc->count += *pcount;
  155. *pcount = 0;
  156. raw_spin_unlock_irqrestore(&fbc->lock, flags);
  157. }
  158. spin_unlock(&percpu_counters_lock);
  159. #endif
  160. return NOTIFY_OK;
  161. }
  162. /*
  163. * Compare counter against given value.
  164. * Return 1 if greater, 0 if equal and -1 if less
  165. */
  166. int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
  167. {
  168. s64 count;
  169. count = percpu_counter_read(fbc);
  170. /* Check to see if rough count will be sufficient for comparison */
  171. if (abs(count - rhs) > (percpu_counter_batch*num_online_cpus())) {
  172. if (count > rhs)
  173. return 1;
  174. else
  175. return -1;
  176. }
  177. /* Need to use precise count */
  178. count = percpu_counter_sum(fbc);
  179. if (count > rhs)
  180. return 1;
  181. else if (count < rhs)
  182. return -1;
  183. else
  184. return 0;
  185. }
  186. EXPORT_SYMBOL(percpu_counter_compare);
  187. static int __init percpu_counter_startup(void)
  188. {
  189. compute_batch_value();
  190. hotcpu_notifier(percpu_counter_hotcpu_callback, 0);
  191. return 0;
  192. }
  193. module_init(percpu_counter_startup);