1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/export.h>
3 #include <linux/lockref.h>
5 #if USE_CMPXCHG_LOCKREF
8 * Note that the "cmpxchg()" reloads the "old" value for the
11 #define CMPXCHG_LOOP(CODE, SUCCESS) do { \
14 BUILD_BUG_ON(sizeof(old) != 8); \
15 old.lock_count = READ_ONCE(lockref->lock_count); \
16 while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) { \
17 struct lockref new = old; \
19 if (likely(try_cmpxchg64_relaxed(&lockref->lock_count, \
31 #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
36 * lockref_get - Increments reference count unconditionally
37 * @lockref: pointer to lockref structure
39 * This operation is only valid if you already hold a reference
40 * to the object, so you know the count cannot be zero.
42 void lockref_get(struct lockref *lockref)
50 spin_lock(&lockref->lock);
52 spin_unlock(&lockref->lock);
54 EXPORT_SYMBOL(lockref_get);
57 * lockref_get_not_zero - Increments count unless the count is 0 or dead
58 * @lockref: pointer to lockref structure
59 * Return: 1 if count updated successfully or 0 if count was zero
61 int lockref_get_not_zero(struct lockref *lockref)
73 spin_lock(&lockref->lock);
75 if (lockref->count > 0) {
79 spin_unlock(&lockref->lock);
82 EXPORT_SYMBOL(lockref_get_not_zero);
85 * lockref_put_not_zero - Decrements count unless count <= 1 before decrement
86 * @lockref: pointer to lockref structure
87 * Return: 1 if count updated successfully or 0 if count would become zero
89 int lockref_put_not_zero(struct lockref *lockref)
101 spin_lock(&lockref->lock);
103 if (lockref->count > 1) {
107 spin_unlock(&lockref->lock);
110 EXPORT_SYMBOL(lockref_put_not_zero);
113 * lockref_put_return - Decrement reference count if possible
114 * @lockref: pointer to lockref structure
116 * Decrement the reference count and return the new value.
117 * If the lockref was dead or locked, return an error.
119 int lockref_put_return(struct lockref *lockref)
130 EXPORT_SYMBOL(lockref_put_return);
133 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
134 * @lockref: pointer to lockref structure
135 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
137 int lockref_put_or_lock(struct lockref *lockref)
147 spin_lock(&lockref->lock);
148 if (lockref->count <= 1)
151 spin_unlock(&lockref->lock);
154 EXPORT_SYMBOL(lockref_put_or_lock);
157 * lockref_mark_dead - mark lockref dead
158 * @lockref: pointer to lockref structure
160 void lockref_mark_dead(struct lockref *lockref)
162 assert_spin_locked(&lockref->lock);
163 lockref->count = -128;
165 EXPORT_SYMBOL(lockref_mark_dead);
168 * lockref_get_not_dead - Increments count unless the ref is dead
169 * @lockref: pointer to lockref structure
170 * Return: 1 if count updated successfully or 0 if lockref was dead
172 int lockref_get_not_dead(struct lockref *lockref)
184 spin_lock(&lockref->lock);
186 if (lockref->count >= 0) {
190 spin_unlock(&lockref->lock);
193 EXPORT_SYMBOL(lockref_get_not_dead);