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