1 #ifndef _TOOLS_LINUX_REFCOUNT_H
2 #define _TOOLS_LINUX_REFCOUNT_H
5 * Variant of atomic_t specialized for reference counts.
7 * The interface matches the atomic_t interface (to aid in porting) but only
8 * provides the few functions one should use for reference counting.
10 * It differs in that the counter saturates at UINT_MAX and will not move once
11 * there. This avoids wrapping the counter and causing 'spurious'
12 * use-after-free issues.
14 * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
15 * and provide only what is strictly required for refcounts.
17 * The increments are fully relaxed; these will not provide ordering. The
18 * rationale is that whatever is used to obtain the object we're increasing the
19 * reference count on will provide the ordering. For locked data structures,
20 * its the lock acquire, for RCU/lockless data structures its the dependent
23 * Do note that inc_not_zero() provides a control dependency which will order
24 * future stores against the inc, this ensures we'll never modify the object
25 * if we did not in fact acquire a reference.
27 * The decrements will provide release order, such that all the prior loads and
28 * stores will be issued before, it also provides a control dependency, which
29 * will order us against the subsequent free().
31 * The control dependency is against the load of the cmpxchg (ll/sc) that
32 * succeeded. This means the stores aren't fully ordered, but this is fine
33 * because the 1->0 transition indicates no concurrency.
35 * Note that the allocator is responsible for ordering things between free()
40 #include <linux/atomic.h>
41 #include <linux/kernel.h>
44 #define REFCOUNT_WARN(cond, str) (void)(cond)
45 #define __refcount_check
47 #define REFCOUNT_WARN(cond, str) BUG_ON(cond)
48 #define __refcount_check __must_check
51 typedef struct refcount_struct {
55 #define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), }
57 static inline void refcount_set(refcount_t *r, unsigned int n)
59 atomic_set(&r->refs, n);
62 static inline unsigned int refcount_read(const refcount_t *r)
64 return atomic_read(&r->refs);
68 * Similar to atomic_inc_not_zero(), will saturate at UINT_MAX and WARN.
70 * Provides no memory ordering, it is assumed the caller has guaranteed the
71 * object memory to be stable (RCU, etc.). It does provide a control dependency
72 * and thereby orders future stores. See the comment on top.
74 static inline __refcount_check
75 bool refcount_inc_not_zero(refcount_t *r)
77 unsigned int old, new, val = atomic_read(&r->refs);
88 old = atomic_cmpxchg_relaxed(&r->refs, val, new);
95 REFCOUNT_WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
101 * Similar to atomic_inc(), will saturate at UINT_MAX and WARN.
103 * Provides no memory ordering, it is assumed the caller already has a
104 * reference on the object, will WARN when this is not so.
106 static inline void refcount_inc(refcount_t *r)
108 REFCOUNT_WARN(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
112 * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
113 * decrement when saturated at UINT_MAX.
115 * Provides release memory ordering, such that prior loads and stores are done
116 * before, and provides a control dependency such that free() must come after.
117 * See the comment on top.
119 static inline __refcount_check
120 bool refcount_sub_and_test(unsigned int i, refcount_t *r)
122 unsigned int old, new, val = atomic_read(&r->refs);
125 if (unlikely(val == UINT_MAX))
130 REFCOUNT_WARN(new > val, "refcount_t: underflow; use-after-free.\n");
134 old = atomic_cmpxchg_release(&r->refs, val, new);
144 static inline __refcount_check
145 bool refcount_dec_and_test(refcount_t *r)
147 return refcount_sub_and_test(1, r);
151 #endif /* _ATOMIC_LINUX_REFCOUNT_H */