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 { \
13 BUILD_BUG_ON(sizeof(old) != 8); \
14 old.lock_count = READ_ONCE(lockref->lock_count); \
15 while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) { \
16 struct lockref new = old, prev = old; \
18 old.lock_count = cmpxchg64_relaxed(&lockref->lock_count, \
21 if (likely(old.lock_count == prev.lock_count)) { \
30 #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
35 * lockref_get - Increments reference count unconditionally
36 * @lockref: pointer to lockref structure
38 * This operation is only valid if you already hold a reference
39 * to the object, so you know the count cannot be zero.
41 void lockref_get(struct lockref *lockref)
49 spin_lock(&lockref->lock);
51 spin_unlock(&lockref->lock);
53 EXPORT_SYMBOL(lockref_get);
56 * lockref_get_not_zero - Increments count unless the count is 0 or dead
57 * @lockref: pointer to lockref structure
58 * Return: 1 if count updated successfully or 0 if count was zero
60 int lockref_get_not_zero(struct lockref *lockref)
72 spin_lock(&lockref->lock);
74 if (lockref->count > 0) {
78 spin_unlock(&lockref->lock);
81 EXPORT_SYMBOL(lockref_get_not_zero);
84 * lockref_get_or_lock - Increments count unless the count is 0 or dead
85 * @lockref: pointer to lockref structure
86 * Return: 1 if count updated successfully or 0 if count was zero
87 * and we got the lock instead.
89 int lockref_get_or_lock(struct lockref *lockref)
99 spin_lock(&lockref->lock);
100 if (lockref->count <= 0)
103 spin_unlock(&lockref->lock);
106 EXPORT_SYMBOL(lockref_get_or_lock);
109 * lockref_put_return - Decrement reference count if possible
110 * @lockref: pointer to lockref structure
112 * Decrement the reference count and return the new value.
113 * If the lockref was dead or locked, return an error.
115 int lockref_put_return(struct lockref *lockref)
126 EXPORT_SYMBOL(lockref_put_return);
129 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
130 * @lockref: pointer to lockref structure
131 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
133 int lockref_put_or_lock(struct lockref *lockref)
143 spin_lock(&lockref->lock);
144 if (lockref->count <= 1)
147 spin_unlock(&lockref->lock);
150 EXPORT_SYMBOL(lockref_put_or_lock);
153 * lockref_mark_dead - mark lockref dead
154 * @lockref: pointer to lockref structure
156 void lockref_mark_dead(struct lockref *lockref)
158 assert_spin_locked(&lockref->lock);
159 lockref->count = -128;
161 EXPORT_SYMBOL(lockref_mark_dead);
164 * lockref_get_not_dead - Increments count unless the ref is dead
165 * @lockref: pointer to lockref structure
166 * Return: 1 if count updated successfully or 0 if lockref was dead
168 int lockref_get_not_dead(struct lockref *lockref)
180 spin_lock(&lockref->lock);
182 if (lockref->count >= 0) {
186 spin_unlock(&lockref->lock);
189 EXPORT_SYMBOL(lockref_get_not_dead);