linux/lib/refcount.c
<<
>>
Prefs
   1/*
   2 * Variant of atomic_t specialized for reference counts.
   3 *
   4 * The interface matches the atomic_t interface (to aid in porting) but only
   5 * provides the few functions one should use for reference counting.
   6 *
   7 * It differs in that the counter saturates at UINT_MAX and will not move once
   8 * there. This avoids wrapping the counter and causing 'spurious'
   9 * use-after-free issues.
  10 *
  11 * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
  12 * and provide only what is strictly required for refcounts.
  13 *
  14 * The increments are fully relaxed; these will not provide ordering. The
  15 * rationale is that whatever is used to obtain the object we're increasing the
  16 * reference count on will provide the ordering. For locked data structures,
  17 * its the lock acquire, for RCU/lockless data structures its the dependent
  18 * load.
  19 *
  20 * Do note that inc_not_zero() provides a control dependency which will order
  21 * future stores against the inc, this ensures we'll never modify the object
  22 * if we did not in fact acquire a reference.
  23 *
  24 * The decrements will provide release order, such that all the prior loads and
  25 * stores will be issued before, it also provides a control dependency, which
  26 * will order us against the subsequent free().
  27 *
  28 * The control dependency is against the load of the cmpxchg (ll/sc) that
  29 * succeeded. This means the stores aren't fully ordered, but this is fine
  30 * because the 1->0 transition indicates no concurrency.
  31 *
  32 * Note that the allocator is responsible for ordering things between free()
  33 * and alloc().
  34 *
  35 */
  36
  37#include <linux/refcount.h>
  38#include <linux/bug.h>
  39
  40#ifdef CONFIG_REFCOUNT_FULL
  41
  42/**
  43 * refcount_add_not_zero - add a value to a refcount unless it is 0
  44 * @i: the value to add to the refcount
  45 * @r: the refcount
  46 *
  47 * Will saturate at UINT_MAX and WARN.
  48 *
  49 * Provides no memory ordering, it is assumed the caller has guaranteed the
  50 * object memory to be stable (RCU, etc.). It does provide a control dependency
  51 * and thereby orders future stores. See the comment on top.
  52 *
  53 * Use of this function is not recommended for the normal reference counting
  54 * use case in which references are taken and released one at a time.  In these
  55 * cases, refcount_inc(), or one of its variants, should instead be used to
  56 * increment a reference count.
  57 *
  58 * Return: false if the passed refcount is 0, true otherwise
  59 */
  60bool refcount_add_not_zero(unsigned int i, refcount_t *r)
  61{
  62        unsigned int old, new, val = atomic_read(&r->refs);
  63
  64        for (;;) {
  65                if (!val)
  66                        return false;
  67
  68                if (unlikely(val == UINT_MAX))
  69                        return true;
  70
  71                new = val + i;
  72                if (new < val)
  73                        new = UINT_MAX;
  74                old = atomic_cmpxchg_relaxed(&r->refs, val, new);
  75                if (old == val)
  76                        break;
  77
  78                val = old;
  79        }
  80
  81        WARN_ONCE(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
  82
  83        return true;
  84}
  85EXPORT_SYMBOL(refcount_add_not_zero);
  86
  87/**
  88 * refcount_add - add a value to a refcount
  89 * @i: the value to add to the refcount
  90 * @r: the refcount
  91 *
  92 * Similar to atomic_add(), but will saturate at UINT_MAX and WARN.
  93 *
  94 * Provides no memory ordering, it is assumed the caller has guaranteed the
  95 * object memory to be stable (RCU, etc.). It does provide a control dependency
  96 * and thereby orders future stores. See the comment on top.
  97 *
  98 * Use of this function is not recommended for the normal reference counting
  99 * use case in which references are taken and released one at a time.  In these
 100 * cases, refcount_inc(), or one of its variants, should instead be used to
 101 * increment a reference count.
 102 */
 103void refcount_add(unsigned int i, refcount_t *r)
 104{
 105        WARN_ONCE(!refcount_add_not_zero(i, r), "refcount_t: addition on 0; use-after-free.\n");
 106}
 107EXPORT_SYMBOL(refcount_add);
 108
 109/**
 110 * refcount_inc_not_zero - increment a refcount unless it is 0
 111 * @r: the refcount to increment
 112 *
 113 * Similar to atomic_inc_not_zero(), but will saturate at UINT_MAX and WARN.
 114 *
 115 * Provides no memory ordering, it is assumed the caller has guaranteed the
 116 * object memory to be stable (RCU, etc.). It does provide a control dependency
 117 * and thereby orders future stores. See the comment on top.
 118 *
 119 * Return: true if the increment was successful, false otherwise
 120 */
 121bool refcount_inc_not_zero(refcount_t *r)
 122{
 123        unsigned int old, new, val = atomic_read(&r->refs);
 124
 125        for (;;) {
 126                new = val + 1;
 127
 128                if (!val)
 129                        return false;
 130
 131                if (unlikely(!new))
 132                        return true;
 133
 134                old = atomic_cmpxchg_relaxed(&r->refs, val, new);
 135                if (old == val)
 136                        break;
 137
 138                val = old;
 139        }
 140
 141        WARN_ONCE(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
 142
 143        return true;
 144}
 145EXPORT_SYMBOL(refcount_inc_not_zero);
 146
 147/**
 148 * refcount_inc - increment a refcount
 149 * @r: the refcount to increment
 150 *
 151 * Similar to atomic_inc(), but will saturate at UINT_MAX and WARN.
 152 *
 153 * Provides no memory ordering, it is assumed the caller already has a
 154 * reference on the object.
 155 *
 156 * Will WARN if the refcount is 0, as this represents a possible use-after-free
 157 * condition.
 158 */
 159void refcount_inc(refcount_t *r)
 160{
 161        WARN_ONCE(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
 162}
 163EXPORT_SYMBOL(refcount_inc);
 164
 165/**
 166 * refcount_sub_and_test - subtract from a refcount and test if it is 0
 167 * @i: amount to subtract from the refcount
 168 * @r: the refcount
 169 *
 170 * Similar to atomic_dec_and_test(), but it will WARN, return false and
 171 * ultimately leak on underflow and will fail to decrement when saturated
 172 * at UINT_MAX.
 173 *
 174 * Provides release memory ordering, such that prior loads and stores are done
 175 * before, and provides a control dependency such that free() must come after.
 176 * See the comment on top.
 177 *
 178 * Use of this function is not recommended for the normal reference counting
 179 * use case in which references are taken and released one at a time.  In these
 180 * cases, refcount_dec(), or one of its variants, should instead be used to
 181 * decrement a reference count.
 182 *
 183 * Return: true if the resulting refcount is 0, false otherwise
 184 */
 185bool refcount_sub_and_test(unsigned int i, refcount_t *r)
 186{
 187        unsigned int old, new, val = atomic_read(&r->refs);
 188
 189        for (;;) {
 190                if (unlikely(val == UINT_MAX))
 191                        return false;
 192
 193                new = val - i;
 194                if (new > val) {
 195                        WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
 196                        return false;
 197                }
 198
 199                old = atomic_cmpxchg_release(&r->refs, val, new);
 200                if (old == val)
 201                        break;
 202
 203                val = old;
 204        }
 205
 206        return !new;
 207}
 208EXPORT_SYMBOL(refcount_sub_and_test);
 209
 210/**
 211 * refcount_dec_and_test - decrement a refcount and test if it is 0
 212 * @r: the refcount
 213 *
 214 * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
 215 * decrement when saturated at UINT_MAX.
 216 *
 217 * Provides release memory ordering, such that prior loads and stores are done
 218 * before, and provides a control dependency such that free() must come after.
 219 * See the comment on top.
 220 *
 221 * Return: true if the resulting refcount is 0, false otherwise
 222 */
 223bool refcount_dec_and_test(refcount_t *r)
 224{
 225        return refcount_sub_and_test(1, r);
 226}
 227EXPORT_SYMBOL(refcount_dec_and_test);
 228
 229/**
 230 * refcount_dec - decrement a refcount
 231 * @r: the refcount
 232 *
 233 * Similar to atomic_dec(), it will WARN on underflow and fail to decrement
 234 * when saturated at UINT_MAX.
 235 *
 236 * Provides release memory ordering, such that prior loads and stores are done
 237 * before.
 238 */
 239void refcount_dec(refcount_t *r)
 240{
 241        WARN_ONCE(refcount_dec_and_test(r), "refcount_t: decrement hit 0; leaking memory.\n");
 242}
 243EXPORT_SYMBOL(refcount_dec);
 244#endif /* CONFIG_REFCOUNT_FULL */
 245
 246/**
 247 * refcount_dec_if_one - decrement a refcount if it is 1
 248 * @r: the refcount
 249 *
 250 * No atomic_t counterpart, it attempts a 1 -> 0 transition and returns the
 251 * success thereof.
 252 *
 253 * Like all decrement operations, it provides release memory order and provides
 254 * a control dependency.
 255 *
 256 * It can be used like a try-delete operator; this explicit case is provided
 257 * and not cmpxchg in generic, because that would allow implementing unsafe
 258 * operations.
 259 *
 260 * Return: true if the resulting refcount is 0, false otherwise
 261 */
 262bool refcount_dec_if_one(refcount_t *r)
 263{
 264        return atomic_cmpxchg_release(&r->refs, 1, 0) == 1;
 265}
 266EXPORT_SYMBOL(refcount_dec_if_one);
 267
 268/**
 269 * refcount_dec_not_one - decrement a refcount if it is not 1
 270 * @r: the refcount
 271 *
 272 * No atomic_t counterpart, it decrements unless the value is 1, in which case
 273 * it will return false.
 274 *
 275 * Was often done like: atomic_add_unless(&var, -1, 1)
 276 *
 277 * Return: true if the decrement operation was successful, false otherwise
 278 */
 279bool refcount_dec_not_one(refcount_t *r)
 280{
 281        unsigned int old, new, val = atomic_read(&r->refs);
 282
 283        for (;;) {
 284                if (unlikely(val == UINT_MAX))
 285                        return true;
 286
 287                if (val == 1)
 288                        return false;
 289
 290                new = val - 1;
 291                if (new > val) {
 292                        WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
 293                        return true;
 294                }
 295
 296                old = atomic_cmpxchg_release(&r->refs, val, new);
 297                if (old == val)
 298                        break;
 299
 300                val = old;
 301        }
 302
 303        return true;
 304}
 305EXPORT_SYMBOL(refcount_dec_not_one);
 306
 307/**
 308 * refcount_dec_and_mutex_lock - return holding mutex if able to decrement
 309 *                               refcount to 0
 310 * @r: the refcount
 311 * @lock: the mutex to be locked
 312 *
 313 * Similar to atomic_dec_and_mutex_lock(), it will WARN on underflow and fail
 314 * to decrement when saturated at UINT_MAX.
 315 *
 316 * Provides release memory ordering, such that prior loads and stores are done
 317 * before, and provides a control dependency such that free() must come after.
 318 * See the comment on top.
 319 *
 320 * Return: true and hold mutex if able to decrement refcount to 0, false
 321 *         otherwise
 322 */
 323bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock)
 324{
 325        if (refcount_dec_not_one(r))
 326                return false;
 327
 328        mutex_lock(lock);
 329        if (!refcount_dec_and_test(r)) {
 330                mutex_unlock(lock);
 331                return false;
 332        }
 333
 334        return true;
 335}
 336EXPORT_SYMBOL(refcount_dec_and_mutex_lock);
 337
 338/**
 339 * refcount_dec_and_lock - return holding spinlock if able to decrement
 340 *                         refcount to 0
 341 * @r: the refcount
 342 * @lock: the spinlock to be locked
 343 *
 344 * Similar to atomic_dec_and_lock(), it will WARN on underflow and fail to
 345 * decrement when saturated at UINT_MAX.
 346 *
 347 * Provides release memory ordering, such that prior loads and stores are done
 348 * before, and provides a control dependency such that free() must come after.
 349 * See the comment on top.
 350 *
 351 * Return: true and hold spinlock if able to decrement refcount to 0, false
 352 *         otherwise
 353 */
 354bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock)
 355{
 356        if (refcount_dec_not_one(r))
 357                return false;
 358
 359        spin_lock(lock);
 360        if (!refcount_dec_and_test(r)) {
 361                spin_unlock(lock);
 362                return false;
 363        }
 364
 365        return true;
 366}
 367EXPORT_SYMBOL(refcount_dec_and_lock);
 368
 369