refcount.h 4.1 KB

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