percpu-refcount.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /**
  31. * percpu_ref_init - initialize a percpu refcount
  32. * @ref: percpu_ref to initialize
  33. * @release: function which will be called when refcount hits 0
  34. *
  35. * Initializes the refcount in single atomic counter mode with a refcount of 1;
  36. * analagous to atomic_set(ref, 1).
  37. *
  38. * Note that @release must not sleep - it may potentially be called from RCU
  39. * callback context by percpu_ref_kill().
  40. */
  41. int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release)
  42. {
  43. atomic_set(&ref->count, 1 + PCPU_COUNT_BIAS);
  44. ref->pcpu_count = alloc_percpu(unsigned);
  45. if (!ref->pcpu_count)
  46. return -ENOMEM;
  47. ref->release = release;
  48. return 0;
  49. }
  50. EXPORT_SYMBOL_GPL(percpu_ref_init);
  51. /**
  52. * percpu_ref_cancel_init - cancel percpu_ref_init()
  53. * @ref: percpu_ref to cancel init for
  54. *
  55. * Once a percpu_ref is initialized, its destruction is initiated by
  56. * percpu_ref_kill() and completes asynchronously, which can be painful to
  57. * do when destroying a half-constructed object in init failure path.
  58. *
  59. * This function destroys @ref without invoking @ref->release and the
  60. * memory area containing it can be freed immediately on return. To
  61. * prevent accidental misuse, it's required that @ref has finished
  62. * percpu_ref_init(), whether successful or not, but never used.
  63. *
  64. * The weird name and usage restriction are to prevent people from using
  65. * this function by mistake for normal shutdown instead of
  66. * percpu_ref_kill().
  67. */
  68. void percpu_ref_cancel_init(struct percpu_ref *ref)
  69. {
  70. unsigned __percpu *pcpu_count = ref->pcpu_count;
  71. int cpu;
  72. WARN_ON_ONCE(atomic_read(&ref->count) != 1 + PCPU_COUNT_BIAS);
  73. if (pcpu_count) {
  74. for_each_possible_cpu(cpu)
  75. WARN_ON_ONCE(*per_cpu_ptr(pcpu_count, cpu));
  76. free_percpu(ref->pcpu_count);
  77. }
  78. }
  79. EXPORT_SYMBOL_GPL(percpu_ref_cancel_init);
  80. static void percpu_ref_kill_rcu(struct rcu_head *rcu)
  81. {
  82. struct percpu_ref *ref = container_of(rcu, struct percpu_ref, rcu);
  83. unsigned __percpu *pcpu_count = ref->pcpu_count;
  84. unsigned count = 0;
  85. int cpu;
  86. /* Mask out PCPU_REF_DEAD */
  87. pcpu_count = (unsigned __percpu *)
  88. (((unsigned long) pcpu_count) & ~PCPU_STATUS_MASK);
  89. for_each_possible_cpu(cpu)
  90. count += *per_cpu_ptr(pcpu_count, cpu);
  91. free_percpu(pcpu_count);
  92. pr_debug("global %i pcpu %i", atomic_read(&ref->count), (int) count);
  93. /*
  94. * It's crucial that we sum the percpu counters _before_ adding the sum
  95. * to &ref->count; since gets could be happening on one cpu while puts
  96. * happen on another, adding a single cpu's count could cause
  97. * @ref->count to hit 0 before we've got a consistent value - but the
  98. * sum of all the counts will be consistent and correct.
  99. *
  100. * Subtracting the bias value then has to happen _after_ adding count to
  101. * &ref->count; we need the bias value to prevent &ref->count from
  102. * reaching 0 before we add the percpu counts. But doing it at the same
  103. * time is equivalent and saves us atomic operations:
  104. */
  105. atomic_add((int) count - PCPU_COUNT_BIAS, &ref->count);
  106. WARN_ONCE(atomic_read(&ref->count) <= 0, "percpu ref <= 0 (%i)",
  107. atomic_read(&ref->count));
  108. /* @ref is viewed as dead on all CPUs, send out kill confirmation */
  109. if (ref->confirm_kill)
  110. ref->confirm_kill(ref);
  111. /*
  112. * Now we're in single atomic_t mode with a consistent refcount, so it's
  113. * safe to drop our initial ref:
  114. */
  115. percpu_ref_put(ref);
  116. }
  117. /**
  118. * percpu_ref_kill_and_confirm - drop the initial ref and schedule confirmation
  119. * @ref: percpu_ref to kill
  120. * @confirm_kill: optional confirmation callback
  121. *
  122. * Equivalent to percpu_ref_kill() but also schedules kill confirmation if
  123. * @confirm_kill is not NULL. @confirm_kill, which may not block, will be
  124. * called after @ref is seen as dead from all CPUs - all further
  125. * invocations of percpu_ref_tryget() will fail. See percpu_ref_tryget()
  126. * for more details.
  127. *
  128. * Due to the way percpu_ref is implemented, @confirm_kill will be called
  129. * after at least one full RCU grace period has passed but this is an
  130. * implementation detail and callers must not depend on it.
  131. */
  132. void percpu_ref_kill_and_confirm(struct percpu_ref *ref,
  133. percpu_ref_func_t *confirm_kill)
  134. {
  135. WARN_ONCE(REF_STATUS(ref->pcpu_count) == PCPU_REF_DEAD,
  136. "percpu_ref_kill() called more than once!\n");
  137. ref->pcpu_count = (unsigned __percpu *)
  138. (((unsigned long) ref->pcpu_count)|PCPU_REF_DEAD);
  139. ref->confirm_kill = confirm_kill;
  140. call_rcu_sched(&ref->rcu, percpu_ref_kill_rcu);
  141. }
  142. EXPORT_SYMBOL_GPL(percpu_ref_kill_and_confirm);