percpu-refcount.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. static void percpu_ref_kill_rcu(struct rcu_head *rcu)
  51. {
  52. struct percpu_ref *ref = container_of(rcu, struct percpu_ref, rcu);
  53. unsigned __percpu *pcpu_count = ref->pcpu_count;
  54. unsigned count = 0;
  55. int cpu;
  56. /* Mask out PCPU_REF_DEAD */
  57. pcpu_count = (unsigned __percpu *)
  58. (((unsigned long) pcpu_count) & ~PCPU_STATUS_MASK);
  59. for_each_possible_cpu(cpu)
  60. count += *per_cpu_ptr(pcpu_count, cpu);
  61. free_percpu(pcpu_count);
  62. pr_debug("global %i pcpu %i", atomic_read(&ref->count), (int) count);
  63. /*
  64. * It's crucial that we sum the percpu counters _before_ adding the sum
  65. * to &ref->count; since gets could be happening on one cpu while puts
  66. * happen on another, adding a single cpu's count could cause
  67. * @ref->count to hit 0 before we've got a consistent value - but the
  68. * sum of all the counts will be consistent and correct.
  69. *
  70. * Subtracting the bias value then has to happen _after_ adding count to
  71. * &ref->count; we need the bias value to prevent &ref->count from
  72. * reaching 0 before we add the percpu counts. But doing it at the same
  73. * time is equivalent and saves us atomic operations:
  74. */
  75. atomic_add((int) count - PCPU_COUNT_BIAS, &ref->count);
  76. /*
  77. * Now we're in single atomic_t mode with a consistent refcount, so it's
  78. * safe to drop our initial ref:
  79. */
  80. percpu_ref_put(ref);
  81. }
  82. /**
  83. * percpu_ref_kill - safely drop initial ref
  84. * @ref: percpu_ref to kill
  85. *
  86. * Must be used to drop the initial ref on a percpu refcount; must be called
  87. * precisely once before shutdown.
  88. *
  89. * Puts @ref in non percpu mode, then does a call_rcu() before gathering up the
  90. * percpu counters and dropping the initial ref.
  91. */
  92. void percpu_ref_kill(struct percpu_ref *ref)
  93. {
  94. WARN_ONCE(REF_STATUS(ref->pcpu_count) == PCPU_REF_DEAD,
  95. "percpu_ref_kill() called more than once!\n");
  96. ref->pcpu_count = (unsigned __percpu *)
  97. (((unsigned long) ref->pcpu_count)|PCPU_REF_DEAD);
  98. call_rcu(&ref->rcu, percpu_ref_kill_rcu);
  99. }