refcount.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _TOOLS_LINUX_REFCOUNT_H
  3. #define _TOOLS_LINUX_REFCOUNT_H
  4. /*
  5. * Variant of atomic_t specialized for reference counts.
  6. *
  7. * The interface matches the atomic_t interface (to aid in porting) but only
  8. * provides the few functions one should use for reference counting.
  9. *
  10. * It differs in that the counter saturates at UINT_MAX and will not move once
  11. * there. This avoids wrapping the counter and causing 'spurious'
  12. * use-after-free issues.
  13. *
  14. * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
  15. * and provide only what is strictly required for refcounts.
  16. *
  17. * The increments are fully relaxed; these will not provide ordering. The
  18. * rationale is that whatever is used to obtain the object we're increasing the
  19. * reference count on will provide the ordering. For locked data structures,
  20. * its the lock acquire, for RCU/lockless data structures its the dependent
  21. * load.
  22. *
  23. * Do note that inc_not_zero() provides a control dependency which will order
  24. * future stores against the inc, this ensures we'll never modify the object
  25. * if we did not in fact acquire a reference.
  26. *
  27. * The decrements will provide release order, such that all the prior loads and
  28. * stores will be issued before, it also provides a control dependency, which
  29. * will order us against the subsequent free().
  30. *
  31. * The control dependency is against the load of the cmpxchg (ll/sc) that
  32. * succeeded. This means the stores aren't fully ordered, but this is fine
  33. * because the 1->0 transition indicates no concurrency.
  34. *
  35. * Note that the allocator is responsible for ordering things between free()
  36. * and alloc().
  37. *
  38. */
  39. #include <linux/atomic.h>
  40. #include <linux/kernel.h>
  41. #ifdef NDEBUG
  42. #define REFCOUNT_WARN(cond, str) (void)(cond)
  43. #define __refcount_check
  44. #else
  45. #define REFCOUNT_WARN(cond, str) BUG_ON(cond)
  46. #define __refcount_check __must_check
  47. #endif
  48. typedef struct refcount_struct {
  49. atomic_t refs;
  50. } refcount_t;
  51. #define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), }
  52. static inline void refcount_set(refcount_t *r, unsigned int n)
  53. {
  54. atomic_set(&r->refs, n);
  55. }
  56. static inline unsigned int refcount_read(const refcount_t *r)
  57. {
  58. return atomic_read(&r->refs);
  59. }
  60. /*
  61. * Similar to atomic_inc_not_zero(), will saturate at UINT_MAX and WARN.
  62. *
  63. * Provides no memory ordering, it is assumed the caller has guaranteed the
  64. * object memory to be stable (RCU, etc.). It does provide a control dependency
  65. * and thereby orders future stores. See the comment on top.
  66. */
  67. static inline __refcount_check
  68. bool refcount_inc_not_zero(refcount_t *r)
  69. {
  70. unsigned int old, new, val = atomic_read(&r->refs);
  71. for (;;) {
  72. new = val + 1;
  73. if (!val)
  74. return false;
  75. if (unlikely(!new))
  76. return true;
  77. old = atomic_cmpxchg_relaxed(&r->refs, val, new);
  78. if (old == val)
  79. break;
  80. val = old;
  81. }
  82. REFCOUNT_WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
  83. return true;
  84. }
  85. /*
  86. * Similar to atomic_inc(), will saturate at UINT_MAX and WARN.
  87. *
  88. * Provides no memory ordering, it is assumed the caller already has a
  89. * reference on the object, will WARN when this is not so.
  90. */
  91. static inline void refcount_inc(refcount_t *r)
  92. {
  93. REFCOUNT_WARN(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
  94. }
  95. /*
  96. * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
  97. * decrement when saturated at UINT_MAX.
  98. *
  99. * Provides release memory ordering, such that prior loads and stores are done
  100. * before, and provides a control dependency such that free() must come after.
  101. * See the comment on top.
  102. */
  103. static inline __refcount_check
  104. bool refcount_sub_and_test(unsigned int i, refcount_t *r)
  105. {
  106. unsigned int old, new, val = atomic_read(&r->refs);
  107. for (;;) {
  108. if (unlikely(val == UINT_MAX))
  109. return false;
  110. new = val - i;
  111. if (new > val) {
  112. REFCOUNT_WARN(new > val, "refcount_t: underflow; use-after-free.\n");
  113. return false;
  114. }
  115. old = atomic_cmpxchg_release(&r->refs, val, new);
  116. if (old == val)
  117. break;
  118. val = old;
  119. }
  120. return !new;
  121. }
  122. static inline __refcount_check
  123. bool refcount_dec_and_test(refcount_t *r)
  124. {
  125. return refcount_sub_and_test(1, r);
  126. }
  127. #endif /* _ATOMIC_LINUX_REFCOUNT_H */