blob: 81ac5f3552428ce9c74c1ce9dafaa729295e9319 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds2f4f12e2013-09-02 11:58:20 -07002#include <linux/export.h>
3#include <linux/lockref.h>
4
Peter Zijlstra57f42572013-11-14 14:31:54 -08005#if USE_CMPXCHG_LOCKREF
Linus Torvaldsbc08b442013-09-02 12:12:15 -07006
7/*
8 * Note that the "cmpxchg()" reloads the "old" value for the
9 * failure case.
10 */
11#define CMPXCHG_LOOP(CODE, SUCCESS) do { \
Jan Glauber893a7d32019-06-05 15:48:49 +020012 int retry = 100; \
Linus Torvaldsbc08b442013-09-02 12:12:15 -070013 struct lockref old; \
14 BUILD_BUG_ON(sizeof(old) != 8); \
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -080015 old.lock_count = READ_ONCE(lockref->lock_count); \
Linus Torvaldsbc08b442013-09-02 12:12:15 -070016 while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) { \
17 struct lockref new = old, prev = old; \
18 CODE \
Will Deacond2212b42013-09-26 17:27:00 +010019 old.lock_count = cmpxchg64_relaxed(&lockref->lock_count, \
20 old.lock_count, \
21 new.lock_count); \
Linus Torvaldsbc08b442013-09-02 12:12:15 -070022 if (likely(old.lock_count == prev.lock_count)) { \
23 SUCCESS; \
24 } \
Jan Glauber893a7d32019-06-05 15:48:49 +020025 if (!--retry) \
26 break; \
Linus Torvaldsbc08b442013-09-02 12:12:15 -070027 } \
28} while (0)
29
30#else
31
32#define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
33
34#endif
35
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070036/**
37 * lockref_get - Increments reference count unconditionally
Linus Torvalds44a0cf92013-09-07 15:30:29 -070038 * @lockref: pointer to lockref structure
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070039 *
40 * This operation is only valid if you already hold a reference
41 * to the object, so you know the count cannot be zero.
42 */
43void lockref_get(struct lockref *lockref)
44{
Linus Torvaldsbc08b442013-09-02 12:12:15 -070045 CMPXCHG_LOOP(
46 new.count++;
47 ,
48 return;
49 );
50
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070051 spin_lock(&lockref->lock);
52 lockref->count++;
53 spin_unlock(&lockref->lock);
54}
55EXPORT_SYMBOL(lockref_get);
56
57/**
Linus Torvalds360f5472015-01-09 15:19:03 -080058 * lockref_get_not_zero - Increments count unless the count is 0 or dead
Linus Torvalds44a0cf92013-09-07 15:30:29 -070059 * @lockref: pointer to lockref structure
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070060 * Return: 1 if count updated successfully or 0 if count was zero
61 */
62int lockref_get_not_zero(struct lockref *lockref)
63{
Linus Torvaldsbc08b442013-09-02 12:12:15 -070064 int retval;
65
66 CMPXCHG_LOOP(
67 new.count++;
Linus Torvalds360f5472015-01-09 15:19:03 -080068 if (old.count <= 0)
Linus Torvaldsbc08b442013-09-02 12:12:15 -070069 return 0;
70 ,
71 return 1;
72 );
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070073
74 spin_lock(&lockref->lock);
Linus Torvaldsbc08b442013-09-02 12:12:15 -070075 retval = 0;
Linus Torvalds360f5472015-01-09 15:19:03 -080076 if (lockref->count > 0) {
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070077 lockref->count++;
78 retval = 1;
79 }
80 spin_unlock(&lockref->lock);
81 return retval;
82}
83EXPORT_SYMBOL(lockref_get_not_zero);
84
85/**
Andreas Gruenbacher450b1f62018-03-29 08:07:46 +010086 * 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
89 */
90int lockref_put_not_zero(struct lockref *lockref)
91{
92 int retval;
93
94 CMPXCHG_LOOP(
95 new.count--;
96 if (old.count <= 1)
97 return 0;
98 ,
99 return 1;
100 );
101
102 spin_lock(&lockref->lock);
103 retval = 0;
104 if (lockref->count > 1) {
105 lockref->count--;
106 retval = 1;
107 }
108 spin_unlock(&lockref->lock);
109 return retval;
110}
111EXPORT_SYMBOL(lockref_put_not_zero);
112
113/**
Linus Torvalds360f5472015-01-09 15:19:03 -0800114 * lockref_get_or_lock - Increments count unless the count is 0 or dead
Linus Torvalds44a0cf92013-09-07 15:30:29 -0700115 * @lockref: pointer to lockref structure
Linus Torvalds2f4f12e2013-09-02 11:58:20 -0700116 * Return: 1 if count updated successfully or 0 if count was zero
117 * and we got the lock instead.
118 */
119int lockref_get_or_lock(struct lockref *lockref)
120{
Linus Torvaldsbc08b442013-09-02 12:12:15 -0700121 CMPXCHG_LOOP(
122 new.count++;
Linus Torvalds360f5472015-01-09 15:19:03 -0800123 if (old.count <= 0)
Linus Torvaldsbc08b442013-09-02 12:12:15 -0700124 break;
125 ,
126 return 1;
127 );
128
Linus Torvalds2f4f12e2013-09-02 11:58:20 -0700129 spin_lock(&lockref->lock);
Linus Torvalds360f5472015-01-09 15:19:03 -0800130 if (lockref->count <= 0)
Linus Torvalds2f4f12e2013-09-02 11:58:20 -0700131 return 0;
132 lockref->count++;
133 spin_unlock(&lockref->lock);
134 return 1;
135}
136EXPORT_SYMBOL(lockref_get_or_lock);
137
138/**
Linus Torvalds360f5472015-01-09 15:19:03 -0800139 * lockref_put_return - Decrement reference count if possible
140 * @lockref: pointer to lockref structure
141 *
142 * Decrement the reference count and return the new value.
143 * If the lockref was dead or locked, return an error.
144 */
145int lockref_put_return(struct lockref *lockref)
146{
147 CMPXCHG_LOOP(
148 new.count--;
149 if (old.count <= 0)
150 return -1;
151 ,
152 return new.count;
153 );
154 return -1;
155}
156EXPORT_SYMBOL(lockref_put_return);
157
158/**
Linus Torvalds2f4f12e2013-09-02 11:58:20 -0700159 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
Linus Torvalds44a0cf92013-09-07 15:30:29 -0700160 * @lockref: pointer to lockref structure
Linus Torvalds2f4f12e2013-09-02 11:58:20 -0700161 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
162 */
163int lockref_put_or_lock(struct lockref *lockref)
164{
Linus Torvaldsbc08b442013-09-02 12:12:15 -0700165 CMPXCHG_LOOP(
166 new.count--;
167 if (old.count <= 1)
168 break;
169 ,
170 return 1;
171 );
172
Linus Torvalds2f4f12e2013-09-02 11:58:20 -0700173 spin_lock(&lockref->lock);
174 if (lockref->count <= 1)
175 return 0;
176 lockref->count--;
177 spin_unlock(&lockref->lock);
178 return 1;
179}
180EXPORT_SYMBOL(lockref_put_or_lock);
Linus Torvaldse7d33bb2013-09-07 15:49:18 -0700181
182/**
183 * lockref_mark_dead - mark lockref dead
184 * @lockref: pointer to lockref structure
185 */
186void lockref_mark_dead(struct lockref *lockref)
187{
188 assert_spin_locked(&lockref->lock);
189 lockref->count = -128;
190}
Steven Whitehousee66cf162013-10-15 15:18:08 +0100191EXPORT_SYMBOL(lockref_mark_dead);
Linus Torvaldse7d33bb2013-09-07 15:49:18 -0700192
193/**
194 * lockref_get_not_dead - Increments count unless the ref is dead
195 * @lockref: pointer to lockref structure
196 * Return: 1 if count updated successfully or 0 if lockref was dead
197 */
198int lockref_get_not_dead(struct lockref *lockref)
199{
200 int retval;
201
202 CMPXCHG_LOOP(
203 new.count++;
Linus Torvalds360f5472015-01-09 15:19:03 -0800204 if (old.count < 0)
Linus Torvaldse7d33bb2013-09-07 15:49:18 -0700205 return 0;
206 ,
207 return 1;
208 );
209
210 spin_lock(&lockref->lock);
211 retval = 0;
Linus Torvalds360f5472015-01-09 15:19:03 -0800212 if (lockref->count >= 0) {
Linus Torvaldse7d33bb2013-09-07 15:49:18 -0700213 lockref->count++;
214 retval = 1;
215 }
216 spin_unlock(&lockref->lock);
217 return retval;
218}
219EXPORT_SYMBOL(lockref_get_not_dead);