~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/include/linux/refcount.h

Version: ~ [ linux-5.1-rc5 ] ~ [ linux-5.0.7 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.34 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.111 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.168 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.178 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.138 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.65 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 52 typedef struct refcount_struct {
 53         atomic_t refs;
 54 } refcount_t;
 55 
 56 #define REFCOUNT_INIT(n)        { .refs = ATOMIC_INIT(n), }
 57 
 58 static inline void refcount_set(refcount_t *r, unsigned int n)
 59 {
 60         atomic_set(&r->refs, n);
 61 }
 62 
 63 static inline unsigned int refcount_read(const refcount_t *r)
 64 {
 65         return atomic_read(&r->refs);
 66 }
 67 
 68 /*
 69  * Similar to atomic_inc_not_zero(), will saturate at UINT_MAX and WARN.
 70  *
 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.
 74  */
 75 static inline __refcount_check
 76 bool refcount_inc_not_zero(refcount_t *r)
 77 {
 78         unsigned int old, new, val = atomic_read(&r->refs);
 79 
 80         for (;;) {
 81                 new = val + 1;
 82 
 83                 if (!val)
 84                         return false;
 85 
 86                 if (unlikely(!new))
 87                         return true;
 88 
 89                 old = atomic_cmpxchg_relaxed(&r->refs, val, new);
 90                 if (old == val)
 91                         break;
 92 
 93                 val = old;
 94         }
 95 
 96         REFCOUNT_WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
 97 
 98         return true;
 99 }
100 
101 /*
102  * Similar to atomic_inc(), will saturate at UINT_MAX and WARN.
103  *
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.
106  */
107 static inline void refcount_inc(refcount_t *r)
108 {
109         REFCOUNT_WARN(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
110 }
111 
112 /*
113  * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
114  * decrement when saturated at UINT_MAX.
115  *
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.
119  */
120 static inline __refcount_check
121 bool refcount_sub_and_test(unsigned int i, refcount_t *r)
122 {
123         unsigned int old, new, val = atomic_read(&r->refs);
124 
125         for (;;) {
126                 if (unlikely(val == UINT_MAX))
127                         return false;
128 
129                 new = val - i;
130                 if (new > val) {
131                         REFCOUNT_WARN(new > val, "refcount_t: underflow; use-after-free.\n");
132                         return false;
133                 }
134 
135                 old = atomic_cmpxchg_release(&r->refs, val, new);
136                 if (old == val)
137                         break;
138 
139                 val = old;
140         }
141 
142         return !new;
143 }
144 
145 static inline __refcount_check
146 bool refcount_dec_and_test(refcount_t *r)
147 {
148         return refcount_sub_and_test(1, r);
149 }
150 
151 
152 #endif /* _ATOMIC_LINUX_REFCOUNT_H */
153 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp