xref: /linux-6.15/include/linux/refcount.h (revision 4e0dbe10)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
277e9971cSWill Deacon /*
377e9971cSWill Deacon  * Variant of atomic_t specialized for reference counts.
477e9971cSWill Deacon  *
577e9971cSWill Deacon  * The interface matches the atomic_t interface (to aid in porting) but only
677e9971cSWill Deacon  * provides the few functions one should use for reference counting.
777e9971cSWill Deacon  *
8dcb78649SWill Deacon  * Saturation semantics
9dcb78649SWill Deacon  * ====================
10dcb78649SWill Deacon  *
11dcb78649SWill Deacon  * refcount_t differs from atomic_t in that the counter saturates at
12dcb78649SWill Deacon  * REFCOUNT_SATURATED and will not move once there. This avoids wrapping the
13dcb78649SWill Deacon  * counter and causing 'spurious' use-after-free issues. In order to avoid the
14dcb78649SWill Deacon  * cost associated with introducing cmpxchg() loops into all of the saturating
15dcb78649SWill Deacon  * operations, we temporarily allow the counter to take on an unchecked value
16dcb78649SWill Deacon  * and then explicitly set it to REFCOUNT_SATURATED on detecting that underflow
17dcb78649SWill Deacon  * or overflow has occurred. Although this is racy when multiple threads
18dcb78649SWill Deacon  * access the refcount concurrently, by placing REFCOUNT_SATURATED roughly
19dcb78649SWill Deacon  * equidistant from 0 and INT_MAX we minimise the scope for error:
20dcb78649SWill Deacon  *
21dcb78649SWill Deacon  * 	                           INT_MAX     REFCOUNT_SATURATED   UINT_MAX
22dcb78649SWill Deacon  *   0                          (0x7fff_ffff)    (0xc000_0000)    (0xffff_ffff)
23dcb78649SWill Deacon  *   +--------------------------------+----------------+----------------+
24dcb78649SWill Deacon  *                                     <---------- bad value! ---------->
25dcb78649SWill Deacon  *
26dcb78649SWill Deacon  * (in a signed view of the world, the "bad value" range corresponds to
27dcb78649SWill Deacon  * a negative counter value).
28dcb78649SWill Deacon  *
29dcb78649SWill Deacon  * As an example, consider a refcount_inc() operation that causes the counter
30dcb78649SWill Deacon  * to overflow:
31dcb78649SWill Deacon  *
32dcb78649SWill Deacon  * 	int old = atomic_fetch_add_relaxed(r);
33dcb78649SWill Deacon  *	// old is INT_MAX, refcount now INT_MIN (0x8000_0000)
34dcb78649SWill Deacon  *	if (old < 0)
35dcb78649SWill Deacon  *		atomic_set(r, REFCOUNT_SATURATED);
36dcb78649SWill Deacon  *
37dcb78649SWill Deacon  * If another thread also performs a refcount_inc() operation between the two
38dcb78649SWill Deacon  * atomic operations, then the count will continue to edge closer to 0. If it
39dcb78649SWill Deacon  * reaches a value of 1 before /any/ of the threads reset it to the saturated
40dcb78649SWill Deacon  * value, then a concurrent refcount_dec_and_test() may erroneously free the
41a13f58a0SJann Horn  * underlying object.
42a13f58a0SJann Horn  * Linux limits the maximum number of tasks to PID_MAX_LIMIT, which is currently
43a13f58a0SJann Horn  * 0x400000 (and can't easily be raised in the future beyond FUTEX_TID_MASK).
44a13f58a0SJann Horn  * With the current PID limit, if no batched refcounting operations are used and
45a13f58a0SJann Horn  * the attacker can't repeatedly trigger kernel oopses in the middle of refcount
46a13f58a0SJann Horn  * operations, this makes it impossible for a saturated refcount to leave the
47a13f58a0SJann Horn  * saturation range, even if it is possible for multiple uses of the same
48a13f58a0SJann Horn  * refcount to nest in the context of a single task:
49a13f58a0SJann Horn  *
50a13f58a0SJann Horn  *     (UINT_MAX+1-REFCOUNT_SATURATED) / PID_MAX_LIMIT =
51a13f58a0SJann Horn  *     0x40000000 / 0x400000 = 0x100 = 256
52a13f58a0SJann Horn  *
53a13f58a0SJann Horn  * If hundreds of references are added/removed with a single refcounting
54a13f58a0SJann Horn  * operation, it may potentially be possible to leave the saturation range; but
55a13f58a0SJann Horn  * given the precise timing details involved with the round-robin scheduling of
56a13f58a0SJann Horn  * each thread manipulating the refcount and the need to hit the race multiple
57a13f58a0SJann Horn  * times in succession, there doesn't appear to be a practical avenue of attack
58a13f58a0SJann Horn  * even if using refcount_add() operations with larger increments.
59dcb78649SWill Deacon  *
60dcb78649SWill Deacon  * Memory ordering
61dcb78649SWill Deacon  * ===============
6277e9971cSWill Deacon  *
6377e9971cSWill Deacon  * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
6477e9971cSWill Deacon  * and provide only what is strictly required for refcounts.
6577e9971cSWill Deacon  *
6677e9971cSWill Deacon  * The increments are fully relaxed; these will not provide ordering. The
6777e9971cSWill Deacon  * rationale is that whatever is used to obtain the object we're increasing the
6877e9971cSWill Deacon  * reference count on will provide the ordering. For locked data structures,
6977e9971cSWill Deacon  * its the lock acquire, for RCU/lockless data structures its the dependent
7077e9971cSWill Deacon  * load.
7177e9971cSWill Deacon  *
7277e9971cSWill Deacon  * Do note that inc_not_zero() provides a control dependency which will order
7377e9971cSWill Deacon  * future stores against the inc, this ensures we'll never modify the object
7477e9971cSWill Deacon  * if we did not in fact acquire a reference.
7577e9971cSWill Deacon  *
7677e9971cSWill Deacon  * The decrements will provide release order, such that all the prior loads and
7777e9971cSWill Deacon  * stores will be issued before, it also provides a control dependency, which
7877e9971cSWill Deacon  * will order us against the subsequent free().
7977e9971cSWill Deacon  *
8077e9971cSWill Deacon  * The control dependency is against the load of the cmpxchg (ll/sc) that
8177e9971cSWill Deacon  * succeeded. This means the stores aren't fully ordered, but this is fine
8277e9971cSWill Deacon  * because the 1->0 transition indicates no concurrency.
8377e9971cSWill Deacon  *
8477e9971cSWill Deacon  * Note that the allocator is responsible for ordering things between free()
8577e9971cSWill Deacon  * and alloc().
8677e9971cSWill Deacon  *
8777e9971cSWill Deacon  * The decrements dec_and_test() and sub_and_test() also provide acquire
8877e9971cSWill Deacon  * ordering on success.
8977e9971cSWill Deacon  *
907f8ceea0SSuren Baghdasaryan  * refcount_{add|inc}_not_zero_acquire() and refcount_set_release() provide
917f8ceea0SSuren Baghdasaryan  * acquire and release ordering for cases when the memory occupied by the
927f8ceea0SSuren Baghdasaryan  * object might be reused to store another object. This is important for the
937f8ceea0SSuren Baghdasaryan  * cases where secondary validation is required to detect such reuse, e.g.
947f8ceea0SSuren Baghdasaryan  * SLAB_TYPESAFE_BY_RCU. The secondary validation checks have to happen after
957f8ceea0SSuren Baghdasaryan  * the refcount is taken, hence acquire order is necessary. Similarly, when the
967f8ceea0SSuren Baghdasaryan  * object is initialized, all stores to its attributes should be visible before
977f8ceea0SSuren Baghdasaryan  * the refcount is set, otherwise a stale attribute value might be used by
987f8ceea0SSuren Baghdasaryan  * another task which succeeds in taking a refcount to the new object.
9977e9971cSWill Deacon  */
100f405df5dSPeter Zijlstra 
101fb041bb7SWill Deacon #ifndef _LINUX_REFCOUNT_H
102fb041bb7SWill Deacon #define _LINUX_REFCOUNT_H
103fb041bb7SWill Deacon 
104fb041bb7SWill Deacon #include <linux/atomic.h>
105fb041bb7SWill Deacon #include <linux/bug.h>
106fb041bb7SWill Deacon #include <linux/compiler.h>
107fb041bb7SWill Deacon #include <linux/limits.h>
108f9d6966bSKent Overstreet #include <linux/refcount_types.h>
109fb041bb7SWill Deacon #include <linux/spinlock_types.h>
110fb041bb7SWill Deacon 
111fb041bb7SWill Deacon struct mutex;
112fb041bb7SWill Deacon 
113fb041bb7SWill Deacon #define REFCOUNT_INIT(n)	{ .refs = ATOMIC_INIT(n), }
114fb041bb7SWill Deacon #define REFCOUNT_MAX		INT_MAX
115fb041bb7SWill Deacon #define REFCOUNT_SATURATED	(INT_MIN / 2)
116fb041bb7SWill Deacon 
117fb041bb7SWill Deacon enum refcount_saturation_type {
118fb041bb7SWill Deacon 	REFCOUNT_ADD_NOT_ZERO_OVF,
119fb041bb7SWill Deacon 	REFCOUNT_ADD_OVF,
120fb041bb7SWill Deacon 	REFCOUNT_ADD_UAF,
121fb041bb7SWill Deacon 	REFCOUNT_SUB_UAF,
122fb041bb7SWill Deacon 	REFCOUNT_DEC_LEAK,
123fb041bb7SWill Deacon };
124fb041bb7SWill Deacon 
125fb041bb7SWill Deacon void refcount_warn_saturate(refcount_t *r, enum refcount_saturation_type t);
126fb041bb7SWill Deacon 
127fb041bb7SWill Deacon /**
128fb041bb7SWill Deacon  * refcount_set - set a refcount's value
129fb041bb7SWill Deacon  * @r: the refcount
130fb041bb7SWill Deacon  * @n: value to which the refcount will be set
131fb041bb7SWill Deacon  */
refcount_set(refcount_t * r,int n)132fb041bb7SWill Deacon static inline void refcount_set(refcount_t *r, int n)
133fb041bb7SWill Deacon {
134fb041bb7SWill Deacon 	atomic_set(&r->refs, n);
135fb041bb7SWill Deacon }
136fb041bb7SWill Deacon 
137fb041bb7SWill Deacon /**
1387f8ceea0SSuren Baghdasaryan  * refcount_set_release - set a refcount's value with release ordering
1397f8ceea0SSuren Baghdasaryan  * @r: the refcount
1407f8ceea0SSuren Baghdasaryan  * @n: value to which the refcount will be set
1417f8ceea0SSuren Baghdasaryan  *
1427f8ceea0SSuren Baghdasaryan  * This function should be used when memory occupied by the object might be
1437f8ceea0SSuren Baghdasaryan  * reused to store another object -- consider SLAB_TYPESAFE_BY_RCU.
1447f8ceea0SSuren Baghdasaryan  *
1457f8ceea0SSuren Baghdasaryan  * Provides release memory ordering which will order previous memory operations
1467f8ceea0SSuren Baghdasaryan  * against this store. This ensures all updates to this object are visible
1477f8ceea0SSuren Baghdasaryan  * once the refcount is set and stale values from the object previously
1487f8ceea0SSuren Baghdasaryan  * occupying this memory are overwritten with new ones.
1497f8ceea0SSuren Baghdasaryan  *
1507f8ceea0SSuren Baghdasaryan  * This function should be called only after new object is fully initialized.
1517f8ceea0SSuren Baghdasaryan  * After this call the object should be considered visible to other tasks even
1527f8ceea0SSuren Baghdasaryan  * if it was not yet added into an object collection normally used to discover
1537f8ceea0SSuren Baghdasaryan  * it. This is because other tasks might have discovered the object previously
1547f8ceea0SSuren Baghdasaryan  * occupying the same memory and after memory reuse they can succeed in taking
1557f8ceea0SSuren Baghdasaryan  * refcount to the new object and start using it.
1567f8ceea0SSuren Baghdasaryan  */
refcount_set_release(refcount_t * r,int n)1577f8ceea0SSuren Baghdasaryan static inline void refcount_set_release(refcount_t *r, int n)
1587f8ceea0SSuren Baghdasaryan {
1597f8ceea0SSuren Baghdasaryan 	atomic_set_release(&r->refs, n);
1607f8ceea0SSuren Baghdasaryan }
1617f8ceea0SSuren Baghdasaryan 
1627f8ceea0SSuren Baghdasaryan /**
163fb041bb7SWill Deacon  * refcount_read - get a refcount's value
164fb041bb7SWill Deacon  * @r: the refcount
165fb041bb7SWill Deacon  *
166fb041bb7SWill Deacon  * Return: the refcount's value
167fb041bb7SWill Deacon  */
refcount_read(const refcount_t * r)168fb041bb7SWill Deacon static inline unsigned int refcount_read(const refcount_t *r)
169fb041bb7SWill Deacon {
170fb041bb7SWill Deacon 	return atomic_read(&r->refs);
171fb041bb7SWill Deacon }
172fb041bb7SWill Deacon 
17399db710fSKees Cook static inline __must_check __signed_wrap
__refcount_add_not_zero(int i,refcount_t * r,int * oldp)17499db710fSKees Cook bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp)
17577e9971cSWill Deacon {
176dcb78649SWill Deacon 	int old = refcount_read(r);
177f405df5dSPeter Zijlstra 
17877e9971cSWill Deacon 	do {
179dcb78649SWill Deacon 		if (!old)
180dcb78649SWill Deacon 			break;
181dcb78649SWill Deacon 	} while (!atomic_try_cmpxchg_relaxed(&r->refs, &old, old + i));
182afed7bcfSMark Rutland 
183a435b9a1SPeter Zijlstra 	if (oldp)
184a435b9a1SPeter Zijlstra 		*oldp = old;
185a435b9a1SPeter Zijlstra 
1861eb085d9SWill Deacon 	if (unlikely(old < 0 || old + i < 0))
1871eb085d9SWill Deacon 		refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF);
188afed7bcfSMark Rutland 
189dcb78649SWill Deacon 	return old;
19077e9971cSWill Deacon }
19177e9971cSWill Deacon 
192cf38cc9fSMauro Carvalho Chehab /**
193cf38cc9fSMauro Carvalho Chehab  * refcount_add_not_zero - add a value to a refcount unless it is 0
194cf38cc9fSMauro Carvalho Chehab  * @i: the value to add to the refcount
195cf38cc9fSMauro Carvalho Chehab  * @r: the refcount
196cf38cc9fSMauro Carvalho Chehab  *
197cf38cc9fSMauro Carvalho Chehab  * Will saturate at REFCOUNT_SATURATED and WARN.
198cf38cc9fSMauro Carvalho Chehab  *
199cf38cc9fSMauro Carvalho Chehab  * Provides no memory ordering, it is assumed the caller has guaranteed the
200cf38cc9fSMauro Carvalho Chehab  * object memory to be stable (RCU, etc.). It does provide a control dependency
201cf38cc9fSMauro Carvalho Chehab  * and thereby orders future stores. See the comment on top.
202cf38cc9fSMauro Carvalho Chehab  *
203cf38cc9fSMauro Carvalho Chehab  * Use of this function is not recommended for the normal reference counting
204cf38cc9fSMauro Carvalho Chehab  * use case in which references are taken and released one at a time.  In these
205cf38cc9fSMauro Carvalho Chehab  * cases, refcount_inc(), or one of its variants, should instead be used to
206cf38cc9fSMauro Carvalho Chehab  * increment a reference count.
207cf38cc9fSMauro Carvalho Chehab  *
208cf38cc9fSMauro Carvalho Chehab  * Return: false if the passed refcount is 0, true otherwise
209cf38cc9fSMauro Carvalho Chehab  */
refcount_add_not_zero(int i,refcount_t * r)210a435b9a1SPeter Zijlstra static inline __must_check bool refcount_add_not_zero(int i, refcount_t *r)
211a435b9a1SPeter Zijlstra {
212a435b9a1SPeter Zijlstra 	return __refcount_add_not_zero(i, r, NULL);
213a435b9a1SPeter Zijlstra }
214a435b9a1SPeter Zijlstra 
2157f8ceea0SSuren Baghdasaryan static inline __must_check __signed_wrap
__refcount_add_not_zero_limited_acquire(int i,refcount_t * r,int * oldp,int limit)216*4e0dbe10SSuren Baghdasaryan bool __refcount_add_not_zero_limited_acquire(int i, refcount_t *r, int *oldp,
217*4e0dbe10SSuren Baghdasaryan 					     int limit)
2187f8ceea0SSuren Baghdasaryan {
2197f8ceea0SSuren Baghdasaryan 	int old = refcount_read(r);
2207f8ceea0SSuren Baghdasaryan 
2217f8ceea0SSuren Baghdasaryan 	do {
2227f8ceea0SSuren Baghdasaryan 		if (!old)
2237f8ceea0SSuren Baghdasaryan 			break;
224*4e0dbe10SSuren Baghdasaryan 
225*4e0dbe10SSuren Baghdasaryan 		if (i > limit - old) {
226*4e0dbe10SSuren Baghdasaryan 			if (oldp)
227*4e0dbe10SSuren Baghdasaryan 				*oldp = old;
228*4e0dbe10SSuren Baghdasaryan 			return false;
229*4e0dbe10SSuren Baghdasaryan 		}
2307f8ceea0SSuren Baghdasaryan 	} while (!atomic_try_cmpxchg_acquire(&r->refs, &old, old + i));
2317f8ceea0SSuren Baghdasaryan 
2327f8ceea0SSuren Baghdasaryan 	if (oldp)
2337f8ceea0SSuren Baghdasaryan 		*oldp = old;
2347f8ceea0SSuren Baghdasaryan 
2357f8ceea0SSuren Baghdasaryan 	if (unlikely(old < 0 || old + i < 0))
2367f8ceea0SSuren Baghdasaryan 		refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF);
2377f8ceea0SSuren Baghdasaryan 
2387f8ceea0SSuren Baghdasaryan 	return old;
2397f8ceea0SSuren Baghdasaryan }
2407f8ceea0SSuren Baghdasaryan 
241*4e0dbe10SSuren Baghdasaryan static inline __must_check bool
__refcount_inc_not_zero_limited_acquire(refcount_t * r,int * oldp,int limit)242*4e0dbe10SSuren Baghdasaryan __refcount_inc_not_zero_limited_acquire(refcount_t *r, int *oldp, int limit)
243*4e0dbe10SSuren Baghdasaryan {
244*4e0dbe10SSuren Baghdasaryan 	return __refcount_add_not_zero_limited_acquire(1, r, oldp, limit);
245*4e0dbe10SSuren Baghdasaryan }
246*4e0dbe10SSuren Baghdasaryan 
247*4e0dbe10SSuren Baghdasaryan static inline __must_check __signed_wrap
__refcount_add_not_zero_acquire(int i,refcount_t * r,int * oldp)248*4e0dbe10SSuren Baghdasaryan bool __refcount_add_not_zero_acquire(int i, refcount_t *r, int *oldp)
249*4e0dbe10SSuren Baghdasaryan {
250*4e0dbe10SSuren Baghdasaryan 	return __refcount_add_not_zero_limited_acquire(i, r, oldp, INT_MAX);
251*4e0dbe10SSuren Baghdasaryan }
252*4e0dbe10SSuren Baghdasaryan 
2537f8ceea0SSuren Baghdasaryan /**
2547f8ceea0SSuren Baghdasaryan  * refcount_add_not_zero_acquire - add a value to a refcount with acquire ordering unless it is 0
2557f8ceea0SSuren Baghdasaryan  *
2567f8ceea0SSuren Baghdasaryan  * @i: the value to add to the refcount
2577f8ceea0SSuren Baghdasaryan  * @r: the refcount
2587f8ceea0SSuren Baghdasaryan  *
2597f8ceea0SSuren Baghdasaryan  * Will saturate at REFCOUNT_SATURATED and WARN.
2607f8ceea0SSuren Baghdasaryan  *
2617f8ceea0SSuren Baghdasaryan  * This function should be used when memory occupied by the object might be
2627f8ceea0SSuren Baghdasaryan  * reused to store another object -- consider SLAB_TYPESAFE_BY_RCU.
2637f8ceea0SSuren Baghdasaryan  *
2647f8ceea0SSuren Baghdasaryan  * Provides acquire memory ordering on success, it is assumed the caller has
2657f8ceea0SSuren Baghdasaryan  * guaranteed the object memory to be stable (RCU, etc.). It does provide a
2667f8ceea0SSuren Baghdasaryan  * control dependency and thereby orders future stores. See the comment on top.
2677f8ceea0SSuren Baghdasaryan  *
2687f8ceea0SSuren Baghdasaryan  * Use of this function is not recommended for the normal reference counting
2697f8ceea0SSuren Baghdasaryan  * use case in which references are taken and released one at a time.  In these
2707f8ceea0SSuren Baghdasaryan  * cases, refcount_inc_not_zero_acquire() should instead be used to increment a
2717f8ceea0SSuren Baghdasaryan  * reference count.
2727f8ceea0SSuren Baghdasaryan  *
2737f8ceea0SSuren Baghdasaryan  * Return: false if the passed refcount is 0, true otherwise
2747f8ceea0SSuren Baghdasaryan  */
refcount_add_not_zero_acquire(int i,refcount_t * r)2757f8ceea0SSuren Baghdasaryan static inline __must_check bool refcount_add_not_zero_acquire(int i, refcount_t *r)
2767f8ceea0SSuren Baghdasaryan {
2777f8ceea0SSuren Baghdasaryan 	return __refcount_add_not_zero_acquire(i, r, NULL);
2787f8ceea0SSuren Baghdasaryan }
2797f8ceea0SSuren Baghdasaryan 
28099db710fSKees Cook static inline __signed_wrap
__refcount_add(int i,refcount_t * r,int * oldp)28199db710fSKees Cook void __refcount_add(int i, refcount_t *r, int *oldp)
282cf38cc9fSMauro Carvalho Chehab {
283cf38cc9fSMauro Carvalho Chehab 	int old = atomic_fetch_add_relaxed(i, &r->refs);
284cf38cc9fSMauro Carvalho Chehab 
285cf38cc9fSMauro Carvalho Chehab 	if (oldp)
286cf38cc9fSMauro Carvalho Chehab 		*oldp = old;
287cf38cc9fSMauro Carvalho Chehab 
288cf38cc9fSMauro Carvalho Chehab 	if (unlikely(!old))
289cf38cc9fSMauro Carvalho Chehab 		refcount_warn_saturate(r, REFCOUNT_ADD_UAF);
290cf38cc9fSMauro Carvalho Chehab 	else if (unlikely(old < 0 || old + i < 0))
291cf38cc9fSMauro Carvalho Chehab 		refcount_warn_saturate(r, REFCOUNT_ADD_OVF);
292cf38cc9fSMauro Carvalho Chehab }
293cf38cc9fSMauro Carvalho Chehab 
29477e9971cSWill Deacon /**
29577e9971cSWill Deacon  * refcount_add - add a value to a refcount
29677e9971cSWill Deacon  * @i: the value to add to the refcount
29777e9971cSWill Deacon  * @r: the refcount
29877e9971cSWill Deacon  *
29977e9971cSWill Deacon  * Similar to atomic_add(), but will saturate at REFCOUNT_SATURATED and WARN.
30077e9971cSWill Deacon  *
30177e9971cSWill Deacon  * Provides no memory ordering, it is assumed the caller has guaranteed the
30277e9971cSWill Deacon  * object memory to be stable (RCU, etc.). It does provide a control dependency
30377e9971cSWill Deacon  * and thereby orders future stores. See the comment on top.
30477e9971cSWill Deacon  *
30577e9971cSWill Deacon  * Use of this function is not recommended for the normal reference counting
30677e9971cSWill Deacon  * use case in which references are taken and released one at a time.  In these
30777e9971cSWill Deacon  * cases, refcount_inc(), or one of its variants, should instead be used to
30877e9971cSWill Deacon  * increment a reference count.
30977e9971cSWill Deacon  */
refcount_add(int i,refcount_t * r)310a435b9a1SPeter Zijlstra static inline void refcount_add(int i, refcount_t *r)
311a435b9a1SPeter Zijlstra {
312a435b9a1SPeter Zijlstra 	__refcount_add(i, r, NULL);
313a435b9a1SPeter Zijlstra }
314a435b9a1SPeter Zijlstra 
__refcount_inc_not_zero(refcount_t * r,int * oldp)315cf38cc9fSMauro Carvalho Chehab static inline __must_check bool __refcount_inc_not_zero(refcount_t *r, int *oldp)
316cf38cc9fSMauro Carvalho Chehab {
317cf38cc9fSMauro Carvalho Chehab 	return __refcount_add_not_zero(1, r, oldp);
318cf38cc9fSMauro Carvalho Chehab }
319cf38cc9fSMauro Carvalho Chehab 
32077e9971cSWill Deacon /**
32177e9971cSWill Deacon  * refcount_inc_not_zero - increment a refcount unless it is 0
32277e9971cSWill Deacon  * @r: the refcount to increment
32377e9971cSWill Deacon  *
32477e9971cSWill Deacon  * Similar to atomic_inc_not_zero(), but will saturate at REFCOUNT_SATURATED
32577e9971cSWill Deacon  * and WARN.
32677e9971cSWill Deacon  *
32777e9971cSWill Deacon  * Provides no memory ordering, it is assumed the caller has guaranteed the
32877e9971cSWill Deacon  * object memory to be stable (RCU, etc.). It does provide a control dependency
32977e9971cSWill Deacon  * and thereby orders future stores. See the comment on top.
33077e9971cSWill Deacon  *
33177e9971cSWill Deacon  * Return: true if the increment was successful, false otherwise
33277e9971cSWill Deacon  */
refcount_inc_not_zero(refcount_t * r)33377e9971cSWill Deacon static inline __must_check bool refcount_inc_not_zero(refcount_t *r)
33477e9971cSWill Deacon {
335a435b9a1SPeter Zijlstra 	return __refcount_inc_not_zero(r, NULL);
33677e9971cSWill Deacon }
33777e9971cSWill Deacon 
__refcount_inc_not_zero_acquire(refcount_t * r,int * oldp)3387f8ceea0SSuren Baghdasaryan static inline __must_check bool __refcount_inc_not_zero_acquire(refcount_t *r, int *oldp)
3397f8ceea0SSuren Baghdasaryan {
3407f8ceea0SSuren Baghdasaryan 	return __refcount_add_not_zero_acquire(1, r, oldp);
3417f8ceea0SSuren Baghdasaryan }
3427f8ceea0SSuren Baghdasaryan 
3437f8ceea0SSuren Baghdasaryan /**
3447f8ceea0SSuren Baghdasaryan  * refcount_inc_not_zero_acquire - increment a refcount with acquire ordering unless it is 0
3457f8ceea0SSuren Baghdasaryan  * @r: the refcount to increment
3467f8ceea0SSuren Baghdasaryan  *
3477f8ceea0SSuren Baghdasaryan  * Similar to refcount_inc_not_zero(), but provides acquire memory ordering on
3487f8ceea0SSuren Baghdasaryan  * success.
3497f8ceea0SSuren Baghdasaryan  *
3507f8ceea0SSuren Baghdasaryan  * This function should be used when memory occupied by the object might be
3517f8ceea0SSuren Baghdasaryan  * reused to store another object -- consider SLAB_TYPESAFE_BY_RCU.
3527f8ceea0SSuren Baghdasaryan  *
3537f8ceea0SSuren Baghdasaryan  * Provides acquire memory ordering on success, it is assumed the caller has
3547f8ceea0SSuren Baghdasaryan  * guaranteed the object memory to be stable (RCU, etc.). It does provide a
3557f8ceea0SSuren Baghdasaryan  * control dependency and thereby orders future stores. See the comment on top.
3567f8ceea0SSuren Baghdasaryan  *
3577f8ceea0SSuren Baghdasaryan  * Return: true if the increment was successful, false otherwise
3587f8ceea0SSuren Baghdasaryan  */
refcount_inc_not_zero_acquire(refcount_t * r)3597f8ceea0SSuren Baghdasaryan static inline __must_check bool refcount_inc_not_zero_acquire(refcount_t *r)
3607f8ceea0SSuren Baghdasaryan {
3617f8ceea0SSuren Baghdasaryan 	return __refcount_inc_not_zero_acquire(r, NULL);
3627f8ceea0SSuren Baghdasaryan }
3637f8ceea0SSuren Baghdasaryan 
__refcount_inc(refcount_t * r,int * oldp)364cf38cc9fSMauro Carvalho Chehab static inline void __refcount_inc(refcount_t *r, int *oldp)
365cf38cc9fSMauro Carvalho Chehab {
366cf38cc9fSMauro Carvalho Chehab 	__refcount_add(1, r, oldp);
367cf38cc9fSMauro Carvalho Chehab }
368cf38cc9fSMauro Carvalho Chehab 
36977e9971cSWill Deacon /**
37077e9971cSWill Deacon  * refcount_inc - increment a refcount
37177e9971cSWill Deacon  * @r: the refcount to increment
37277e9971cSWill Deacon  *
37377e9971cSWill Deacon  * Similar to atomic_inc(), but will saturate at REFCOUNT_SATURATED and WARN.
37477e9971cSWill Deacon  *
37577e9971cSWill Deacon  * Provides no memory ordering, it is assumed the caller already has a
37677e9971cSWill Deacon  * reference on the object.
37777e9971cSWill Deacon  *
37877e9971cSWill Deacon  * Will WARN if the refcount is 0, as this represents a possible use-after-free
37977e9971cSWill Deacon  * condition.
38077e9971cSWill Deacon  */
refcount_inc(refcount_t * r)38177e9971cSWill Deacon static inline void refcount_inc(refcount_t *r)
38277e9971cSWill Deacon {
383a435b9a1SPeter Zijlstra 	__refcount_inc(r, NULL);
38477e9971cSWill Deacon }
38577e9971cSWill Deacon 
38699db710fSKees Cook static inline __must_check __signed_wrap
__refcount_sub_and_test(int i,refcount_t * r,int * oldp)38799db710fSKees Cook bool __refcount_sub_and_test(int i, refcount_t *r, int *oldp)
388cf38cc9fSMauro Carvalho Chehab {
389cf38cc9fSMauro Carvalho Chehab 	int old = atomic_fetch_sub_release(i, &r->refs);
390cf38cc9fSMauro Carvalho Chehab 
391cf38cc9fSMauro Carvalho Chehab 	if (oldp)
392cf38cc9fSMauro Carvalho Chehab 		*oldp = old;
393cf38cc9fSMauro Carvalho Chehab 
394f91f7ac9SPetr Pavlu 	if (old > 0 && old == i) {
395cf38cc9fSMauro Carvalho Chehab 		smp_acquire__after_ctrl_dep();
396cf38cc9fSMauro Carvalho Chehab 		return true;
397cf38cc9fSMauro Carvalho Chehab 	}
398cf38cc9fSMauro Carvalho Chehab 
399f91f7ac9SPetr Pavlu 	if (unlikely(old <= 0 || old - i < 0))
400cf38cc9fSMauro Carvalho Chehab 		refcount_warn_saturate(r, REFCOUNT_SUB_UAF);
401cf38cc9fSMauro Carvalho Chehab 
402cf38cc9fSMauro Carvalho Chehab 	return false;
403cf38cc9fSMauro Carvalho Chehab }
404cf38cc9fSMauro Carvalho Chehab 
40577e9971cSWill Deacon /**
40677e9971cSWill Deacon  * refcount_sub_and_test - subtract from a refcount and test if it is 0
40777e9971cSWill Deacon  * @i: amount to subtract from the refcount
40877e9971cSWill Deacon  * @r: the refcount
40977e9971cSWill Deacon  *
41077e9971cSWill Deacon  * Similar to atomic_dec_and_test(), but it will WARN, return false and
41177e9971cSWill Deacon  * ultimately leak on underflow and will fail to decrement when saturated
41277e9971cSWill Deacon  * at REFCOUNT_SATURATED.
41377e9971cSWill Deacon  *
41477e9971cSWill Deacon  * Provides release memory ordering, such that prior loads and stores are done
41577e9971cSWill Deacon  * before, and provides an acquire ordering on success such that free()
41677e9971cSWill Deacon  * must come after.
41777e9971cSWill Deacon  *
41877e9971cSWill Deacon  * Use of this function is not recommended for the normal reference counting
41977e9971cSWill Deacon  * use case in which references are taken and released one at a time.  In these
42077e9971cSWill Deacon  * cases, refcount_dec(), or one of its variants, should instead be used to
42177e9971cSWill Deacon  * decrement a reference count.
42277e9971cSWill Deacon  *
42377e9971cSWill Deacon  * Return: true if the resulting refcount is 0, false otherwise
42477e9971cSWill Deacon  */
refcount_sub_and_test(int i,refcount_t * r)425a435b9a1SPeter Zijlstra static inline __must_check bool refcount_sub_and_test(int i, refcount_t *r)
426a435b9a1SPeter Zijlstra {
427a435b9a1SPeter Zijlstra 	return __refcount_sub_and_test(i, r, NULL);
428a435b9a1SPeter Zijlstra }
429a435b9a1SPeter Zijlstra 
__refcount_dec_and_test(refcount_t * r,int * oldp)430cf38cc9fSMauro Carvalho Chehab static inline __must_check bool __refcount_dec_and_test(refcount_t *r, int *oldp)
431cf38cc9fSMauro Carvalho Chehab {
432cf38cc9fSMauro Carvalho Chehab 	return __refcount_sub_and_test(1, r, oldp);
433cf38cc9fSMauro Carvalho Chehab }
434cf38cc9fSMauro Carvalho Chehab 
43577e9971cSWill Deacon /**
43677e9971cSWill Deacon  * refcount_dec_and_test - decrement a refcount and test if it is 0
43777e9971cSWill Deacon  * @r: the refcount
43877e9971cSWill Deacon  *
43977e9971cSWill Deacon  * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
44077e9971cSWill Deacon  * decrement when saturated at REFCOUNT_SATURATED.
44177e9971cSWill Deacon  *
44277e9971cSWill Deacon  * Provides release memory ordering, such that prior loads and stores are done
44377e9971cSWill Deacon  * before, and provides an acquire ordering on success such that free()
44477e9971cSWill Deacon  * must come after.
44577e9971cSWill Deacon  *
44677e9971cSWill Deacon  * Return: true if the resulting refcount is 0, false otherwise
44777e9971cSWill Deacon  */
refcount_dec_and_test(refcount_t * r)44877e9971cSWill Deacon static inline __must_check bool refcount_dec_and_test(refcount_t *r)
44977e9971cSWill Deacon {
450a435b9a1SPeter Zijlstra 	return __refcount_dec_and_test(r, NULL);
45177e9971cSWill Deacon }
45277e9971cSWill Deacon 
__refcount_dec(refcount_t * r,int * oldp)453cf38cc9fSMauro Carvalho Chehab static inline void __refcount_dec(refcount_t *r, int *oldp)
454cf38cc9fSMauro Carvalho Chehab {
455cf38cc9fSMauro Carvalho Chehab 	int old = atomic_fetch_sub_release(1, &r->refs);
456cf38cc9fSMauro Carvalho Chehab 
457cf38cc9fSMauro Carvalho Chehab 	if (oldp)
458cf38cc9fSMauro Carvalho Chehab 		*oldp = old;
459cf38cc9fSMauro Carvalho Chehab 
460cf38cc9fSMauro Carvalho Chehab 	if (unlikely(old <= 1))
461cf38cc9fSMauro Carvalho Chehab 		refcount_warn_saturate(r, REFCOUNT_DEC_LEAK);
462cf38cc9fSMauro Carvalho Chehab }
463cf38cc9fSMauro Carvalho Chehab 
46477e9971cSWill Deacon /**
46577e9971cSWill Deacon  * refcount_dec - decrement a refcount
46677e9971cSWill Deacon  * @r: the refcount
46777e9971cSWill Deacon  *
46877e9971cSWill Deacon  * Similar to atomic_dec(), it will WARN on underflow and fail to decrement
46977e9971cSWill Deacon  * when saturated at REFCOUNT_SATURATED.
47077e9971cSWill Deacon  *
47177e9971cSWill Deacon  * Provides release memory ordering, such that prior loads and stores are done
47277e9971cSWill Deacon  * before.
47377e9971cSWill Deacon  */
refcount_dec(refcount_t * r)47477e9971cSWill Deacon static inline void refcount_dec(refcount_t *r)
47577e9971cSWill Deacon {
476a435b9a1SPeter Zijlstra 	__refcount_dec(r, NULL);
477dcb78649SWill Deacon }
478f405df5dSPeter Zijlstra 
47929dee3c0SPeter Zijlstra extern __must_check bool refcount_dec_if_one(refcount_t *r);
48029dee3c0SPeter Zijlstra extern __must_check bool refcount_dec_not_one(refcount_t *r);
4814a557a5dSLinus Torvalds extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock) __cond_acquires(lock);
4824a557a5dSLinus Torvalds extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock) __cond_acquires(lock);
4837ea959c4SAnna-Maria Gleixner extern __must_check bool refcount_dec_and_lock_irqsave(refcount_t *r,
4847ea959c4SAnna-Maria Gleixner 						       spinlock_t *lock,
4854a557a5dSLinus Torvalds 						       unsigned long *flags) __cond_acquires(lock);
486f405df5dSPeter Zijlstra #endif /* _LINUX_REFCOUNT_H */
487