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, \
32 #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
37 * lockref_get - Increments reference count unconditionally
38 * @lockref: pointer to lockref structure
40 * This operation is only valid if you already hold a reference
41 * to the object, so you know the count cannot be zero.
43 void lockref_get(struct lockref *lockref)
51 spin_lock(&lockref->lock);
53 spin_unlock(&lockref->lock);
55 EXPORT_SYMBOL(lockref_get);
58 * lockref_get_not_zero - Increments count unless the count is 0 or dead
59 * @lockref: pointer to lockref structure
60 * Return: 1 if count updated successfully or 0 if count was zero
62 int lockref_get_not_zero(struct lockref *lockref)
74 spin_lock(&lockref->lock);
76 if (lockref->count > 0) {
80 spin_unlock(&lockref->lock);
83 EXPORT_SYMBOL(lockref_get_not_zero);
86 * lockref_put_not_zero - Decrements count unless count <= 1 before decrement
87 * @lockref: pointer to lockref structure
88 * Return: 1 if count updated successfully or 0 if count would become zero
90 int lockref_put_not_zero(struct lockref *lockref)
102 spin_lock(&lockref->lock);
104 if (lockref->count > 1) {
108 spin_unlock(&lockref->lock);
111 EXPORT_SYMBOL(lockref_put_not_zero);
114 * lockref_get_or_lock - Increments count unless the count is 0 or dead
115 * @lockref: pointer to lockref structure
116 * Return: 1 if count updated successfully or 0 if count was zero
117 * and we got the lock instead.
119 int lockref_get_or_lock(struct lockref *lockref)
129 spin_lock(&lockref->lock);
130 if (lockref->count <= 0)
133 spin_unlock(&lockref->lock);
136 EXPORT_SYMBOL(lockref_get_or_lock);
139 * lockref_put_return - Decrement reference count if possible
140 * @lockref: pointer to lockref structure
142 * Decrement the reference count and return the new value.
143 * If the lockref was dead or locked, return an error.
145 int lockref_put_return(struct lockref *lockref)
156 EXPORT_SYMBOL(lockref_put_return);
159 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
160 * @lockref: pointer to lockref structure
161 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
163 int lockref_put_or_lock(struct lockref *lockref)
173 spin_lock(&lockref->lock);
174 if (lockref->count <= 1)
177 spin_unlock(&lockref->lock);
180 EXPORT_SYMBOL(lockref_put_or_lock);
183 * lockref_mark_dead - mark lockref dead
184 * @lockref: pointer to lockref structure
186 void lockref_mark_dead(struct lockref *lockref)
188 assert_spin_locked(&lockref->lock);
189 lockref->count = -128;
191 EXPORT_SYMBOL(lockref_mark_dead);
194 * lockref_get_not_dead - Increments count unless the ref is dead
195 * @lockref: pointer to lockref structure
196 * Return: 1 if count updated successfully or 0 if lockref was dead
198 int lockref_get_not_dead(struct lockref *lockref)
210 spin_lock(&lockref->lock);
212 if (lockref->count >= 0) {
216 spin_unlock(&lockref->lock);
219 EXPORT_SYMBOL(lockref_get_not_dead);