percpu-refcount.c 5.5 KB

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