Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 1 | #include <linux/export.h> |
| 2 | #include <linux/lockref.h> |
| 3 | |
Peter Zijlstra | 57f4257 | 2013-11-14 14:31:54 -0800 | [diff] [blame] | 4 | #if USE_CMPXCHG_LOCKREF |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 5 | |
| 6 | /* |
Will Deacon | d2212b4 | 2013-09-26 17:27:00 +0100 | [diff] [blame] | 7 | * Allow weakly-ordered memory architectures to provide barrier-less |
| 8 | * cmpxchg semantics for lockref updates. |
| 9 | */ |
| 10 | #ifndef cmpxchg64_relaxed |
| 11 | # define cmpxchg64_relaxed cmpxchg64 |
| 12 | #endif |
| 13 | |
| 14 | /* |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 15 | * Note that the "cmpxchg()" reloads the "old" value for the |
| 16 | * failure case. |
| 17 | */ |
| 18 | #define CMPXCHG_LOOP(CODE, SUCCESS) do { \ |
| 19 | struct lockref old; \ |
| 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; \ |
| 24 | CODE \ |
Will Deacon | d2212b4 | 2013-09-26 17:27:00 +0100 | [diff] [blame] | 25 | old.lock_count = cmpxchg64_relaxed(&lockref->lock_count, \ |
| 26 | old.lock_count, \ |
| 27 | new.lock_count); \ |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 28 | if (likely(old.lock_count == prev.lock_count)) { \ |
| 29 | SUCCESS; \ |
| 30 | } \ |
Davidlohr Bueso | 3a6bfbc | 2014-06-29 15:09:33 -0700 | [diff] [blame] | 31 | cpu_relax_lowlatency(); \ |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 32 | } \ |
| 33 | } while (0) |
| 34 | |
| 35 | #else |
| 36 | |
| 37 | #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0) |
| 38 | |
| 39 | #endif |
| 40 | |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 41 | /** |
| 42 | * lockref_get - Increments reference count unconditionally |
Linus Torvalds | 44a0cf9 | 2013-09-07 15:30:29 -0700 | [diff] [blame] | 43 | * @lockref: pointer to lockref structure |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 44 | * |
| 45 | * This operation is only valid if you already hold a reference |
| 46 | * to the object, so you know the count cannot be zero. |
| 47 | */ |
| 48 | void lockref_get(struct lockref *lockref) |
| 49 | { |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 50 | CMPXCHG_LOOP( |
| 51 | new.count++; |
| 52 | , |
| 53 | return; |
| 54 | ); |
| 55 | |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 56 | spin_lock(&lockref->lock); |
| 57 | lockref->count++; |
| 58 | spin_unlock(&lockref->lock); |
| 59 | } |
| 60 | EXPORT_SYMBOL(lockref_get); |
| 61 | |
| 62 | /** |
Linus Torvalds | 360f547 | 2015-01-09 15:19:03 -0800 | [diff] [blame] | 63 | * lockref_get_not_zero - Increments count unless the count is 0 or dead |
Linus Torvalds | 44a0cf9 | 2013-09-07 15:30:29 -0700 | [diff] [blame] | 64 | * @lockref: pointer to lockref structure |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 65 | * Return: 1 if count updated successfully or 0 if count was zero |
| 66 | */ |
| 67 | int lockref_get_not_zero(struct lockref *lockref) |
| 68 | { |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 69 | int retval; |
| 70 | |
| 71 | CMPXCHG_LOOP( |
| 72 | new.count++; |
Linus Torvalds | 360f547 | 2015-01-09 15:19:03 -0800 | [diff] [blame] | 73 | if (old.count <= 0) |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 74 | return 0; |
| 75 | , |
| 76 | return 1; |
| 77 | ); |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 78 | |
| 79 | spin_lock(&lockref->lock); |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 80 | retval = 0; |
Linus Torvalds | 360f547 | 2015-01-09 15:19:03 -0800 | [diff] [blame] | 81 | if (lockref->count > 0) { |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 82 | lockref->count++; |
| 83 | retval = 1; |
| 84 | } |
| 85 | spin_unlock(&lockref->lock); |
| 86 | return retval; |
| 87 | } |
| 88 | EXPORT_SYMBOL(lockref_get_not_zero); |
| 89 | |
| 90 | /** |
Linus Torvalds | 360f547 | 2015-01-09 15:19:03 -0800 | [diff] [blame] | 91 | * lockref_get_or_lock - Increments count unless the count is 0 or dead |
Linus Torvalds | 44a0cf9 | 2013-09-07 15:30:29 -0700 | [diff] [blame] | 92 | * @lockref: pointer to lockref structure |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 93 | * Return: 1 if count updated successfully or 0 if count was zero |
| 94 | * and we got the lock instead. |
| 95 | */ |
| 96 | int lockref_get_or_lock(struct lockref *lockref) |
| 97 | { |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 98 | CMPXCHG_LOOP( |
| 99 | new.count++; |
Linus Torvalds | 360f547 | 2015-01-09 15:19:03 -0800 | [diff] [blame] | 100 | if (old.count <= 0) |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 101 | break; |
| 102 | , |
| 103 | return 1; |
| 104 | ); |
| 105 | |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 106 | spin_lock(&lockref->lock); |
Linus Torvalds | 360f547 | 2015-01-09 15:19:03 -0800 | [diff] [blame] | 107 | if (lockref->count <= 0) |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 108 | return 0; |
| 109 | lockref->count++; |
| 110 | spin_unlock(&lockref->lock); |
| 111 | return 1; |
| 112 | } |
| 113 | EXPORT_SYMBOL(lockref_get_or_lock); |
| 114 | |
| 115 | /** |
Linus Torvalds | 360f547 | 2015-01-09 15:19:03 -0800 | [diff] [blame] | 116 | * lockref_put_return - Decrement reference count if possible |
| 117 | * @lockref: pointer to lockref structure |
| 118 | * |
| 119 | * Decrement the reference count and return the new value. |
| 120 | * If the lockref was dead or locked, return an error. |
| 121 | */ |
| 122 | int lockref_put_return(struct lockref *lockref) |
| 123 | { |
| 124 | CMPXCHG_LOOP( |
| 125 | new.count--; |
| 126 | if (old.count <= 0) |
| 127 | return -1; |
| 128 | , |
| 129 | return new.count; |
| 130 | ); |
| 131 | return -1; |
| 132 | } |
| 133 | EXPORT_SYMBOL(lockref_put_return); |
| 134 | |
| 135 | /** |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 136 | * lockref_put_or_lock - decrements count unless count <= 1 before decrement |
Linus Torvalds | 44a0cf9 | 2013-09-07 15:30:29 -0700 | [diff] [blame] | 137 | * @lockref: pointer to lockref structure |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 138 | * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken |
| 139 | */ |
| 140 | int lockref_put_or_lock(struct lockref *lockref) |
| 141 | { |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 142 | CMPXCHG_LOOP( |
| 143 | new.count--; |
| 144 | if (old.count <= 1) |
| 145 | break; |
| 146 | , |
| 147 | return 1; |
| 148 | ); |
| 149 | |
Linus Torvalds | 2f4f12e | 2013-09-02 11:58:20 -0700 | [diff] [blame] | 150 | spin_lock(&lockref->lock); |
| 151 | if (lockref->count <= 1) |
| 152 | return 0; |
| 153 | lockref->count--; |
| 154 | spin_unlock(&lockref->lock); |
| 155 | return 1; |
| 156 | } |
| 157 | EXPORT_SYMBOL(lockref_put_or_lock); |
Linus Torvalds | e7d33bb | 2013-09-07 15:49:18 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * lockref_mark_dead - mark lockref dead |
| 161 | * @lockref: pointer to lockref structure |
| 162 | */ |
| 163 | void lockref_mark_dead(struct lockref *lockref) |
| 164 | { |
| 165 | assert_spin_locked(&lockref->lock); |
| 166 | lockref->count = -128; |
| 167 | } |
Steven Whitehouse | e66cf16 | 2013-10-15 15:18:08 +0100 | [diff] [blame] | 168 | EXPORT_SYMBOL(lockref_mark_dead); |
Linus Torvalds | e7d33bb | 2013-09-07 15:49:18 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * lockref_get_not_dead - Increments count unless the ref is dead |
| 172 | * @lockref: pointer to lockref structure |
| 173 | * Return: 1 if count updated successfully or 0 if lockref was dead |
| 174 | */ |
| 175 | int lockref_get_not_dead(struct lockref *lockref) |
| 176 | { |
| 177 | int retval; |
| 178 | |
| 179 | CMPXCHG_LOOP( |
| 180 | new.count++; |
Linus Torvalds | 360f547 | 2015-01-09 15:19:03 -0800 | [diff] [blame] | 181 | if (old.count < 0) |
Linus Torvalds | e7d33bb | 2013-09-07 15:49:18 -0700 | [diff] [blame] | 182 | return 0; |
| 183 | , |
| 184 | return 1; |
| 185 | ); |
| 186 | |
| 187 | spin_lock(&lockref->lock); |
| 188 | retval = 0; |
Linus Torvalds | 360f547 | 2015-01-09 15:19:03 -0800 | [diff] [blame] | 189 | if (lockref->count >= 0) { |
Linus Torvalds | e7d33bb | 2013-09-07 15:49:18 -0700 | [diff] [blame] | 190 | lockref->count++; |
| 191 | retval = 1; |
| 192 | } |
| 193 | spin_unlock(&lockref->lock); |
| 194 | return retval; |
| 195 | } |
| 196 | EXPORT_SYMBOL(lockref_get_not_dead); |