1 #include <linux/export.h>
2 #include <linux/lockref.h>
4 #ifdef CONFIG_CMPXCHG_LOCKREF
7 * Allow weakly-ordered memory architectures to provide barrier-less
8 * cmpxchg semantics for lockref updates.
10 #ifndef cmpxchg64_relaxed
11 # define cmpxchg64_relaxed cmpxchg64
15 * Note that the "cmpxchg()" reloads the "old" value for the
18 #define CMPXCHG_LOOP(CODE, SUCCESS) do { \
20 BUILD_BUG_ON(sizeof(old) != 8); \
21 old.lock_count = ACCESS_ONCE(lockref->lock_count); \
22 while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) { \
23 struct lockref new = old, prev = old; \
25 old.lock_count = cmpxchg64_relaxed(&lockref->lock_count, \
28 if (likely(old.lock_count == prev.lock_count)) { \
37 #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
42 * lockref_get - Increments reference count unconditionally
43 * @lockref: pointer to lockref structure
45 * This operation is only valid if you already hold a reference
46 * to the object, so you know the count cannot be zero.
48 void lockref_get(struct lockref *lockref)
56 spin_lock(&lockref->lock);
58 spin_unlock(&lockref->lock);
60 EXPORT_SYMBOL(lockref_get);
63 * lockref_get_not_zero - Increments count unless the count is 0
64 * @lockref: pointer to lockref structure
65 * Return: 1 if count updated successfully or 0 if count was zero
67 int lockref_get_not_zero(struct lockref *lockref)
79 spin_lock(&lockref->lock);
85 spin_unlock(&lockref->lock);
88 EXPORT_SYMBOL(lockref_get_not_zero);
91 * lockref_get_or_lock - Increments count unless the count is 0
92 * @lockref: pointer to lockref structure
93 * Return: 1 if count updated successfully or 0 if count was zero
94 * and we got the lock instead.
96 int lockref_get_or_lock(struct lockref *lockref)
106 spin_lock(&lockref->lock);
110 spin_unlock(&lockref->lock);
113 EXPORT_SYMBOL(lockref_get_or_lock);
116 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
117 * @lockref: pointer to lockref structure
118 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
120 int lockref_put_or_lock(struct lockref *lockref)
130 spin_lock(&lockref->lock);
131 if (lockref->count <= 1)
134 spin_unlock(&lockref->lock);
137 EXPORT_SYMBOL(lockref_put_or_lock);
140 * lockref_mark_dead - mark lockref dead
141 * @lockref: pointer to lockref structure
143 void lockref_mark_dead(struct lockref *lockref)
145 assert_spin_locked(&lockref->lock);
146 lockref->count = -128;
150 * lockref_get_not_dead - Increments count unless the ref is dead
151 * @lockref: pointer to lockref structure
152 * Return: 1 if count updated successfully or 0 if lockref was dead
154 int lockref_get_not_dead(struct lockref *lockref)
160 if ((int)old.count < 0)
166 spin_lock(&lockref->lock);
168 if ((int) lockref->count >= 0) {
172 spin_unlock(&lockref->lock);
175 EXPORT_SYMBOL(lockref_get_not_dead);