1 #include <linux/export.h>
2 #include <linux/lockref.h>
4 #if USE_CMPXCHG_LOCKREF
7 * Note that the "cmpxchg()" reloads the "old" value for the
10 #define CMPXCHG_LOOP(CODE, SUCCESS) do { \
12 BUILD_BUG_ON(sizeof(old) != 8); \
13 old.lock_count = READ_ONCE(lockref->lock_count); \
14 while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) { \
15 struct lockref new = old, prev = old; \
17 old.lock_count = cmpxchg64_relaxed(&lockref->lock_count, \
20 if (likely(old.lock_count == prev.lock_count)) { \
29 #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
34 * lockref_get - Increments reference count unconditionally
35 * @lockref: pointer to lockref structure
37 * This operation is only valid if you already hold a reference
38 * to the object, so you know the count cannot be zero.
40 void lockref_get(struct lockref
*lockref
)
48 spin_lock(&lockref
->lock
);
50 spin_unlock(&lockref
->lock
);
52 EXPORT_SYMBOL(lockref_get
);
55 * lockref_get_not_zero - Increments count unless the count is 0 or dead
56 * @lockref: pointer to lockref structure
57 * Return: 1 if count updated successfully or 0 if count was zero
59 int lockref_get_not_zero(struct lockref
*lockref
)
71 spin_lock(&lockref
->lock
);
73 if (lockref
->count
> 0) {
77 spin_unlock(&lockref
->lock
);
80 EXPORT_SYMBOL(lockref_get_not_zero
);
83 * lockref_get_or_lock - Increments count unless the count is 0 or dead
84 * @lockref: pointer to lockref structure
85 * Return: 1 if count updated successfully or 0 if count was zero
86 * and we got the lock instead.
88 int lockref_get_or_lock(struct lockref
*lockref
)
98 spin_lock(&lockref
->lock
);
99 if (lockref
->count
<= 0)
102 spin_unlock(&lockref
->lock
);
105 EXPORT_SYMBOL(lockref_get_or_lock
);
108 * lockref_put_return - Decrement reference count if possible
109 * @lockref: pointer to lockref structure
111 * Decrement the reference count and return the new value.
112 * If the lockref was dead or locked, return an error.
114 int lockref_put_return(struct lockref
*lockref
)
125 EXPORT_SYMBOL(lockref_put_return
);
128 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
129 * @lockref: pointer to lockref structure
130 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
132 int lockref_put_or_lock(struct lockref
*lockref
)
142 spin_lock(&lockref
->lock
);
143 if (lockref
->count
<= 1)
146 spin_unlock(&lockref
->lock
);
149 EXPORT_SYMBOL(lockref_put_or_lock
);
152 * lockref_mark_dead - mark lockref dead
153 * @lockref: pointer to lockref structure
155 void lockref_mark_dead(struct lockref
*lockref
)
157 assert_spin_locked(&lockref
->lock
);
158 lockref
->count
= -128;
160 EXPORT_SYMBOL(lockref_mark_dead
);
163 * lockref_get_not_dead - Increments count unless the ref is dead
164 * @lockref: pointer to lockref structure
165 * Return: 1 if count updated successfully or 0 if lockref was dead
167 int lockref_get_not_dead(struct lockref
*lockref
)
179 spin_lock(&lockref
->lock
);
181 if (lockref
->count
>= 0) {
185 spin_unlock(&lockref
->lock
);
188 EXPORT_SYMBOL(lockref_get_not_dead
);