percpu_counter.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Fast batching percpu counters.
  4. */
  5. #include <linux/percpu_counter.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 const struct debug_obj_descr percpu_counter_debug_descr;
  17. static bool 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 true;
  25. default:
  26. return false;
  27. }
  28. }
  29. static const 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. /**
  63. * This function is both preempt and irq safe. The former is due to explicit
  64. * preemption disable. The latter is guaranteed by the fact that the slow path
  65. * is explicitly protected by an irq-safe spinlock whereas the fast patch uses
  66. * this_cpu_add which is irq-safe by definition. Hence there is no need muck
  67. * with irq state before calling this one
  68. */
  69. void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch)
  70. {
  71. s64 count;
  72. preempt_disable();
  73. count = __this_cpu_read(*fbc->counters) + amount;
  74. if (abs(count) >= batch) {
  75. unsigned long flags;
  76. raw_spin_lock_irqsave(&fbc->lock, flags);
  77. fbc->count += count;
  78. __this_cpu_sub(*fbc->counters, count - amount);
  79. raw_spin_unlock_irqrestore(&fbc->lock, flags);
  80. } else {
  81. this_cpu_add(*fbc->counters, amount);
  82. }
  83. preempt_enable();
  84. }
  85. EXPORT_SYMBOL(percpu_counter_add_batch);
  86. /*
  87. * For percpu_counter with a big batch, the devication of its count could
  88. * be big, and there is requirement to reduce the deviation, like when the
  89. * counter's batch could be runtime decreased to get a better accuracy,
  90. * which can be achieved by running this sync function on each CPU.
  91. */
  92. void percpu_counter_sync(struct percpu_counter *fbc)
  93. {
  94. unsigned long flags;
  95. s64 count;
  96. raw_spin_lock_irqsave(&fbc->lock, flags);
  97. count = __this_cpu_read(*fbc->counters);
  98. fbc->count += count;
  99. __this_cpu_sub(*fbc->counters, count);
  100. raw_spin_unlock_irqrestore(&fbc->lock, flags);
  101. }
  102. EXPORT_SYMBOL(percpu_counter_sync);
  103. /*
  104. * Add up all the per-cpu counts, return the result. This is a more accurate
  105. * but much slower version of percpu_counter_read_positive()
  106. */
  107. s64 __percpu_counter_sum(struct percpu_counter *fbc)
  108. {
  109. s64 ret;
  110. int cpu;
  111. unsigned long flags;
  112. raw_spin_lock_irqsave(&fbc->lock, flags);
  113. ret = fbc->count;
  114. for_each_online_cpu(cpu) {
  115. s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
  116. ret += *pcount;
  117. }
  118. raw_spin_unlock_irqrestore(&fbc->lock, flags);
  119. return ret;
  120. }
  121. EXPORT_SYMBOL(__percpu_counter_sum);
  122. int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp,
  123. struct lock_class_key *key)
  124. {
  125. unsigned long flags __maybe_unused;
  126. raw_spin_lock_init(&fbc->lock);
  127. lockdep_set_class(&fbc->lock, key);
  128. fbc->count = amount;
  129. fbc->counters = alloc_percpu_gfp(s32, gfp);
  130. if (!fbc->counters)
  131. return -ENOMEM;
  132. debug_percpu_counter_activate(fbc);
  133. #ifdef CONFIG_HOTPLUG_CPU
  134. INIT_LIST_HEAD(&fbc->list);
  135. spin_lock_irqsave(&percpu_counters_lock, flags);
  136. list_add(&fbc->list, &percpu_counters);
  137. spin_unlock_irqrestore(&percpu_counters_lock, flags);
  138. #endif
  139. return 0;
  140. }
  141. EXPORT_SYMBOL(__percpu_counter_init);
  142. void percpu_counter_destroy(struct percpu_counter *fbc)
  143. {
  144. unsigned long flags __maybe_unused;
  145. if (!fbc->counters)
  146. return;
  147. debug_percpu_counter_deactivate(fbc);
  148. #ifdef CONFIG_HOTPLUG_CPU
  149. spin_lock_irqsave(&percpu_counters_lock, flags);
  150. list_del(&fbc->list);
  151. spin_unlock_irqrestore(&percpu_counters_lock, flags);
  152. #endif
  153. free_percpu(fbc->counters);
  154. fbc->counters = NULL;
  155. }
  156. EXPORT_SYMBOL(percpu_counter_destroy);
  157. int percpu_counter_batch __read_mostly = 32;
  158. EXPORT_SYMBOL(percpu_counter_batch);
  159. static int compute_batch_value(unsigned int cpu)
  160. {
  161. int nr = num_online_cpus();
  162. percpu_counter_batch = max(32, nr*2);
  163. return 0;
  164. }
  165. static int percpu_counter_cpu_dead(unsigned int cpu)
  166. {
  167. #ifdef CONFIG_HOTPLUG_CPU
  168. struct percpu_counter *fbc;
  169. compute_batch_value(cpu);
  170. spin_lock_irq(&percpu_counters_lock);
  171. list_for_each_entry(fbc, &percpu_counters, list) {
  172. s32 *pcount;
  173. raw_spin_lock(&fbc->lock);
  174. pcount = per_cpu_ptr(fbc->counters, cpu);
  175. fbc->count += *pcount;
  176. *pcount = 0;
  177. raw_spin_unlock(&fbc->lock);
  178. }
  179. spin_unlock_irq(&percpu_counters_lock);
  180. #endif
  181. return 0;
  182. }
  183. /*
  184. * Compare counter against given value.
  185. * Return 1 if greater, 0 if equal and -1 if less
  186. */
  187. int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch)
  188. {
  189. s64 count;
  190. count = percpu_counter_read(fbc);
  191. /* Check to see if rough count will be sufficient for comparison */
  192. if (abs(count - rhs) > (batch * num_online_cpus())) {
  193. if (count > rhs)
  194. return 1;
  195. else
  196. return -1;
  197. }
  198. /* Need to use precise count */
  199. count = percpu_counter_sum(fbc);
  200. if (count > rhs)
  201. return 1;
  202. else if (count < rhs)
  203. return -1;
  204. else
  205. return 0;
  206. }
  207. EXPORT_SYMBOL(__percpu_counter_compare);
  208. static int __init percpu_counter_startup(void)
  209. {
  210. int ret;
  211. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "lib/percpu_cnt:online",
  212. compute_batch_value, NULL);
  213. WARN_ON(ret < 0);
  214. ret = cpuhp_setup_state_nocalls(CPUHP_PERCPU_CNT_DEAD,
  215. "lib/percpu_cnt:dead", NULL,
  216. percpu_counter_cpu_dead);
  217. WARN_ON(ret < 0);
  218. return 0;
  219. }
  220. module_init(percpu_counter_startup);