percpu_counter.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 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 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 (count >= batch || 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. * Add up all the per-cpu counts, return the result. This is a more accurate
  88. * but much slower version of percpu_counter_read_positive()
  89. */
  90. s64 __percpu_counter_sum(struct percpu_counter *fbc)
  91. {
  92. s64 ret;
  93. int cpu;
  94. unsigned long flags;
  95. raw_spin_lock_irqsave(&fbc->lock, flags);
  96. ret = fbc->count;
  97. for_each_online_cpu(cpu) {
  98. s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
  99. ret += *pcount;
  100. }
  101. raw_spin_unlock_irqrestore(&fbc->lock, flags);
  102. return ret;
  103. }
  104. EXPORT_SYMBOL(__percpu_counter_sum);
  105. int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp,
  106. struct lock_class_key *key)
  107. {
  108. unsigned long flags __maybe_unused;
  109. raw_spin_lock_init(&fbc->lock);
  110. lockdep_set_class(&fbc->lock, key);
  111. fbc->count = amount;
  112. fbc->counters = alloc_percpu_gfp(s32, gfp);
  113. if (!fbc->counters)
  114. return -ENOMEM;
  115. debug_percpu_counter_activate(fbc);
  116. #ifdef CONFIG_HOTPLUG_CPU
  117. INIT_LIST_HEAD(&fbc->list);
  118. spin_lock_irqsave(&percpu_counters_lock, flags);
  119. list_add(&fbc->list, &percpu_counters);
  120. spin_unlock_irqrestore(&percpu_counters_lock, flags);
  121. #endif
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(__percpu_counter_init);
  125. void percpu_counter_destroy(struct percpu_counter *fbc)
  126. {
  127. unsigned long flags __maybe_unused;
  128. if (!fbc->counters)
  129. return;
  130. debug_percpu_counter_deactivate(fbc);
  131. #ifdef CONFIG_HOTPLUG_CPU
  132. spin_lock_irqsave(&percpu_counters_lock, flags);
  133. list_del(&fbc->list);
  134. spin_unlock_irqrestore(&percpu_counters_lock, flags);
  135. #endif
  136. free_percpu(fbc->counters);
  137. fbc->counters = NULL;
  138. }
  139. EXPORT_SYMBOL(percpu_counter_destroy);
  140. int percpu_counter_batch __read_mostly = 32;
  141. EXPORT_SYMBOL(percpu_counter_batch);
  142. static int compute_batch_value(unsigned int cpu)
  143. {
  144. int nr = num_online_cpus();
  145. percpu_counter_batch = max(32, nr*2);
  146. return 0;
  147. }
  148. static int percpu_counter_cpu_dead(unsigned int cpu)
  149. {
  150. #ifdef CONFIG_HOTPLUG_CPU
  151. struct percpu_counter *fbc;
  152. compute_batch_value(cpu);
  153. spin_lock_irq(&percpu_counters_lock);
  154. list_for_each_entry(fbc, &percpu_counters, list) {
  155. s32 *pcount;
  156. raw_spin_lock(&fbc->lock);
  157. pcount = per_cpu_ptr(fbc->counters, cpu);
  158. fbc->count += *pcount;
  159. *pcount = 0;
  160. raw_spin_unlock(&fbc->lock);
  161. }
  162. spin_unlock_irq(&percpu_counters_lock);
  163. #endif
  164. return 0;
  165. }
  166. /*
  167. * Compare counter against given value.
  168. * Return 1 if greater, 0 if equal and -1 if less
  169. */
  170. int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch)
  171. {
  172. s64 count;
  173. count = percpu_counter_read(fbc);
  174. /* Check to see if rough count will be sufficient for comparison */
  175. if (abs(count - rhs) > (batch * num_online_cpus())) {
  176. if (count > rhs)
  177. return 1;
  178. else
  179. return -1;
  180. }
  181. /* Need to use precise count */
  182. count = percpu_counter_sum(fbc);
  183. if (count > rhs)
  184. return 1;
  185. else if (count < rhs)
  186. return -1;
  187. else
  188. return 0;
  189. }
  190. EXPORT_SYMBOL(__percpu_counter_compare);
  191. static int __init percpu_counter_startup(void)
  192. {
  193. int ret;
  194. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "lib/percpu_cnt:online",
  195. compute_batch_value, NULL);
  196. WARN_ON(ret < 0);
  197. ret = cpuhp_setup_state_nocalls(CPUHP_PERCPU_CNT_DEAD,
  198. "lib/percpu_cnt:dead", NULL,
  199. percpu_counter_cpu_dead);
  200. WARN_ON(ret < 0);
  201. return 0;
  202. }
  203. module_init(percpu_counter_startup);