File Index
Files and directories
Sign in
FILE

refcount.h

File preview with syntax highlighting.

linux-6.19.14/tools/include/linux/refcount.h C
  1/* SPDX-License-Identifier: GPL-2.0 */
  2#ifndef _TOOLS_LINUX_REFCOUNT_H
  3#define _TOOLS_LINUX_REFCOUNT_H
  4
  5/*
  6 * Variant of atomic_t specialized for reference counts.
  7 *
  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.
 10 *
 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.
 14 *
 15 * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
 16 * and provide only what is strictly required for refcounts.
 17 *
 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.
 23 *
 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.
 27 *
 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().
 31 *
 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.
 35 *
 36 * Note that the allocator is responsible for ordering things between free()
 37 * and alloc().
 38 *
 39 */
 40
 41#include <linux/atomic.h>
 42#include <linux/kernel.h>
 43
 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
 51
 52typedef struct refcount_struct {
 53	atomic_t refs;
 54} refcount_t;
 55
 56#define REFCOUNT_INIT(n)	{ .refs = ATOMIC_INIT(n), }
 57
 58static inline void refcount_set(refcount_t *r, unsigned int n)
 59{
 60	atomic_set(&r->refs, n);
 61}
 62
 63static inline void refcount_set_release(refcount_t *r, unsigned int n)
 64{
 65	atomic_set(&r->refs, n);
 66}
 67
 68static inline unsigned int refcount_read(const refcount_t *r)
 69{
 70	return atomic_read(&r->refs);
 71}
 72
 73/*
 74 * Similar to atomic_inc_not_zero(), will saturate at UINT_MAX and WARN.
 75 *
 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.
 79 */
 80static inline __refcount_check
 81bool refcount_inc_not_zero(refcount_t *r)
 82{
 83	unsigned int old, new, val = atomic_read(&r->refs);
 84
 85	for (;;) {
 86		new = val + 1;
 87
 88		if (!val)
 89			return false;
 90
 91		if (unlikely(!new))
 92			return true;
 93
 94		old = atomic_cmpxchg_relaxed(&r->refs, val, new);
 95		if (old == val)
 96			break;
 97
 98		val = old;
 99	}
100
101	REFCOUNT_WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
102
103	return true;
104}
105
106/*
107 * Similar to atomic_inc(), will saturate at UINT_MAX and WARN.
108 *
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.
111 */
112static inline void refcount_inc(refcount_t *r)
113{
114	REFCOUNT_WARN(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
115}
116
117/*
118 * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
119 * decrement when saturated at UINT_MAX.
120 *
121 * Provides release memory ordering, such that prior loads and stores are done
122 * before, and provides a control dependency such that free() must come after.
123 * See the comment on top.
124 */
125static inline __refcount_check
126bool refcount_sub_and_test(unsigned int i, refcount_t *r)
127{
128	unsigned int old, new, val = atomic_read(&r->refs);
129
130	for (;;) {
131		if (unlikely(val == UINT_MAX))
132			return false;
133
134		new = val - i;
135		if (new > val) {
136			REFCOUNT_WARN(new > val, "refcount_t: underflow; use-after-free.\n");
137			return false;
138		}
139
140		old = atomic_cmpxchg_release(&r->refs, val, new);
141		if (old == val)
142			break;
143
144		val = old;
145	}
146
147	return !new;
148}
149
150static inline __refcount_check
151bool refcount_dec_and_test(refcount_t *r)
152{
153	return refcount_sub_and_test(1, r);
154}
155
156
157#endif /* _ATOMIC_LINUX_REFCOUNT_H */
~/ Read-only file preview.