2 * Variant of atomic_t specialized for reference counts.
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.
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.
11 * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
12 * and provide only what is strictly required for refcounts.
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
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.
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().
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.
32 * Note that the allocator is responsible for ordering things between free()
37 #include <linux/refcount.h>
38 #include <linux/bug.h>
40 #ifdef CONFIG_REFCOUNT_FULL
43 * refcount_add_not_zero - add a value to a refcount unless it is 0
44 * @i: the value to add to the refcount
47 * Will saturate at UINT_MAX and WARN.
49 * Provides no memory ordering, it is assumed the caller has guaranteed the
50 * object memory to be stable (RCU, etc.). It does provide a control dependency
51 * and thereby orders future stores. See the comment on top.
53 * Use of this function is not recommended for the normal reference counting
54 * use case in which references are taken and released one at a time. In these
55 * cases, refcount_inc(), or one of its variants, should instead be used to
56 * increment a reference count.
58 * Return: false if the passed refcount is 0, true otherwise
60 bool refcount_add_not_zero(unsigned int i, refcount_t *r)
62 unsigned int new, val = atomic_read(&r->refs);
68 if (unlikely(val == UINT_MAX))
75 } while (!atomic_try_cmpxchg_relaxed(&r->refs, &val, new));
77 WARN_ONCE(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
81 EXPORT_SYMBOL(refcount_add_not_zero);
84 * refcount_add - add a value to a refcount
85 * @i: the value to add to the refcount
88 * Similar to atomic_add(), but will saturate at UINT_MAX and WARN.
90 * Provides no memory ordering, it is assumed the caller has guaranteed the
91 * object memory to be stable (RCU, etc.). It does provide a control dependency
92 * and thereby orders future stores. See the comment on top.
94 * Use of this function is not recommended for the normal reference counting
95 * use case in which references are taken and released one at a time. In these
96 * cases, refcount_inc(), or one of its variants, should instead be used to
97 * increment a reference count.
99 void refcount_add(unsigned int i, refcount_t *r)
101 WARN_ONCE(!refcount_add_not_zero(i, r), "refcount_t: addition on 0; use-after-free.\n");
103 EXPORT_SYMBOL(refcount_add);
106 * refcount_inc_not_zero - increment a refcount unless it is 0
107 * @r: the refcount to increment
109 * Similar to atomic_inc_not_zero(), but will saturate at UINT_MAX and WARN.
111 * Provides no memory ordering, it is assumed the caller has guaranteed the
112 * object memory to be stable (RCU, etc.). It does provide a control dependency
113 * and thereby orders future stores. See the comment on top.
115 * Return: true if the increment was successful, false otherwise
117 bool refcount_inc_not_zero(refcount_t *r)
119 unsigned int new, val = atomic_read(&r->refs);
130 } while (!atomic_try_cmpxchg_relaxed(&r->refs, &val, new));
132 WARN_ONCE(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
136 EXPORT_SYMBOL(refcount_inc_not_zero);
139 * refcount_inc - increment a refcount
140 * @r: the refcount to increment
142 * Similar to atomic_inc(), but will saturate at UINT_MAX and WARN.
144 * Provides no memory ordering, it is assumed the caller already has a
145 * reference on the object.
147 * Will WARN if the refcount is 0, as this represents a possible use-after-free
150 void refcount_inc(refcount_t *r)
152 WARN_ONCE(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
154 EXPORT_SYMBOL(refcount_inc);
157 * refcount_sub_and_test - subtract from a refcount and test if it is 0
158 * @i: amount to subtract from the refcount
161 * Similar to atomic_dec_and_test(), but it will WARN, return false and
162 * ultimately leak on underflow and will fail to decrement when saturated
165 * Provides release memory ordering, such that prior loads and stores are done
166 * before, and provides a control dependency such that free() must come after.
167 * See the comment on top.
169 * Use of this function is not recommended for the normal reference counting
170 * use case in which references are taken and released one at a time. In these
171 * cases, refcount_dec(), or one of its variants, should instead be used to
172 * decrement a reference count.
174 * Return: true if the resulting refcount is 0, false otherwise
176 bool refcount_sub_and_test(unsigned int i, refcount_t *r)
178 unsigned int new, val = atomic_read(&r->refs);
181 if (unlikely(val == UINT_MAX))
186 WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
190 } while (!atomic_try_cmpxchg_release(&r->refs, &val, new));
194 EXPORT_SYMBOL(refcount_sub_and_test);
197 * refcount_dec_and_test - decrement a refcount and test if it is 0
200 * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
201 * decrement when saturated at UINT_MAX.
203 * Provides release memory ordering, such that prior loads and stores are done
204 * before, and provides a control dependency such that free() must come after.
205 * See the comment on top.
207 * Return: true if the resulting refcount is 0, false otherwise
209 bool refcount_dec_and_test(refcount_t *r)
211 return refcount_sub_and_test(1, r);
213 EXPORT_SYMBOL(refcount_dec_and_test);
216 * refcount_dec - decrement a refcount
219 * Similar to atomic_dec(), it will WARN on underflow and fail to decrement
220 * when saturated at UINT_MAX.
222 * Provides release memory ordering, such that prior loads and stores are done
225 void refcount_dec(refcount_t *r)
227 WARN_ONCE(refcount_dec_and_test(r), "refcount_t: decrement hit 0; leaking memory.\n");
229 EXPORT_SYMBOL(refcount_dec);
230 #endif /* CONFIG_REFCOUNT_FULL */
233 * refcount_dec_if_one - decrement a refcount if it is 1
236 * No atomic_t counterpart, it attempts a 1 -> 0 transition and returns the
239 * Like all decrement operations, it provides release memory order and provides
240 * a control dependency.
242 * It can be used like a try-delete operator; this explicit case is provided
243 * and not cmpxchg in generic, because that would allow implementing unsafe
246 * Return: true if the resulting refcount is 0, false otherwise
248 bool refcount_dec_if_one(refcount_t *r)
252 return atomic_try_cmpxchg_release(&r->refs, &val, 0);
254 EXPORT_SYMBOL(refcount_dec_if_one);
257 * refcount_dec_not_one - decrement a refcount if it is not 1
260 * No atomic_t counterpart, it decrements unless the value is 1, in which case
261 * it will return false.
263 * Was often done like: atomic_add_unless(&var, -1, 1)
265 * Return: true if the decrement operation was successful, false otherwise
267 bool refcount_dec_not_one(refcount_t *r)
269 unsigned int new, val = atomic_read(&r->refs);
272 if (unlikely(val == UINT_MAX))
280 WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
284 } while (!atomic_try_cmpxchg_release(&r->refs, &val, new));
288 EXPORT_SYMBOL(refcount_dec_not_one);
291 * refcount_dec_and_mutex_lock - return holding mutex if able to decrement
294 * @lock: the mutex to be locked
296 * Similar to atomic_dec_and_mutex_lock(), it will WARN on underflow and fail
297 * to decrement when saturated at UINT_MAX.
299 * Provides release memory ordering, such that prior loads and stores are done
300 * before, and provides a control dependency such that free() must come after.
301 * See the comment on top.
303 * Return: true and hold mutex if able to decrement refcount to 0, false
306 bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock)
308 if (refcount_dec_not_one(r))
312 if (!refcount_dec_and_test(r)) {
319 EXPORT_SYMBOL(refcount_dec_and_mutex_lock);
322 * refcount_dec_and_lock - return holding spinlock if able to decrement
325 * @lock: the spinlock to be locked
327 * Similar to atomic_dec_and_lock(), it will WARN on underflow and fail to
328 * decrement when saturated at UINT_MAX.
330 * Provides release memory ordering, such that prior loads and stores are done
331 * before, and provides a control dependency such that free() must come after.
332 * See the comment on top.
334 * Return: true and hold spinlock if able to decrement refcount to 0, false
337 bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock)
339 if (refcount_dec_not_one(r))
343 if (!refcount_dec_and_test(r)) {
350 EXPORT_SYMBOL(refcount_dec_and_lock);