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 bool refcount_add_not_zero(unsigned int i, refcount_t *r)
42 unsigned int old, new, val = atomic_read(&r->refs);
48 if (unlikely(val == UINT_MAX))
54 old = atomic_cmpxchg_relaxed(&r->refs, val, new);
61 WARN_ONCE(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
65 EXPORT_SYMBOL_GPL(refcount_add_not_zero);
67 void refcount_add(unsigned int i, refcount_t *r)
69 WARN_ONCE(!refcount_add_not_zero(i, r), "refcount_t: addition on 0; use-after-free.\n");
71 EXPORT_SYMBOL_GPL(refcount_add);
74 * Similar to atomic_inc_not_zero(), will saturate at UINT_MAX and WARN.
76 * Provides no memory ordering, it is assumed the caller has guaranteed the
77 * object memory to be stable (RCU, etc.). It does provide a control dependency
78 * and thereby orders future stores. See the comment on top.
80 bool refcount_inc_not_zero(refcount_t *r)
82 unsigned int old, new, val = atomic_read(&r->refs);
93 old = atomic_cmpxchg_relaxed(&r->refs, val, new);
100 WARN_ONCE(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
104 EXPORT_SYMBOL_GPL(refcount_inc_not_zero);
107 * Similar to atomic_inc(), will saturate at UINT_MAX and WARN.
109 * Provides no memory ordering, it is assumed the caller already has a
110 * reference on the object, will WARN when this is not so.
112 void refcount_inc(refcount_t *r)
114 WARN_ONCE(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
116 EXPORT_SYMBOL_GPL(refcount_inc);
118 bool refcount_sub_and_test(unsigned int i, refcount_t *r)
120 unsigned int old, new, val = atomic_read(&r->refs);
123 if (unlikely(val == UINT_MAX))
128 WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
132 old = atomic_cmpxchg_release(&r->refs, val, new);
141 EXPORT_SYMBOL_GPL(refcount_sub_and_test);
144 * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
145 * decrement when saturated at UINT_MAX.
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.
151 bool refcount_dec_and_test(refcount_t *r)
153 return refcount_sub_and_test(1, r);
155 EXPORT_SYMBOL_GPL(refcount_dec_and_test);
158 * Similar to atomic_dec(), it will WARN on underflow and fail to decrement
159 * when saturated at UINT_MAX.
161 * Provides release memory ordering, such that prior loads and stores are done
165 void refcount_dec(refcount_t *r)
167 WARN_ONCE(refcount_dec_and_test(r), "refcount_t: decrement hit 0; leaking memory.\n");
169 EXPORT_SYMBOL_GPL(refcount_dec);
172 * No atomic_t counterpart, it attempts a 1 -> 0 transition and returns the
175 * Like all decrement operations, it provides release memory order and provides
176 * a control dependency.
178 * It can be used like a try-delete operator; this explicit case is provided
179 * and not cmpxchg in generic, because that would allow implementing unsafe
182 bool refcount_dec_if_one(refcount_t *r)
184 return atomic_cmpxchg_release(&r->refs, 1, 0) == 1;
186 EXPORT_SYMBOL_GPL(refcount_dec_if_one);
189 * No atomic_t counterpart, it decrements unless the value is 1, in which case
190 * it will return false.
192 * Was often done like: atomic_add_unless(&var, -1, 1)
194 bool refcount_dec_not_one(refcount_t *r)
196 unsigned int old, new, val = atomic_read(&r->refs);
199 if (unlikely(val == UINT_MAX))
207 WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
211 old = atomic_cmpxchg_release(&r->refs, val, new);
220 EXPORT_SYMBOL_GPL(refcount_dec_not_one);
223 * Similar to atomic_dec_and_mutex_lock(), it will WARN on underflow and fail
224 * to decrement when saturated at UINT_MAX.
226 * Provides release memory ordering, such that prior loads and stores are done
227 * before, and provides a control dependency such that free() must come after.
228 * See the comment on top.
230 bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock)
232 if (refcount_dec_not_one(r))
236 if (!refcount_dec_and_test(r)) {
243 EXPORT_SYMBOL_GPL(refcount_dec_and_mutex_lock);
246 * Similar to atomic_dec_and_lock(), it will WARN on underflow and fail to
247 * decrement when saturated at UINT_MAX.
249 * Provides release memory ordering, such that prior loads and stores are done
250 * before, and provides a control dependency such that free() must come after.
251 * See the comment on top.
253 bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock)
255 if (refcount_dec_not_one(r))
259 if (!refcount_dec_and_test(r)) {
266 EXPORT_SYMBOL_GPL(refcount_dec_and_lock);