Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _TOOLS_LINUX_REFCOUNT_H
0003 #define _TOOLS_LINUX_REFCOUNT_H
0004 
0005 /*
0006  * Variant of atomic_t specialized for reference counts.
0007  *
0008  * The interface matches the atomic_t interface (to aid in porting) but only
0009  * provides the few functions one should use for reference counting.
0010  *
0011  * It differs in that the counter saturates at UINT_MAX and will not move once
0012  * there. This avoids wrapping the counter and causing 'spurious'
0013  * use-after-free issues.
0014  *
0015  * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
0016  * and provide only what is strictly required for refcounts.
0017  *
0018  * The increments are fully relaxed; these will not provide ordering. The
0019  * rationale is that whatever is used to obtain the object we're increasing the
0020  * reference count on will provide the ordering. For locked data structures,
0021  * its the lock acquire, for RCU/lockless data structures its the dependent
0022  * load.
0023  *
0024  * Do note that inc_not_zero() provides a control dependency which will order
0025  * future stores against the inc, this ensures we'll never modify the object
0026  * if we did not in fact acquire a reference.
0027  *
0028  * The decrements will provide release order, such that all the prior loads and
0029  * stores will be issued before, it also provides a control dependency, which
0030  * will order us against the subsequent free().
0031  *
0032  * The control dependency is against the load of the cmpxchg (ll/sc) that
0033  * succeeded. This means the stores aren't fully ordered, but this is fine
0034  * because the 1->0 transition indicates no concurrency.
0035  *
0036  * Note that the allocator is responsible for ordering things between free()
0037  * and alloc().
0038  *
0039  */
0040 
0041 #include <linux/atomic.h>
0042 #include <linux/kernel.h>
0043 
0044 #ifdef NDEBUG
0045 #define REFCOUNT_WARN(cond, str) (void)(cond)
0046 #define __refcount_check
0047 #else
0048 #define REFCOUNT_WARN(cond, str) BUG_ON(cond)
0049 #define __refcount_check    __must_check
0050 #endif
0051 
0052 typedef struct refcount_struct {
0053     atomic_t refs;
0054 } refcount_t;
0055 
0056 #define REFCOUNT_INIT(n)    { .refs = ATOMIC_INIT(n), }
0057 
0058 static inline void refcount_set(refcount_t *r, unsigned int n)
0059 {
0060     atomic_set(&r->refs, n);
0061 }
0062 
0063 static inline unsigned int refcount_read(const refcount_t *r)
0064 {
0065     return atomic_read(&r->refs);
0066 }
0067 
0068 /*
0069  * Similar to atomic_inc_not_zero(), will saturate at UINT_MAX and WARN.
0070  *
0071  * Provides no memory ordering, it is assumed the caller has guaranteed the
0072  * object memory to be stable (RCU, etc.). It does provide a control dependency
0073  * and thereby orders future stores. See the comment on top.
0074  */
0075 static inline __refcount_check
0076 bool refcount_inc_not_zero(refcount_t *r)
0077 {
0078     unsigned int old, new, val = atomic_read(&r->refs);
0079 
0080     for (;;) {
0081         new = val + 1;
0082 
0083         if (!val)
0084             return false;
0085 
0086         if (unlikely(!new))
0087             return true;
0088 
0089         old = atomic_cmpxchg_relaxed(&r->refs, val, new);
0090         if (old == val)
0091             break;
0092 
0093         val = old;
0094     }
0095 
0096     REFCOUNT_WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
0097 
0098     return true;
0099 }
0100 
0101 /*
0102  * Similar to atomic_inc(), will saturate at UINT_MAX and WARN.
0103  *
0104  * Provides no memory ordering, it is assumed the caller already has a
0105  * reference on the object, will WARN when this is not so.
0106  */
0107 static inline void refcount_inc(refcount_t *r)
0108 {
0109     REFCOUNT_WARN(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
0110 }
0111 
0112 /*
0113  * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
0114  * decrement when saturated at UINT_MAX.
0115  *
0116  * Provides release memory ordering, such that prior loads and stores are done
0117  * before, and provides a control dependency such that free() must come after.
0118  * See the comment on top.
0119  */
0120 static inline __refcount_check
0121 bool refcount_sub_and_test(unsigned int i, refcount_t *r)
0122 {
0123     unsigned int old, new, val = atomic_read(&r->refs);
0124 
0125     for (;;) {
0126         if (unlikely(val == UINT_MAX))
0127             return false;
0128 
0129         new = val - i;
0130         if (new > val) {
0131             REFCOUNT_WARN(new > val, "refcount_t: underflow; use-after-free.\n");
0132             return false;
0133         }
0134 
0135         old = atomic_cmpxchg_release(&r->refs, val, new);
0136         if (old == val)
0137             break;
0138 
0139         val = old;
0140     }
0141 
0142     return !new;
0143 }
0144 
0145 static inline __refcount_check
0146 bool refcount_dec_and_test(refcount_t *r)
0147 {
0148     return refcount_sub_and_test(1, r);
0149 }
0150 
0151 
0152 #endif /* _ATOMIC_LINUX_REFCOUNT_H */