refcount.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include <linux/module.h>
  39. #ifdef CONFIG_REFCOUNT_FULL
  40. /**
  41. * refcount_add_not_zero - add a value to a refcount unless it is 0
  42. * @i: the value to add to the refcount
  43. * @r: the refcount
  44. *
  45. * Will saturate at UINT_MAX and WARN.
  46. *
  47. * Provides no memory ordering, it is assumed the caller has guaranteed the
  48. * object memory to be stable (RCU, etc.). It does provide a control dependency
  49. * and thereby orders future stores. See the comment on top.
  50. *
  51. * Use of this function is not recommended for the normal reference counting
  52. * use case in which references are taken and released one at a time. In these
  53. * cases, refcount_inc(), or one of its variants, should instead be used to
  54. * increment a reference count.
  55. *
  56. * Return: false if the passed refcount is 0, true otherwise
  57. */
  58. bool refcount_add_not_zero(unsigned int i, refcount_t *r)
  59. {
  60. unsigned int new, val = atomic_read(&r->refs);
  61. do {
  62. if (!val)
  63. return false;
  64. if (unlikely(val == UINT_MAX))
  65. return true;
  66. new = val + i;
  67. if (new < val)
  68. new = UINT_MAX;
  69. } while (!atomic_try_cmpxchg_relaxed(&r->refs, &val, new));
  70. WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
  71. return true;
  72. }
  73. EXPORT_SYMBOL(refcount_add_not_zero);
  74. /**
  75. * refcount_add - add a value to a refcount
  76. * @i: the value to add to the refcount
  77. * @r: the refcount
  78. *
  79. * Similar to atomic_add(), but will saturate at UINT_MAX and WARN.
  80. *
  81. * Provides no memory ordering, it is assumed the caller has guaranteed the
  82. * object memory to be stable (RCU, etc.). It does provide a control dependency
  83. * and thereby orders future stores. See the comment on top.
  84. *
  85. * Use of this function is not recommended for the normal reference counting
  86. * use case in which references are taken and released one at a time. In these
  87. * cases, refcount_inc(), or one of its variants, should instead be used to
  88. * increment a reference count.
  89. */
  90. void refcount_add(unsigned int i, refcount_t *r)
  91. {
  92. WARN(!refcount_add_not_zero(i, r), "refcount_t: addition on 0; use-after-free.\n");
  93. }
  94. EXPORT_SYMBOL(refcount_add);
  95. /**
  96. * refcount_inc_not_zero - increment a refcount unless it is 0
  97. * @r: the refcount to increment
  98. *
  99. * Similar to atomic_inc_not_zero(), but will saturate at UINT_MAX and WARN.
  100. *
  101. * Provides no memory ordering, it is assumed the caller has guaranteed the
  102. * object memory to be stable (RCU, etc.). It does provide a control dependency
  103. * and thereby orders future stores. See the comment on top.
  104. *
  105. * Return: true if the increment was successful, false otherwise
  106. */
  107. bool refcount_inc_not_zero(refcount_t *r)
  108. {
  109. unsigned int new, val = atomic_read(&r->refs);
  110. do {
  111. new = val + 1;
  112. if (!val)
  113. return false;
  114. if (unlikely(!new))
  115. return true;
  116. } while (!atomic_try_cmpxchg_relaxed(&r->refs, &val, new));
  117. WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
  118. return true;
  119. }
  120. EXPORT_SYMBOL(refcount_inc_not_zero);
  121. /**
  122. * refcount_inc - increment a refcount
  123. * @r: the refcount to increment
  124. *
  125. * Similar to atomic_inc(), but will saturate at UINT_MAX and WARN.
  126. *
  127. * Provides no memory ordering, it is assumed the caller already has a
  128. * reference on the object.
  129. *
  130. * Will WARN if the refcount is 0, as this represents a possible use-after-free
  131. * condition.
  132. */
  133. void refcount_inc(refcount_t *r)
  134. {
  135. WARN(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
  136. }
  137. EXPORT_SYMBOL(refcount_inc);
  138. /**
  139. * refcount_sub_and_test - subtract from a refcount and test if it is 0
  140. * @i: amount to subtract from the refcount
  141. * @r: the refcount
  142. *
  143. * Similar to atomic_dec_and_test(), but it will WARN, return false and
  144. * ultimately leak on underflow and will fail to decrement when saturated
  145. * at UINT_MAX.
  146. *
  147. * Provides release memory ordering, such that prior loads and stores are done
  148. * before, and provides a control dependency such that free() must come after.
  149. * See the comment on top.
  150. *
  151. * Use of this function is not recommended for the normal reference counting
  152. * use case in which references are taken and released one at a time. In these
  153. * cases, refcount_dec(), or one of its variants, should instead be used to
  154. * decrement a reference count.
  155. *
  156. * Return: true if the resulting refcount is 0, false otherwise
  157. */
  158. bool refcount_sub_and_test(unsigned int i, refcount_t *r)
  159. {
  160. unsigned int new, val = atomic_read(&r->refs);
  161. do {
  162. if (unlikely(val == UINT_MAX))
  163. return false;
  164. new = val - i;
  165. if (new > val) {
  166. WARN(new > val, "refcount_t: underflow; use-after-free.\n");
  167. return false;
  168. }
  169. } while (!atomic_try_cmpxchg_release(&r->refs, &val, new));
  170. return !new;
  171. }
  172. EXPORT_SYMBOL(refcount_sub_and_test);
  173. /**
  174. * refcount_dec_and_test - decrement a refcount and test if it is 0
  175. * @r: the refcount
  176. *
  177. * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
  178. * decrement when saturated at UINT_MAX.
  179. *
  180. * Provides release memory ordering, such that prior loads and stores are done
  181. * before, and provides a control dependency such that free() must come after.
  182. * See the comment on top.
  183. *
  184. * Return: true if the resulting refcount is 0, false otherwise
  185. */
  186. bool refcount_dec_and_test(refcount_t *r)
  187. {
  188. return refcount_sub_and_test(1, r);
  189. }
  190. EXPORT_SYMBOL(refcount_dec_and_test);
  191. /**
  192. * refcount_dec - decrement a refcount
  193. * @r: the refcount
  194. *
  195. * Similar to atomic_dec(), it will WARN on underflow and fail to decrement
  196. * when saturated at UINT_MAX.
  197. *
  198. * Provides release memory ordering, such that prior loads and stores are done
  199. * before.
  200. */
  201. void refcount_dec(refcount_t *r)
  202. {
  203. WARN(refcount_dec_and_test(r), "refcount_t: decrement hit 0; leaking memory.\n");
  204. }
  205. EXPORT_SYMBOL(refcount_dec);
  206. #endif /* CONFIG_REFCOUNT_FULL */
  207. /**
  208. * refcount_dec_if_one - decrement a refcount if it is 1
  209. * @r: the refcount
  210. *
  211. * No atomic_t counterpart, it attempts a 1 -> 0 transition and returns the
  212. * success thereof.
  213. *
  214. * Like all decrement operations, it provides release memory order and provides
  215. * a control dependency.
  216. *
  217. * It can be used like a try-delete operator; this explicit case is provided
  218. * and not cmpxchg in generic, because that would allow implementing unsafe
  219. * operations.
  220. *
  221. * Return: true if the resulting refcount is 0, false otherwise
  222. */
  223. bool refcount_dec_if_one(refcount_t *r)
  224. {
  225. int val = 1;
  226. return atomic_try_cmpxchg_release(&r->refs, &val, 0);
  227. }
  228. EXPORT_SYMBOL(refcount_dec_if_one);
  229. /**
  230. * refcount_dec_not_one - decrement a refcount if it is not 1
  231. * @r: the refcount
  232. *
  233. * No atomic_t counterpart, it decrements unless the value is 1, in which case
  234. * it will return false.
  235. *
  236. * Was often done like: atomic_add_unless(&var, -1, 1)
  237. *
  238. * Return: true if the decrement operation was successful, false otherwise
  239. */
  240. bool refcount_dec_not_one(refcount_t *r)
  241. {
  242. unsigned int new, val = atomic_read(&r->refs);
  243. do {
  244. if (unlikely(val == UINT_MAX))
  245. return true;
  246. if (val == 1)
  247. return false;
  248. new = val - 1;
  249. if (new > val) {
  250. WARN(new > val, "refcount_t: underflow; use-after-free.\n");
  251. return true;
  252. }
  253. } while (!atomic_try_cmpxchg_release(&r->refs, &val, new));
  254. return true;
  255. }
  256. EXPORT_SYMBOL(refcount_dec_not_one);
  257. /**
  258. * refcount_dec_and_mutex_lock - return holding mutex if able to decrement
  259. * refcount to 0
  260. * @r: the refcount
  261. * @lock: the mutex to be locked
  262. *
  263. * Similar to atomic_dec_and_mutex_lock(), it will WARN on underflow and fail
  264. * to decrement when saturated at UINT_MAX.
  265. *
  266. * Provides release memory ordering, such that prior loads and stores are done
  267. * before, and provides a control dependency such that free() must come after.
  268. * See the comment on top.
  269. *
  270. * Return: true and hold mutex if able to decrement refcount to 0, false
  271. * otherwise
  272. */
  273. bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock)
  274. {
  275. if (refcount_dec_not_one(r))
  276. return false;
  277. mutex_lock(lock);
  278. if (!refcount_dec_and_test(r)) {
  279. mutex_unlock(lock);
  280. return false;
  281. }
  282. return true;
  283. }
  284. EXPORT_SYMBOL(refcount_dec_and_mutex_lock);
  285. /**
  286. * refcount_dec_and_lock - return holding spinlock if able to decrement
  287. * refcount to 0
  288. * @r: the refcount
  289. * @lock: the spinlock to be locked
  290. *
  291. * Similar to atomic_dec_and_lock(), it will WARN on underflow and fail to
  292. * decrement when saturated at UINT_MAX.
  293. *
  294. * Provides release memory ordering, such that prior loads and stores are done
  295. * before, and provides a control dependency such that free() must come after.
  296. * See the comment on top.
  297. *
  298. * Return: true and hold spinlock if able to decrement refcount to 0, false
  299. * otherwise
  300. */
  301. bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock)
  302. {
  303. if (refcount_dec_not_one(r))
  304. return false;
  305. spin_lock(lock);
  306. if (!refcount_dec_and_test(r)) {
  307. spin_unlock(lock);
  308. return false;
  309. }
  310. return true;
  311. }
  312. EXPORT_SYMBOL(refcount_dec_and_lock);