xref: /OK3568_Linux_fs/kernel/include/linux/refcount.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Variant of atomic_t specialized for reference counts.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * The interface matches the atomic_t interface (to aid in porting) but only
6*4882a593Smuzhiyun  * provides the few functions one should use for reference counting.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Saturation semantics
9*4882a593Smuzhiyun  * ====================
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * refcount_t differs from atomic_t in that the counter saturates at
12*4882a593Smuzhiyun  * REFCOUNT_SATURATED and will not move once there. This avoids wrapping the
13*4882a593Smuzhiyun  * counter and causing 'spurious' use-after-free issues. In order to avoid the
14*4882a593Smuzhiyun  * cost associated with introducing cmpxchg() loops into all of the saturating
15*4882a593Smuzhiyun  * operations, we temporarily allow the counter to take on an unchecked value
16*4882a593Smuzhiyun  * and then explicitly set it to REFCOUNT_SATURATED on detecting that underflow
17*4882a593Smuzhiyun  * or overflow has occurred. Although this is racy when multiple threads
18*4882a593Smuzhiyun  * access the refcount concurrently, by placing REFCOUNT_SATURATED roughly
19*4882a593Smuzhiyun  * equidistant from 0 and INT_MAX we minimise the scope for error:
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * 	                           INT_MAX     REFCOUNT_SATURATED   UINT_MAX
22*4882a593Smuzhiyun  *   0                          (0x7fff_ffff)    (0xc000_0000)    (0xffff_ffff)
23*4882a593Smuzhiyun  *   +--------------------------------+----------------+----------------+
24*4882a593Smuzhiyun  *                                     <---------- bad value! ---------->
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * (in a signed view of the world, the "bad value" range corresponds to
27*4882a593Smuzhiyun  * a negative counter value).
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * As an example, consider a refcount_inc() operation that causes the counter
30*4882a593Smuzhiyun  * to overflow:
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * 	int old = atomic_fetch_add_relaxed(r);
33*4882a593Smuzhiyun  *	// old is INT_MAX, refcount now INT_MIN (0x8000_0000)
34*4882a593Smuzhiyun  *	if (old < 0)
35*4882a593Smuzhiyun  *		atomic_set(r, REFCOUNT_SATURATED);
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * If another thread also performs a refcount_inc() operation between the two
38*4882a593Smuzhiyun  * atomic operations, then the count will continue to edge closer to 0. If it
39*4882a593Smuzhiyun  * reaches a value of 1 before /any/ of the threads reset it to the saturated
40*4882a593Smuzhiyun  * value, then a concurrent refcount_dec_and_test() may erroneously free the
41*4882a593Smuzhiyun  * underlying object.
42*4882a593Smuzhiyun  * Linux limits the maximum number of tasks to PID_MAX_LIMIT, which is currently
43*4882a593Smuzhiyun  * 0x400000 (and can't easily be raised in the future beyond FUTEX_TID_MASK).
44*4882a593Smuzhiyun  * With the current PID limit, if no batched refcounting operations are used and
45*4882a593Smuzhiyun  * the attacker can't repeatedly trigger kernel oopses in the middle of refcount
46*4882a593Smuzhiyun  * operations, this makes it impossible for a saturated refcount to leave the
47*4882a593Smuzhiyun  * saturation range, even if it is possible for multiple uses of the same
48*4882a593Smuzhiyun  * refcount to nest in the context of a single task:
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  *     (UINT_MAX+1-REFCOUNT_SATURATED) / PID_MAX_LIMIT =
51*4882a593Smuzhiyun  *     0x40000000 / 0x400000 = 0x100 = 256
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * If hundreds of references are added/removed with a single refcounting
54*4882a593Smuzhiyun  * operation, it may potentially be possible to leave the saturation range; but
55*4882a593Smuzhiyun  * given the precise timing details involved with the round-robin scheduling of
56*4882a593Smuzhiyun  * each thread manipulating the refcount and the need to hit the race multiple
57*4882a593Smuzhiyun  * times in succession, there doesn't appear to be a practical avenue of attack
58*4882a593Smuzhiyun  * even if using refcount_add() operations with larger increments.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * Memory ordering
61*4882a593Smuzhiyun  * ===============
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
64*4882a593Smuzhiyun  * and provide only what is strictly required for refcounts.
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  * The increments are fully relaxed; these will not provide ordering. The
67*4882a593Smuzhiyun  * rationale is that whatever is used to obtain the object we're increasing the
68*4882a593Smuzhiyun  * reference count on will provide the ordering. For locked data structures,
69*4882a593Smuzhiyun  * its the lock acquire, for RCU/lockless data structures its the dependent
70*4882a593Smuzhiyun  * load.
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * Do note that inc_not_zero() provides a control dependency which will order
73*4882a593Smuzhiyun  * future stores against the inc, this ensures we'll never modify the object
74*4882a593Smuzhiyun  * if we did not in fact acquire a reference.
75*4882a593Smuzhiyun  *
76*4882a593Smuzhiyun  * The decrements will provide release order, such that all the prior loads and
77*4882a593Smuzhiyun  * stores will be issued before, it also provides a control dependency, which
78*4882a593Smuzhiyun  * will order us against the subsequent free().
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  * The control dependency is against the load of the cmpxchg (ll/sc) that
81*4882a593Smuzhiyun  * succeeded. This means the stores aren't fully ordered, but this is fine
82*4882a593Smuzhiyun  * because the 1->0 transition indicates no concurrency.
83*4882a593Smuzhiyun  *
84*4882a593Smuzhiyun  * Note that the allocator is responsible for ordering things between free()
85*4882a593Smuzhiyun  * and alloc().
86*4882a593Smuzhiyun  *
87*4882a593Smuzhiyun  * The decrements dec_and_test() and sub_and_test() also provide acquire
88*4882a593Smuzhiyun  * ordering on success.
89*4882a593Smuzhiyun  *
90*4882a593Smuzhiyun  */
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun #ifndef _LINUX_REFCOUNT_H
93*4882a593Smuzhiyun #define _LINUX_REFCOUNT_H
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun #include <linux/atomic.h>
96*4882a593Smuzhiyun #include <linux/bug.h>
97*4882a593Smuzhiyun #include <linux/compiler.h>
98*4882a593Smuzhiyun #include <linux/limits.h>
99*4882a593Smuzhiyun #include <linux/spinlock_types.h>
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun struct mutex;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /**
104*4882a593Smuzhiyun  * struct refcount_t - variant of atomic_t specialized for reference counts
105*4882a593Smuzhiyun  * @refs: atomic_t counter field
106*4882a593Smuzhiyun  *
107*4882a593Smuzhiyun  * The counter saturates at REFCOUNT_SATURATED and will not move once
108*4882a593Smuzhiyun  * there. This avoids wrapping the counter and causing 'spurious'
109*4882a593Smuzhiyun  * use-after-free bugs.
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun typedef struct refcount_struct {
112*4882a593Smuzhiyun 	atomic_t refs;
113*4882a593Smuzhiyun } refcount_t;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun #define REFCOUNT_INIT(n)	{ .refs = ATOMIC_INIT(n), }
116*4882a593Smuzhiyun #define REFCOUNT_MAX		INT_MAX
117*4882a593Smuzhiyun #define REFCOUNT_SATURATED	(INT_MIN / 2)
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun enum refcount_saturation_type {
120*4882a593Smuzhiyun 	REFCOUNT_ADD_NOT_ZERO_OVF,
121*4882a593Smuzhiyun 	REFCOUNT_ADD_OVF,
122*4882a593Smuzhiyun 	REFCOUNT_ADD_UAF,
123*4882a593Smuzhiyun 	REFCOUNT_SUB_UAF,
124*4882a593Smuzhiyun 	REFCOUNT_DEC_LEAK,
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun void refcount_warn_saturate(refcount_t *r, enum refcount_saturation_type t);
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /**
130*4882a593Smuzhiyun  * refcount_set - set a refcount's value
131*4882a593Smuzhiyun  * @r: the refcount
132*4882a593Smuzhiyun  * @n: value to which the refcount will be set
133*4882a593Smuzhiyun  */
refcount_set(refcount_t * r,int n)134*4882a593Smuzhiyun static inline void refcount_set(refcount_t *r, int n)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	atomic_set(&r->refs, n);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /**
140*4882a593Smuzhiyun  * refcount_read - get a refcount's value
141*4882a593Smuzhiyun  * @r: the refcount
142*4882a593Smuzhiyun  *
143*4882a593Smuzhiyun  * Return: the refcount's value
144*4882a593Smuzhiyun  */
refcount_read(const refcount_t * r)145*4882a593Smuzhiyun static inline unsigned int refcount_read(const refcount_t *r)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	return atomic_read(&r->refs);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
__refcount_add_not_zero(int i,refcount_t * r,int * oldp)150*4882a593Smuzhiyun static inline __must_check bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	int old = refcount_read(r);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	do {
155*4882a593Smuzhiyun 		if (!old)
156*4882a593Smuzhiyun 			break;
157*4882a593Smuzhiyun 	} while (!atomic_try_cmpxchg_relaxed(&r->refs, &old, old + i));
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	if (oldp)
160*4882a593Smuzhiyun 		*oldp = old;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (unlikely(old < 0 || old + i < 0))
163*4882a593Smuzhiyun 		refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	return old;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun /**
169*4882a593Smuzhiyun  * refcount_add_not_zero - add a value to a refcount unless it is 0
170*4882a593Smuzhiyun  * @i: the value to add to the refcount
171*4882a593Smuzhiyun  * @r: the refcount
172*4882a593Smuzhiyun  *
173*4882a593Smuzhiyun  * Will saturate at REFCOUNT_SATURATED and WARN.
174*4882a593Smuzhiyun  *
175*4882a593Smuzhiyun  * Provides no memory ordering, it is assumed the caller has guaranteed the
176*4882a593Smuzhiyun  * object memory to be stable (RCU, etc.). It does provide a control dependency
177*4882a593Smuzhiyun  * and thereby orders future stores. See the comment on top.
178*4882a593Smuzhiyun  *
179*4882a593Smuzhiyun  * Use of this function is not recommended for the normal reference counting
180*4882a593Smuzhiyun  * use case in which references are taken and released one at a time.  In these
181*4882a593Smuzhiyun  * cases, refcount_inc(), or one of its variants, should instead be used to
182*4882a593Smuzhiyun  * increment a reference count.
183*4882a593Smuzhiyun  *
184*4882a593Smuzhiyun  * Return: false if the passed refcount is 0, true otherwise
185*4882a593Smuzhiyun  */
refcount_add_not_zero(int i,refcount_t * r)186*4882a593Smuzhiyun static inline __must_check bool refcount_add_not_zero(int i, refcount_t *r)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	return __refcount_add_not_zero(i, r, NULL);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
__refcount_add(int i,refcount_t * r,int * oldp)191*4882a593Smuzhiyun static inline void __refcount_add(int i, refcount_t *r, int *oldp)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun 	int old = atomic_fetch_add_relaxed(i, &r->refs);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	if (oldp)
196*4882a593Smuzhiyun 		*oldp = old;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	if (unlikely(!old))
199*4882a593Smuzhiyun 		refcount_warn_saturate(r, REFCOUNT_ADD_UAF);
200*4882a593Smuzhiyun 	else if (unlikely(old < 0 || old + i < 0))
201*4882a593Smuzhiyun 		refcount_warn_saturate(r, REFCOUNT_ADD_OVF);
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun /**
205*4882a593Smuzhiyun  * refcount_add - add a value to a refcount
206*4882a593Smuzhiyun  * @i: the value to add to the refcount
207*4882a593Smuzhiyun  * @r: the refcount
208*4882a593Smuzhiyun  *
209*4882a593Smuzhiyun  * Similar to atomic_add(), but will saturate at REFCOUNT_SATURATED and WARN.
210*4882a593Smuzhiyun  *
211*4882a593Smuzhiyun  * Provides no memory ordering, it is assumed the caller has guaranteed the
212*4882a593Smuzhiyun  * object memory to be stable (RCU, etc.). It does provide a control dependency
213*4882a593Smuzhiyun  * and thereby orders future stores. See the comment on top.
214*4882a593Smuzhiyun  *
215*4882a593Smuzhiyun  * Use of this function is not recommended for the normal reference counting
216*4882a593Smuzhiyun  * use case in which references are taken and released one at a time.  In these
217*4882a593Smuzhiyun  * cases, refcount_inc(), or one of its variants, should instead be used to
218*4882a593Smuzhiyun  * increment a reference count.
219*4882a593Smuzhiyun  */
refcount_add(int i,refcount_t * r)220*4882a593Smuzhiyun static inline void refcount_add(int i, refcount_t *r)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	__refcount_add(i, r, NULL);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun 
__refcount_inc_not_zero(refcount_t * r,int * oldp)225*4882a593Smuzhiyun static inline __must_check bool __refcount_inc_not_zero(refcount_t *r, int *oldp)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	return __refcount_add_not_zero(1, r, oldp);
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun /**
231*4882a593Smuzhiyun  * refcount_inc_not_zero - increment a refcount unless it is 0
232*4882a593Smuzhiyun  * @r: the refcount to increment
233*4882a593Smuzhiyun  *
234*4882a593Smuzhiyun  * Similar to atomic_inc_not_zero(), but will saturate at REFCOUNT_SATURATED
235*4882a593Smuzhiyun  * and WARN.
236*4882a593Smuzhiyun  *
237*4882a593Smuzhiyun  * Provides no memory ordering, it is assumed the caller has guaranteed the
238*4882a593Smuzhiyun  * object memory to be stable (RCU, etc.). It does provide a control dependency
239*4882a593Smuzhiyun  * and thereby orders future stores. See the comment on top.
240*4882a593Smuzhiyun  *
241*4882a593Smuzhiyun  * Return: true if the increment was successful, false otherwise
242*4882a593Smuzhiyun  */
refcount_inc_not_zero(refcount_t * r)243*4882a593Smuzhiyun static inline __must_check bool refcount_inc_not_zero(refcount_t *r)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	return __refcount_inc_not_zero(r, NULL);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun 
__refcount_inc(refcount_t * r,int * oldp)248*4882a593Smuzhiyun static inline void __refcount_inc(refcount_t *r, int *oldp)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun 	__refcount_add(1, r, oldp);
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun /**
254*4882a593Smuzhiyun  * refcount_inc - increment a refcount
255*4882a593Smuzhiyun  * @r: the refcount to increment
256*4882a593Smuzhiyun  *
257*4882a593Smuzhiyun  * Similar to atomic_inc(), but will saturate at REFCOUNT_SATURATED and WARN.
258*4882a593Smuzhiyun  *
259*4882a593Smuzhiyun  * Provides no memory ordering, it is assumed the caller already has a
260*4882a593Smuzhiyun  * reference on the object.
261*4882a593Smuzhiyun  *
262*4882a593Smuzhiyun  * Will WARN if the refcount is 0, as this represents a possible use-after-free
263*4882a593Smuzhiyun  * condition.
264*4882a593Smuzhiyun  */
refcount_inc(refcount_t * r)265*4882a593Smuzhiyun static inline void refcount_inc(refcount_t *r)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun 	__refcount_inc(r, NULL);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun 
__refcount_sub_and_test(int i,refcount_t * r,int * oldp)270*4882a593Smuzhiyun static inline __must_check bool __refcount_sub_and_test(int i, refcount_t *r, int *oldp)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun 	int old = atomic_fetch_sub_release(i, &r->refs);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	if (oldp)
275*4882a593Smuzhiyun 		*oldp = old;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	if (old == i) {
278*4882a593Smuzhiyun 		smp_acquire__after_ctrl_dep();
279*4882a593Smuzhiyun 		return true;
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	if (unlikely(old < 0 || old - i < 0))
283*4882a593Smuzhiyun 		refcount_warn_saturate(r, REFCOUNT_SUB_UAF);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	return false;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun /**
289*4882a593Smuzhiyun  * refcount_sub_and_test - subtract from a refcount and test if it is 0
290*4882a593Smuzhiyun  * @i: amount to subtract from the refcount
291*4882a593Smuzhiyun  * @r: the refcount
292*4882a593Smuzhiyun  *
293*4882a593Smuzhiyun  * Similar to atomic_dec_and_test(), but it will WARN, return false and
294*4882a593Smuzhiyun  * ultimately leak on underflow and will fail to decrement when saturated
295*4882a593Smuzhiyun  * at REFCOUNT_SATURATED.
296*4882a593Smuzhiyun  *
297*4882a593Smuzhiyun  * Provides release memory ordering, such that prior loads and stores are done
298*4882a593Smuzhiyun  * before, and provides an acquire ordering on success such that free()
299*4882a593Smuzhiyun  * must come after.
300*4882a593Smuzhiyun  *
301*4882a593Smuzhiyun  * Use of this function is not recommended for the normal reference counting
302*4882a593Smuzhiyun  * use case in which references are taken and released one at a time.  In these
303*4882a593Smuzhiyun  * cases, refcount_dec(), or one of its variants, should instead be used to
304*4882a593Smuzhiyun  * decrement a reference count.
305*4882a593Smuzhiyun  *
306*4882a593Smuzhiyun  * Return: true if the resulting refcount is 0, false otherwise
307*4882a593Smuzhiyun  */
refcount_sub_and_test(int i,refcount_t * r)308*4882a593Smuzhiyun static inline __must_check bool refcount_sub_and_test(int i, refcount_t *r)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun 	return __refcount_sub_and_test(i, r, NULL);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
__refcount_dec_and_test(refcount_t * r,int * oldp)313*4882a593Smuzhiyun static inline __must_check bool __refcount_dec_and_test(refcount_t *r, int *oldp)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	return __refcount_sub_and_test(1, r, oldp);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun /**
319*4882a593Smuzhiyun  * refcount_dec_and_test - decrement a refcount and test if it is 0
320*4882a593Smuzhiyun  * @r: the refcount
321*4882a593Smuzhiyun  *
322*4882a593Smuzhiyun  * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
323*4882a593Smuzhiyun  * decrement when saturated at REFCOUNT_SATURATED.
324*4882a593Smuzhiyun  *
325*4882a593Smuzhiyun  * Provides release memory ordering, such that prior loads and stores are done
326*4882a593Smuzhiyun  * before, and provides an acquire ordering on success such that free()
327*4882a593Smuzhiyun  * must come after.
328*4882a593Smuzhiyun  *
329*4882a593Smuzhiyun  * Return: true if the resulting refcount is 0, false otherwise
330*4882a593Smuzhiyun  */
refcount_dec_and_test(refcount_t * r)331*4882a593Smuzhiyun static inline __must_check bool refcount_dec_and_test(refcount_t *r)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	return __refcount_dec_and_test(r, NULL);
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun 
__refcount_dec(refcount_t * r,int * oldp)336*4882a593Smuzhiyun static inline void __refcount_dec(refcount_t *r, int *oldp)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun 	int old = atomic_fetch_sub_release(1, &r->refs);
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	if (oldp)
341*4882a593Smuzhiyun 		*oldp = old;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	if (unlikely(old <= 1))
344*4882a593Smuzhiyun 		refcount_warn_saturate(r, REFCOUNT_DEC_LEAK);
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun /**
348*4882a593Smuzhiyun  * refcount_dec - decrement a refcount
349*4882a593Smuzhiyun  * @r: the refcount
350*4882a593Smuzhiyun  *
351*4882a593Smuzhiyun  * Similar to atomic_dec(), it will WARN on underflow and fail to decrement
352*4882a593Smuzhiyun  * when saturated at REFCOUNT_SATURATED.
353*4882a593Smuzhiyun  *
354*4882a593Smuzhiyun  * Provides release memory ordering, such that prior loads and stores are done
355*4882a593Smuzhiyun  * before.
356*4882a593Smuzhiyun  */
refcount_dec(refcount_t * r)357*4882a593Smuzhiyun static inline void refcount_dec(refcount_t *r)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	__refcount_dec(r, NULL);
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun extern __must_check bool refcount_dec_if_one(refcount_t *r);
363*4882a593Smuzhiyun extern __must_check bool refcount_dec_not_one(refcount_t *r);
364*4882a593Smuzhiyun extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock);
365*4882a593Smuzhiyun extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock);
366*4882a593Smuzhiyun extern __must_check bool refcount_dec_and_lock_irqsave(refcount_t *r,
367*4882a593Smuzhiyun 						       spinlock_t *lock,
368*4882a593Smuzhiyun 						       unsigned long *flags);
369*4882a593Smuzhiyun #endif /* _LINUX_REFCOUNT_H */
370