1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _TOOLS_LINUX_REFCOUNT_H
3*4882a593Smuzhiyun #define _TOOLS_LINUX_REFCOUNT_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun * Variant of atomic_t specialized for reference counts.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * The interface matches the atomic_t interface (to aid in porting) but only
9*4882a593Smuzhiyun * provides the few functions one should use for reference counting.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * It differs in that the counter saturates at UINT_MAX and will not move once
12*4882a593Smuzhiyun * there. This avoids wrapping the counter and causing 'spurious'
13*4882a593Smuzhiyun * use-after-free issues.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
16*4882a593Smuzhiyun * and provide only what is strictly required for refcounts.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * The increments are fully relaxed; these will not provide ordering. The
19*4882a593Smuzhiyun * rationale is that whatever is used to obtain the object we're increasing the
20*4882a593Smuzhiyun * reference count on will provide the ordering. For locked data structures,
21*4882a593Smuzhiyun * its the lock acquire, for RCU/lockless data structures its the dependent
22*4882a593Smuzhiyun * load.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Do note that inc_not_zero() provides a control dependency which will order
25*4882a593Smuzhiyun * future stores against the inc, this ensures we'll never modify the object
26*4882a593Smuzhiyun * if we did not in fact acquire a reference.
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * The decrements will provide release order, such that all the prior loads and
29*4882a593Smuzhiyun * stores will be issued before, it also provides a control dependency, which
30*4882a593Smuzhiyun * will order us against the subsequent free().
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * The control dependency is against the load of the cmpxchg (ll/sc) that
33*4882a593Smuzhiyun * succeeded. This means the stores aren't fully ordered, but this is fine
34*4882a593Smuzhiyun * because the 1->0 transition indicates no concurrency.
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * Note that the allocator is responsible for ordering things between free()
37*4882a593Smuzhiyun * and alloc().
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #include <linux/atomic.h>
42*4882a593Smuzhiyun #include <linux/kernel.h>
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #ifdef NDEBUG
45*4882a593Smuzhiyun #define REFCOUNT_WARN(cond, str) (void)(cond)
46*4882a593Smuzhiyun #define __refcount_check
47*4882a593Smuzhiyun #else
48*4882a593Smuzhiyun #define REFCOUNT_WARN(cond, str) BUG_ON(cond)
49*4882a593Smuzhiyun #define __refcount_check __must_check
50*4882a593Smuzhiyun #endif
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun typedef struct refcount_struct {
53*4882a593Smuzhiyun atomic_t refs;
54*4882a593Smuzhiyun } refcount_t;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun #define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), }
57*4882a593Smuzhiyun
refcount_set(refcount_t * r,unsigned int n)58*4882a593Smuzhiyun static inline void refcount_set(refcount_t *r, unsigned int n)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun atomic_set(&r->refs, n);
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
refcount_read(const refcount_t * r)63*4882a593Smuzhiyun static inline unsigned int refcount_read(const refcount_t *r)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun return atomic_read(&r->refs);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /*
69*4882a593Smuzhiyun * Similar to atomic_inc_not_zero(), will saturate at UINT_MAX and WARN.
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * Provides no memory ordering, it is assumed the caller has guaranteed the
72*4882a593Smuzhiyun * object memory to be stable (RCU, etc.). It does provide a control dependency
73*4882a593Smuzhiyun * and thereby orders future stores. See the comment on top.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun static inline __refcount_check
refcount_inc_not_zero(refcount_t * r)76*4882a593Smuzhiyun bool refcount_inc_not_zero(refcount_t *r)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun unsigned int old, new, val = atomic_read(&r->refs);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun for (;;) {
81*4882a593Smuzhiyun new = val + 1;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun if (!val)
84*4882a593Smuzhiyun return false;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun if (unlikely(!new))
87*4882a593Smuzhiyun return true;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun old = atomic_cmpxchg_relaxed(&r->refs, val, new);
90*4882a593Smuzhiyun if (old == val)
91*4882a593Smuzhiyun break;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun val = old;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun REFCOUNT_WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun return true;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun * Similar to atomic_inc(), will saturate at UINT_MAX and WARN.
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * Provides no memory ordering, it is assumed the caller already has a
105*4882a593Smuzhiyun * reference on the object, will WARN when this is not so.
106*4882a593Smuzhiyun */
refcount_inc(refcount_t * r)107*4882a593Smuzhiyun static inline void refcount_inc(refcount_t *r)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun REFCOUNT_WARN(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /*
113*4882a593Smuzhiyun * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
114*4882a593Smuzhiyun * decrement when saturated at UINT_MAX.
115*4882a593Smuzhiyun *
116*4882a593Smuzhiyun * Provides release memory ordering, such that prior loads and stores are done
117*4882a593Smuzhiyun * before, and provides a control dependency such that free() must come after.
118*4882a593Smuzhiyun * See the comment on top.
119*4882a593Smuzhiyun */
120*4882a593Smuzhiyun static inline __refcount_check
refcount_sub_and_test(unsigned int i,refcount_t * r)121*4882a593Smuzhiyun bool refcount_sub_and_test(unsigned int i, refcount_t *r)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun unsigned int old, new, val = atomic_read(&r->refs);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun for (;;) {
126*4882a593Smuzhiyun if (unlikely(val == UINT_MAX))
127*4882a593Smuzhiyun return false;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun new = val - i;
130*4882a593Smuzhiyun if (new > val) {
131*4882a593Smuzhiyun REFCOUNT_WARN(new > val, "refcount_t: underflow; use-after-free.\n");
132*4882a593Smuzhiyun return false;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun old = atomic_cmpxchg_release(&r->refs, val, new);
136*4882a593Smuzhiyun if (old == val)
137*4882a593Smuzhiyun break;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun val = old;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun return !new;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun static inline __refcount_check
refcount_dec_and_test(refcount_t * r)146*4882a593Smuzhiyun bool refcount_dec_and_test(refcount_t *r)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun return refcount_sub_and_test(1, r);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun #endif /* _ATOMIC_LINUX_REFCOUNT_H */
153