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_put_return - Decrement reference count if possible
115 * @lockref: pointer to lockref structure
117 * Decrement the reference count and return the new value.
118 * If the lockref was dead or locked, return an error.
120 int lockref_put_return(struct lockref *lockref)
131 EXPORT_SYMBOL(lockref_put_return);
134 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
135 * @lockref: pointer to lockref structure
136 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
138 int lockref_put_or_lock(struct lockref *lockref)
148 spin_lock(&lockref->lock);
149 if (lockref->count <= 1)
152 spin_unlock(&lockref->lock);
155 EXPORT_SYMBOL(lockref_put_or_lock);
158 * lockref_mark_dead - mark lockref dead
159 * @lockref: pointer to lockref structure
161 void lockref_mark_dead(struct lockref *lockref)
163 assert_spin_locked(&lockref->lock);
164 lockref->count = -128;
166 EXPORT_SYMBOL(lockref_mark_dead);
169 * lockref_get_not_dead - Increments count unless the ref is dead
170 * @lockref: pointer to lockref structure
171 * Return: 1 if count updated successfully or 0 if lockref was dead
173 int lockref_get_not_dead(struct lockref *lockref)
185 spin_lock(&lockref->lock);
187 if (lockref->count >= 0) {
191 spin_unlock(&lockref->lock);
194 EXPORT_SYMBOL(lockref_get_not_dead);