refcount.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Variant of atomic_t specialized for reference counts.
  3. *
  4. * The interface matches the atomic_t interface (to aid in porting) but only
  5. * provides the few functions one should use for reference counting.
  6. *
  7. * It differs in that the counter saturates at UINT_MAX and will not move once
  8. * there. This avoids wrapping the counter and causing 'spurious'
  9. * use-after-free issues.
  10. *
  11. * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
  12. * and provide only what is strictly required for refcounts.
  13. *
  14. * The increments are fully relaxed; these will not provide ordering. The
  15. * rationale is that whatever is used to obtain the object we're increasing the
  16. * reference count on will provide the ordering. For locked data structures,
  17. * its the lock acquire, for RCU/lockless data structures its the dependent
  18. * load.
  19. *
  20. * Do note that inc_not_zero() provides a control dependency which will order
  21. * future stores against the inc, this ensures we'll never modify the object
  22. * if we did not in fact acquire a reference.
  23. *
  24. * The decrements will provide release order, such that all the prior loads and
  25. * stores will be issued before, it also provides a control dependency, which
  26. * will order us against the subsequent free().
  27. *
  28. * The control dependency is against the load of the cmpxchg (ll/sc) that
  29. * succeeded. This means the stores aren't fully ordered, but this is fine
  30. * because the 1->0 transition indicates no concurrency.
  31. *
  32. * Note that the allocator is responsible for ordering things between free()
  33. * and alloc().
  34. *
  35. */
  36. #include <linux/refcount.h>
  37. #include <linux/bug.h>
  38. bool refcount_add_not_zero(unsigned int i, refcount_t *r)
  39. {
  40. unsigned int old, new, val = atomic_read(&r->refs);
  41. for (;;) {
  42. if (!val)
  43. return false;
  44. if (unlikely(val == UINT_MAX))
  45. return true;
  46. new = val + i;
  47. if (new < val)
  48. new = UINT_MAX;
  49. old = atomic_cmpxchg_relaxed(&r->refs, val, new);
  50. if (old == val)
  51. break;
  52. val = old;
  53. }
  54. WARN_ONCE(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
  55. return true;
  56. }
  57. EXPORT_SYMBOL_GPL(refcount_add_not_zero);
  58. void refcount_add(unsigned int i, refcount_t *r)
  59. {
  60. WARN_ONCE(!refcount_add_not_zero(i, r), "refcount_t: addition on 0; use-after-free.\n");
  61. }
  62. EXPORT_SYMBOL_GPL(refcount_add);
  63. /*
  64. * Similar to atomic_inc_not_zero(), will saturate at UINT_MAX and WARN.
  65. *
  66. * Provides no memory ordering, it is assumed the caller has guaranteed the
  67. * object memory to be stable (RCU, etc.). It does provide a control dependency
  68. * and thereby orders future stores. See the comment on top.
  69. */
  70. bool refcount_inc_not_zero(refcount_t *r)
  71. {
  72. unsigned int old, new, val = atomic_read(&r->refs);
  73. for (;;) {
  74. new = val + 1;
  75. if (!val)
  76. return false;
  77. if (unlikely(!new))
  78. return true;
  79. old = atomic_cmpxchg_relaxed(&r->refs, val, new);
  80. if (old == val)
  81. break;
  82. val = old;
  83. }
  84. WARN_ONCE(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
  85. return true;
  86. }
  87. EXPORT_SYMBOL_GPL(refcount_inc_not_zero);
  88. /*
  89. * Similar to atomic_inc(), will saturate at UINT_MAX and WARN.
  90. *
  91. * Provides no memory ordering, it is assumed the caller already has a
  92. * reference on the object, will WARN when this is not so.
  93. */
  94. void refcount_inc(refcount_t *r)
  95. {
  96. WARN_ONCE(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
  97. }
  98. EXPORT_SYMBOL_GPL(refcount_inc);
  99. bool refcount_sub_and_test(unsigned int i, refcount_t *r)
  100. {
  101. unsigned int old, new, val = atomic_read(&r->refs);
  102. for (;;) {
  103. if (unlikely(val == UINT_MAX))
  104. return false;
  105. new = val - i;
  106. if (new > val) {
  107. WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
  108. return false;
  109. }
  110. old = atomic_cmpxchg_release(&r->refs, val, new);
  111. if (old == val)
  112. break;
  113. val = old;
  114. }
  115. return !new;
  116. }
  117. EXPORT_SYMBOL_GPL(refcount_sub_and_test);
  118. /*
  119. * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
  120. * decrement when saturated at UINT_MAX.
  121. *
  122. * Provides release memory ordering, such that prior loads and stores are done
  123. * before, and provides a control dependency such that free() must come after.
  124. * See the comment on top.
  125. */
  126. bool refcount_dec_and_test(refcount_t *r)
  127. {
  128. return refcount_sub_and_test(1, r);
  129. }
  130. EXPORT_SYMBOL_GPL(refcount_dec_and_test);
  131. /*
  132. * Similar to atomic_dec(), it will WARN on underflow and fail to decrement
  133. * when saturated at UINT_MAX.
  134. *
  135. * Provides release memory ordering, such that prior loads and stores are done
  136. * before.
  137. */
  138. void refcount_dec(refcount_t *r)
  139. {
  140. WARN_ONCE(refcount_dec_and_test(r), "refcount_t: decrement hit 0; leaking memory.\n");
  141. }
  142. EXPORT_SYMBOL_GPL(refcount_dec);
  143. /*
  144. * No atomic_t counterpart, it attempts a 1 -> 0 transition and returns the
  145. * success thereof.
  146. *
  147. * Like all decrement operations, it provides release memory order and provides
  148. * a control dependency.
  149. *
  150. * It can be used like a try-delete operator; this explicit case is provided
  151. * and not cmpxchg in generic, because that would allow implementing unsafe
  152. * operations.
  153. */
  154. bool refcount_dec_if_one(refcount_t *r)
  155. {
  156. return atomic_cmpxchg_release(&r->refs, 1, 0) == 1;
  157. }
  158. EXPORT_SYMBOL_GPL(refcount_dec_if_one);
  159. /*
  160. * No atomic_t counterpart, it decrements unless the value is 1, in which case
  161. * it will return false.
  162. *
  163. * Was often done like: atomic_add_unless(&var, -1, 1)
  164. */
  165. bool refcount_dec_not_one(refcount_t *r)
  166. {
  167. unsigned int old, new, val = atomic_read(&r->refs);
  168. for (;;) {
  169. if (unlikely(val == UINT_MAX))
  170. return true;
  171. if (val == 1)
  172. return false;
  173. new = val - 1;
  174. if (new > val) {
  175. WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
  176. return true;
  177. }
  178. old = atomic_cmpxchg_release(&r->refs, val, new);
  179. if (old == val)
  180. break;
  181. val = old;
  182. }
  183. return true;
  184. }
  185. EXPORT_SYMBOL_GPL(refcount_dec_not_one);
  186. /*
  187. * Similar to atomic_dec_and_mutex_lock(), it will WARN on underflow and fail
  188. * to decrement when saturated at UINT_MAX.
  189. *
  190. * Provides release memory ordering, such that prior loads and stores are done
  191. * before, and provides a control dependency such that free() must come after.
  192. * See the comment on top.
  193. */
  194. bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock)
  195. {
  196. if (refcount_dec_not_one(r))
  197. return false;
  198. mutex_lock(lock);
  199. if (!refcount_dec_and_test(r)) {
  200. mutex_unlock(lock);
  201. return false;
  202. }
  203. return true;
  204. }
  205. EXPORT_SYMBOL_GPL(refcount_dec_and_mutex_lock);
  206. /*
  207. * Similar to atomic_dec_and_lock(), it will WARN on underflow and fail to
  208. * decrement when saturated at UINT_MAX.
  209. *
  210. * Provides release memory ordering, such that prior loads and stores are done
  211. * before, and provides a control dependency such that free() must come after.
  212. * See the comment on top.
  213. */
  214. bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock)
  215. {
  216. if (refcount_dec_not_one(r))
  217. return false;
  218. spin_lock(lock);
  219. if (!refcount_dec_and_test(r)) {
  220. spin_unlock(lock);
  221. return false;
  222. }
  223. return true;
  224. }
  225. EXPORT_SYMBOL_GPL(refcount_dec_and_lock);