percpu-refcount.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #define pr_fmt(fmt) "%s: " fmt "\n", __func__
  2. #include <linux/kernel.h>
  3. #include <linux/percpu-refcount.h>
  4. /*
  5. * Initially, a percpu refcount is just a set of percpu counters. Initially, we
  6. * don't try to detect the ref hitting 0 - which means that get/put can just
  7. * increment or decrement the local counter. Note that the counter on a
  8. * particular cpu can (and will) wrap - this is fine, when we go to shutdown the
  9. * percpu counters will all sum to the correct value
  10. *
  11. * (More precisely: because moduler arithmatic is commutative the sum of all the
  12. * pcpu_count vars will be equal to what it would have been if all the gets and
  13. * puts were done to a single integer, even if some of the percpu integers
  14. * overflow or underflow).
  15. *
  16. * The real trick to implementing percpu refcounts is shutdown. We can't detect
  17. * the ref hitting 0 on every put - this would require global synchronization
  18. * and defeat the whole purpose of using percpu refs.
  19. *
  20. * What we do is require the user to keep track of the initial refcount; we know
  21. * the ref can't hit 0 before the user drops the initial ref, so as long as we
  22. * convert to non percpu mode before the initial ref is dropped everything
  23. * works.
  24. *
  25. * Converting to non percpu mode is done with some RCUish stuff in
  26. * percpu_ref_kill. Additionally, we need a bias value so that the atomic_t
  27. * can't hit 0 before we've added up all the percpu refs.
  28. */
  29. #define PCPU_COUNT_BIAS (1U << 31)
  30. static unsigned __percpu *pcpu_count_ptr(struct percpu_ref *ref)
  31. {
  32. return (unsigned __percpu *)(ref->pcpu_count_ptr & ~PCPU_REF_DEAD);
  33. }
  34. /**
  35. * percpu_ref_init - initialize a percpu refcount
  36. * @ref: percpu_ref to initialize
  37. * @release: function which will be called when refcount hits 0
  38. *
  39. * Initializes the refcount in single atomic counter mode with a refcount of 1;
  40. * analagous to atomic_set(ref, 1).
  41. *
  42. * Note that @release must not sleep - it may potentially be called from RCU
  43. * callback context by percpu_ref_kill().
  44. */
  45. int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release)
  46. {
  47. atomic_set(&ref->count, 1 + PCPU_COUNT_BIAS);
  48. ref->pcpu_count_ptr = (unsigned long)alloc_percpu(unsigned);
  49. if (!ref->pcpu_count_ptr)
  50. return -ENOMEM;
  51. ref->release = release;
  52. return 0;
  53. }
  54. EXPORT_SYMBOL_GPL(percpu_ref_init);
  55. /**
  56. * percpu_ref_reinit - re-initialize a percpu refcount
  57. * @ref: perpcu_ref to re-initialize
  58. *
  59. * Re-initialize @ref so that it's in the same state as when it finished
  60. * percpu_ref_init(). @ref must have been initialized successfully, killed
  61. * and reached 0 but not exited.
  62. *
  63. * Note that percpu_ref_tryget[_live]() are safe to perform on @ref while
  64. * this function is in progress.
  65. */
  66. void percpu_ref_reinit(struct percpu_ref *ref)
  67. {
  68. unsigned __percpu *pcpu_count = pcpu_count_ptr(ref);
  69. int cpu;
  70. BUG_ON(!pcpu_count);
  71. WARN_ON(!percpu_ref_is_zero(ref));
  72. atomic_set(&ref->count, 1 + PCPU_COUNT_BIAS);
  73. /*
  74. * Restore per-cpu operation. smp_store_release() is paired with
  75. * smp_read_barrier_depends() in __pcpu_ref_alive() and guarantees
  76. * that the zeroing is visible to all percpu accesses which can see
  77. * the following PCPU_REF_DEAD clearing.
  78. */
  79. for_each_possible_cpu(cpu)
  80. *per_cpu_ptr(pcpu_count, cpu) = 0;
  81. smp_store_release(&ref->pcpu_count_ptr,
  82. ref->pcpu_count_ptr & ~PCPU_REF_DEAD);
  83. }
  84. EXPORT_SYMBOL_GPL(percpu_ref_reinit);
  85. /**
  86. * percpu_ref_exit - undo percpu_ref_init()
  87. * @ref: percpu_ref to exit
  88. *
  89. * This function exits @ref. The caller is responsible for ensuring that
  90. * @ref is no longer in active use. The usual places to invoke this
  91. * function from are the @ref->release() callback or in init failure path
  92. * where percpu_ref_init() succeeded but other parts of the initialization
  93. * of the embedding object failed.
  94. */
  95. void percpu_ref_exit(struct percpu_ref *ref)
  96. {
  97. unsigned __percpu *pcpu_count = pcpu_count_ptr(ref);
  98. if (pcpu_count) {
  99. free_percpu(pcpu_count);
  100. ref->pcpu_count_ptr = PCPU_REF_DEAD;
  101. }
  102. }
  103. EXPORT_SYMBOL_GPL(percpu_ref_exit);
  104. static void percpu_ref_kill_rcu(struct rcu_head *rcu)
  105. {
  106. struct percpu_ref *ref = container_of(rcu, struct percpu_ref, rcu);
  107. unsigned __percpu *pcpu_count = pcpu_count_ptr(ref);
  108. unsigned count = 0;
  109. int cpu;
  110. for_each_possible_cpu(cpu)
  111. count += *per_cpu_ptr(pcpu_count, cpu);
  112. pr_debug("global %i pcpu %i", atomic_read(&ref->count), (int) count);
  113. /*
  114. * It's crucial that we sum the percpu counters _before_ adding the sum
  115. * to &ref->count; since gets could be happening on one cpu while puts
  116. * happen on another, adding a single cpu's count could cause
  117. * @ref->count to hit 0 before we've got a consistent value - but the
  118. * sum of all the counts will be consistent and correct.
  119. *
  120. * Subtracting the bias value then has to happen _after_ adding count to
  121. * &ref->count; we need the bias value to prevent &ref->count from
  122. * reaching 0 before we add the percpu counts. But doing it at the same
  123. * time is equivalent and saves us atomic operations:
  124. */
  125. atomic_add((int) count - PCPU_COUNT_BIAS, &ref->count);
  126. WARN_ONCE(atomic_read(&ref->count) <= 0, "percpu ref <= 0 (%i)",
  127. atomic_read(&ref->count));
  128. /* @ref is viewed as dead on all CPUs, send out kill confirmation */
  129. if (ref->confirm_kill)
  130. ref->confirm_kill(ref);
  131. /*
  132. * Now we're in single atomic_t mode with a consistent refcount, so it's
  133. * safe to drop our initial ref:
  134. */
  135. percpu_ref_put(ref);
  136. }
  137. /**
  138. * percpu_ref_kill_and_confirm - drop the initial ref and schedule confirmation
  139. * @ref: percpu_ref to kill
  140. * @confirm_kill: optional confirmation callback
  141. *
  142. * Equivalent to percpu_ref_kill() but also schedules kill confirmation if
  143. * @confirm_kill is not NULL. @confirm_kill, which may not block, will be
  144. * called after @ref is seen as dead from all CPUs - all further
  145. * invocations of percpu_ref_tryget() will fail. See percpu_ref_tryget()
  146. * for more details.
  147. *
  148. * Due to the way percpu_ref is implemented, @confirm_kill will be called
  149. * after at least one full RCU grace period has passed but this is an
  150. * implementation detail and callers must not depend on it.
  151. */
  152. void percpu_ref_kill_and_confirm(struct percpu_ref *ref,
  153. percpu_ref_func_t *confirm_kill)
  154. {
  155. WARN_ONCE(ref->pcpu_count_ptr & PCPU_REF_DEAD,
  156. "percpu_ref_kill() called more than once!\n");
  157. ref->pcpu_count_ptr |= PCPU_REF_DEAD;
  158. ref->confirm_kill = confirm_kill;
  159. call_rcu_sched(&ref->rcu, percpu_ref_kill_rcu);
  160. }
  161. EXPORT_SYMBOL_GPL(percpu_ref_kill_and_confirm);