refcount.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Variant of atomic_t specialized for reference counts.
  4. *
  5. * The interface matches the atomic_t interface (to aid in porting) but only
  6. * provides the few functions one should use for reference counting.
  7. *
  8. * It differs in that the counter saturates at UINT_MAX and will not move once
  9. * there. This avoids wrapping the counter and causing 'spurious'
  10. * use-after-free issues.
  11. *
  12. * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
  13. * and provide only what is strictly required for refcounts.
  14. *
  15. * The increments are fully relaxed; these will not provide ordering. The
  16. * rationale is that whatever is used to obtain the object we're increasing the
  17. * reference count on will provide the ordering. For locked data structures,
  18. * its the lock acquire, for RCU/lockless data structures its the dependent
  19. * load.
  20. *
  21. * Do note that inc_not_zero() provides a control dependency which will order
  22. * future stores against the inc, this ensures we'll never modify the object
  23. * if we did not in fact acquire a reference.
  24. *
  25. * The decrements will provide release order, such that all the prior loads and
  26. * stores will be issued before, it also provides a control dependency, which
  27. * will order us against the subsequent free().
  28. *
  29. * The control dependency is against the load of the cmpxchg (ll/sc) that
  30. * succeeded. This means the stores aren't fully ordered, but this is fine
  31. * because the 1->0 transition indicates no concurrency.
  32. *
  33. * Note that the allocator is responsible for ordering things between free()
  34. * and alloc().
  35. *
  36. */
  37. #include <linux/mutex.h>
  38. #include <linux/refcount.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/bug.h>
  41. /**
  42. * refcount_add_not_zero_checked - add a value to a refcount unless it is 0
  43. * @i: the value to add to the refcount
  44. * @r: the refcount
  45. *
  46. * Will saturate at UINT_MAX and WARN.
  47. *
  48. * Provides no memory ordering, it is assumed the caller has guaranteed the
  49. * object memory to be stable (RCU, etc.). It does provide a control dependency
  50. * and thereby orders future stores. See the comment on top.
  51. *
  52. * Use of this function is not recommended for the normal reference counting
  53. * use case in which references are taken and released one at a time. In these
  54. * cases, refcount_inc(), or one of its variants, should instead be used to
  55. * increment a reference count.
  56. *
  57. * Return: false if the passed refcount is 0, true otherwise
  58. */
  59. bool refcount_add_not_zero_checked(unsigned int i, refcount_t *r)
  60. {
  61. unsigned int new, val = atomic_read(&r->refs);
  62. do {
  63. if (!val)
  64. return false;
  65. if (unlikely(val == UINT_MAX))
  66. return true;
  67. new = val + i;
  68. if (new < val)
  69. new = UINT_MAX;
  70. } while (!atomic_try_cmpxchg_relaxed(&r->refs, &val, new));
  71. WARN_ONCE(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
  72. return true;
  73. }
  74. EXPORT_SYMBOL(refcount_add_not_zero_checked);
  75. /**
  76. * refcount_add_checked - add a value to a refcount
  77. * @i: the value to add to the refcount
  78. * @r: the refcount
  79. *
  80. * Similar to atomic_add(), but will saturate at UINT_MAX and WARN.
  81. *
  82. * Provides no memory ordering, it is assumed the caller has guaranteed the
  83. * object memory to be stable (RCU, etc.). It does provide a control dependency
  84. * and thereby orders future stores. See the comment on top.
  85. *
  86. * Use of this function is not recommended for the normal reference counting
  87. * use case in which references are taken and released one at a time. In these
  88. * cases, refcount_inc(), or one of its variants, should instead be used to
  89. * increment a reference count.
  90. */
  91. void refcount_add_checked(unsigned int i, refcount_t *r)
  92. {
  93. WARN_ONCE(!refcount_add_not_zero_checked(i, r), "refcount_t: addition on 0; use-after-free.\n");
  94. }
  95. EXPORT_SYMBOL(refcount_add_checked);
  96. /**
  97. * refcount_inc_not_zero_checked - increment a refcount unless it is 0
  98. * @r: the refcount to increment
  99. *
  100. * Similar to atomic_inc_not_zero(), but will saturate at UINT_MAX and WARN.
  101. *
  102. * Provides no memory ordering, it is assumed the caller has guaranteed the
  103. * object memory to be stable (RCU, etc.). It does provide a control dependency
  104. * and thereby orders future stores. See the comment on top.
  105. *
  106. * Return: true if the increment was successful, false otherwise
  107. */
  108. bool refcount_inc_not_zero_checked(refcount_t *r)
  109. {
  110. unsigned int new, val = atomic_read(&r->refs);
  111. do {
  112. new = val + 1;
  113. if (!val)
  114. return false;
  115. if (unlikely(!new))
  116. return true;
  117. } while (!atomic_try_cmpxchg_relaxed(&r->refs, &val, new));
  118. WARN_ONCE(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
  119. return true;
  120. }
  121. EXPORT_SYMBOL(refcount_inc_not_zero_checked);
  122. /**
  123. * refcount_inc_checked - increment a refcount
  124. * @r: the refcount to increment
  125. *
  126. * Similar to atomic_inc(), but will saturate at UINT_MAX and WARN.
  127. *
  128. * Provides no memory ordering, it is assumed the caller already has a
  129. * reference on the object.
  130. *
  131. * Will WARN if the refcount is 0, as this represents a possible use-after-free
  132. * condition.
  133. */
  134. void refcount_inc_checked(refcount_t *r)
  135. {
  136. WARN_ONCE(!refcount_inc_not_zero_checked(r), "refcount_t: increment on 0; use-after-free.\n");
  137. }
  138. EXPORT_SYMBOL(refcount_inc_checked);
  139. /**
  140. * refcount_sub_and_test_checked - subtract from a refcount and test if it is 0
  141. * @i: amount to subtract from the refcount
  142. * @r: the refcount
  143. *
  144. * Similar to atomic_dec_and_test(), but it will WARN, return false and
  145. * ultimately leak on underflow and will fail to decrement when saturated
  146. * at UINT_MAX.
  147. *
  148. * Provides release memory ordering, such that prior loads and stores are done
  149. * before, and provides a control dependency such that free() must come after.
  150. * See the comment on top.
  151. *
  152. * Use of this function is not recommended for the normal reference counting
  153. * use case in which references are taken and released one at a time. In these
  154. * cases, refcount_dec(), or one of its variants, should instead be used to
  155. * decrement a reference count.
  156. *
  157. * Return: true if the resulting refcount is 0, false otherwise
  158. */
  159. bool refcount_sub_and_test_checked(unsigned int i, refcount_t *r)
  160. {
  161. unsigned int new, val = atomic_read(&r->refs);
  162. do {
  163. if (unlikely(val == UINT_MAX))
  164. return false;
  165. new = val - i;
  166. if (new > val) {
  167. WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
  168. return false;
  169. }
  170. } while (!atomic_try_cmpxchg_release(&r->refs, &val, new));
  171. return !new;
  172. }
  173. EXPORT_SYMBOL(refcount_sub_and_test_checked);
  174. /**
  175. * refcount_dec_and_test_checked - decrement a refcount and test if it is 0
  176. * @r: the refcount
  177. *
  178. * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
  179. * decrement when saturated at UINT_MAX.
  180. *
  181. * Provides release memory ordering, such that prior loads and stores are done
  182. * before, and provides a control dependency such that free() must come after.
  183. * See the comment on top.
  184. *
  185. * Return: true if the resulting refcount is 0, false otherwise
  186. */
  187. bool refcount_dec_and_test_checked(refcount_t *r)
  188. {
  189. return refcount_sub_and_test_checked(1, r);
  190. }
  191. EXPORT_SYMBOL(refcount_dec_and_test_checked);
  192. /**
  193. * refcount_dec_checked - decrement a refcount
  194. * @r: the refcount
  195. *
  196. * Similar to atomic_dec(), it will WARN on underflow and fail to decrement
  197. * when saturated at UINT_MAX.
  198. *
  199. * Provides release memory ordering, such that prior loads and stores are done
  200. * before.
  201. */
  202. void refcount_dec_checked(refcount_t *r)
  203. {
  204. WARN_ONCE(refcount_dec_and_test_checked(r), "refcount_t: decrement hit 0; leaking memory.\n");
  205. }
  206. EXPORT_SYMBOL(refcount_dec_checked);
  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_ONCE(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);
  313. /**
  314. * refcount_dec_and_lock_irqsave - return holding spinlock with disabled
  315. * interrupts if able to decrement refcount to 0
  316. * @r: the refcount
  317. * @lock: the spinlock to be locked
  318. * @flags: saved IRQ-flags if the is acquired
  319. *
  320. * Same as refcount_dec_and_lock() above except that the spinlock is acquired
  321. * with disabled interupts.
  322. *
  323. * Return: true and hold spinlock if able to decrement refcount to 0, false
  324. * otherwise
  325. */
  326. bool refcount_dec_and_lock_irqsave(refcount_t *r, spinlock_t *lock,
  327. unsigned long *flags)
  328. {
  329. if (refcount_dec_not_one(r))
  330. return false;
  331. spin_lock_irqsave(lock, *flags);
  332. if (!refcount_dec_and_test(r)) {
  333. spin_unlock_irqrestore(lock, *flags);
  334. return false;
  335. }
  336. return true;
  337. }
  338. EXPORT_SYMBOL(refcount_dec_and_lock_irqsave);