1 // SPDX-License-Identifier: GPL-2.0
3 * Out-of-line refcount functions.
6 #include <linux/mutex.h>
7 #include <linux/refcount.h>
8 #include <linux/spinlock.h>
11 #define REFCOUNT_WARN(str) WARN_ONCE(1, "refcount_t: " str ".\n")
13 void refcount_warn_saturate(refcount_t *r, enum refcount_saturation_type t)
15 refcount_set(r, REFCOUNT_SATURATED);
18 case REFCOUNT_ADD_NOT_ZERO_OVF:
19 REFCOUNT_WARN("saturated; leaking memory");
21 case REFCOUNT_ADD_OVF:
22 REFCOUNT_WARN("saturated; leaking memory");
24 case REFCOUNT_ADD_UAF:
25 REFCOUNT_WARN("addition on 0; use-after-free");
27 case REFCOUNT_SUB_UAF:
28 REFCOUNT_WARN("underflow; use-after-free");
30 case REFCOUNT_DEC_LEAK:
31 REFCOUNT_WARN("decrement hit 0; leaking memory");
34 REFCOUNT_WARN("unknown saturation event!?");
37 EXPORT_SYMBOL(refcount_warn_saturate);
40 * refcount_dec_if_one - decrement a refcount if it is 1
43 * No atomic_t counterpart, it attempts a 1 -> 0 transition and returns the
46 * Like all decrement operations, it provides release memory order and provides
47 * a control dependency.
49 * It can be used like a try-delete operator; this explicit case is provided
50 * and not cmpxchg in generic, because that would allow implementing unsafe
53 * Return: true if the resulting refcount is 0, false otherwise
55 bool refcount_dec_if_one(refcount_t *r)
59 return atomic_try_cmpxchg_release(&r->refs, &val, 0);
61 EXPORT_SYMBOL(refcount_dec_if_one);
64 * refcount_dec_not_one - decrement a refcount if it is not 1
67 * No atomic_t counterpart, it decrements unless the value is 1, in which case
68 * it will return false.
70 * Was often done like: atomic_add_unless(&var, -1, 1)
72 * Return: true if the decrement operation was successful, false otherwise
74 bool refcount_dec_not_one(refcount_t *r)
76 unsigned int new, val = atomic_read(&r->refs);
79 if (unlikely(val == REFCOUNT_SATURATED))
87 WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
91 } while (!atomic_try_cmpxchg_release(&r->refs, &val, new));
95 EXPORT_SYMBOL(refcount_dec_not_one);
98 * refcount_dec_and_mutex_lock - return holding mutex if able to decrement
101 * @lock: the mutex to be locked
103 * Similar to atomic_dec_and_mutex_lock(), it will WARN on underflow and fail
104 * to decrement when saturated at REFCOUNT_SATURATED.
106 * Provides release memory ordering, such that prior loads and stores are done
107 * before, and provides a control dependency such that free() must come after.
108 * See the comment on top.
110 * Return: true and hold mutex if able to decrement refcount to 0, false
113 bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock)
115 if (refcount_dec_not_one(r))
119 if (!refcount_dec_and_test(r)) {
126 EXPORT_SYMBOL(refcount_dec_and_mutex_lock);
129 * refcount_dec_and_lock - return holding spinlock if able to decrement
132 * @lock: the spinlock to be locked
134 * Similar to atomic_dec_and_lock(), it will WARN on underflow and fail to
135 * decrement when saturated at REFCOUNT_SATURATED.
137 * Provides release memory ordering, such that prior loads and stores are done
138 * before, and provides a control dependency such that free() must come after.
139 * See the comment on top.
141 * Return: true and hold spinlock if able to decrement refcount to 0, false
144 bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock)
146 if (refcount_dec_not_one(r))
150 if (!refcount_dec_and_test(r)) {
157 EXPORT_SYMBOL(refcount_dec_and_lock);
160 * refcount_dec_and_lock_irqsave - return holding spinlock with disabled
161 * interrupts if able to decrement refcount to 0
163 * @lock: the spinlock to be locked
164 * @flags: saved IRQ-flags if the is acquired
166 * Same as refcount_dec_and_lock() above except that the spinlock is acquired
167 * with disabled interupts.
169 * Return: true and hold spinlock if able to decrement refcount to 0, false
172 bool refcount_dec_and_lock_irqsave(refcount_t *r, spinlock_t *lock,
173 unsigned long *flags)
175 if (refcount_dec_not_one(r))
178 spin_lock_irqsave(lock, *flags);
179 if (!refcount_dec_and_test(r)) {
180 spin_unlock_irqrestore(lock, *flags);
186 EXPORT_SYMBOL(refcount_dec_and_lock_irqsave);