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, prev = old; \
19 old.lock_count = cmpxchg64_relaxed(&lockref->lock_count, \
22 if (likely(old.lock_count == prev.lock_count)) { \
33 #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
38 * lockref_get - Increments reference count unconditionally
39 * @lockref: pointer to lockref structure
41 * This operation is only valid if you already hold a reference
42 * to the object, so you know the count cannot be zero.
44 void lockref_get(struct lockref *lockref)
52 spin_lock(&lockref->lock);
54 spin_unlock(&lockref->lock);
56 EXPORT_SYMBOL(lockref_get);
59 * lockref_get_not_zero - Increments count unless the count is 0 or dead
60 * @lockref: pointer to lockref structure
61 * Return: 1 if count updated successfully or 0 if count was zero
63 int lockref_get_not_zero(struct lockref *lockref)
75 spin_lock(&lockref->lock);
77 if (lockref->count > 0) {
81 spin_unlock(&lockref->lock);
84 EXPORT_SYMBOL(lockref_get_not_zero);
87 * lockref_put_not_zero - Decrements count unless count <= 1 before decrement
88 * @lockref: pointer to lockref structure
89 * Return: 1 if count updated successfully or 0 if count would become zero
91 int lockref_put_not_zero(struct lockref *lockref)
103 spin_lock(&lockref->lock);
105 if (lockref->count > 1) {
109 spin_unlock(&lockref->lock);
112 EXPORT_SYMBOL(lockref_put_not_zero);
115 * lockref_get_or_lock - Increments count unless the count is 0 or dead
116 * @lockref: pointer to lockref structure
117 * Return: 1 if count updated successfully or 0 if count was zero
118 * and we got the lock instead.
120 int lockref_get_or_lock(struct lockref *lockref)
130 spin_lock(&lockref->lock);
131 if (lockref->count <= 0)
134 spin_unlock(&lockref->lock);
137 EXPORT_SYMBOL(lockref_get_or_lock);
140 * lockref_put_return - Decrement reference count if possible
141 * @lockref: pointer to lockref structure
143 * Decrement the reference count and return the new value.
144 * If the lockref was dead or locked, return an error.
146 int lockref_put_return(struct lockref *lockref)
157 EXPORT_SYMBOL(lockref_put_return);
160 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
161 * @lockref: pointer to lockref structure
162 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
164 int lockref_put_or_lock(struct lockref *lockref)
174 spin_lock(&lockref->lock);
175 if (lockref->count <= 1)
178 spin_unlock(&lockref->lock);
181 EXPORT_SYMBOL(lockref_put_or_lock);
184 * lockref_mark_dead - mark lockref dead
185 * @lockref: pointer to lockref structure
187 void lockref_mark_dead(struct lockref *lockref)
189 assert_spin_locked(&lockref->lock);
190 lockref->count = -128;
192 EXPORT_SYMBOL(lockref_mark_dead);
195 * lockref_get_not_dead - Increments count unless the ref is dead
196 * @lockref: pointer to lockref structure
197 * Return: 1 if count updated successfully or 0 if lockref was dead
199 int lockref_get_not_dead(struct lockref *lockref)
211 spin_lock(&lockref->lock);
213 if (lockref->count >= 0) {
217 spin_unlock(&lockref->lock);
220 EXPORT_SYMBOL(lockref_get_not_dead);