Merge tag 'pull-work.fd-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-block.git] / lib / lockref.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
2f4f12e5
LT
2#include <linux/export.h>
3#include <linux/lockref.h>
4
57f4257e 5#if USE_CMPXCHG_LOCKREF
bc08b449
LT
6
7/*
8 * Note that the "cmpxchg()" reloads the "old" value for the
9 * failure case.
10 */
11#define CMPXCHG_LOOP(CODE, SUCCESS) do { \
893a7d32 12 int retry = 100; \
bc08b449
LT
13 struct lockref old; \
14 BUILD_BUG_ON(sizeof(old) != 8); \
4d3199e4 15 old.lock_count = READ_ONCE(lockref->lock_count); \
bc08b449 16 while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) { \
3378323b 17 struct lockref new = old; \
bc08b449 18 CODE \
3378323b
UB
19 if (likely(try_cmpxchg64_relaxed(&lockref->lock_count, \
20 &old.lock_count, \
21 new.lock_count))) { \
bc08b449
LT
22 SUCCESS; \
23 } \
893a7d32
JG
24 if (!--retry) \
25 break; \
f2f09a4c 26 cpu_relax(); \
bc08b449
LT
27 } \
28} while (0)
29
30#else
31
32#define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
33
34#endif
35
2f4f12e5
LT
36/**
37 * lockref_get - Increments reference count unconditionally
44a0cf92 38 * @lockref: pointer to lockref structure
2f4f12e5
LT
39 *
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{
bc08b449
LT
45 CMPXCHG_LOOP(
46 new.count++;
47 ,
48 return;
49 );
50
2f4f12e5
LT
51 spin_lock(&lockref->lock);
52 lockref->count++;
53 spin_unlock(&lockref->lock);
54}
55EXPORT_SYMBOL(lockref_get);
56
57/**
360f5479 58 * lockref_get_not_zero - Increments count unless the count is 0 or dead
44a0cf92 59 * @lockref: pointer to lockref structure
2f4f12e5
LT
60 * Return: 1 if count updated successfully or 0 if count was zero
61 */
62int lockref_get_not_zero(struct lockref *lockref)
63{
bc08b449
LT
64 int retval;
65
66 CMPXCHG_LOOP(
67 new.count++;
360f5479 68 if (old.count <= 0)
bc08b449
LT
69 return 0;
70 ,
71 return 1;
72 );
2f4f12e5
LT
73
74 spin_lock(&lockref->lock);
bc08b449 75 retval = 0;
360f5479 76 if (lockref->count > 0) {
2f4f12e5
LT
77 lockref->count++;
78 retval = 1;
79 }
80 spin_unlock(&lockref->lock);
81 return retval;
82}
83EXPORT_SYMBOL(lockref_get_not_zero);
84
450b1f6f
AG
85/**
86 * 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
2f4f12e5 113/**
360f5479 114 * lockref_get_or_lock - Increments count unless the count is 0 or dead
44a0cf92 115 * @lockref: pointer to lockref structure
2f4f12e5
LT
116 * 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{
bc08b449
LT
121 CMPXCHG_LOOP(
122 new.count++;
360f5479 123 if (old.count <= 0)
bc08b449
LT
124 break;
125 ,
126 return 1;
127 );
128
2f4f12e5 129 spin_lock(&lockref->lock);
360f5479 130 if (lockref->count <= 0)
2f4f12e5
LT
131 return 0;
132 lockref->count++;
133 spin_unlock(&lockref->lock);
134 return 1;
135}
136EXPORT_SYMBOL(lockref_get_or_lock);
137
360f5479
LT
138/**
139 * 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
2f4f12e5
LT
158/**
159 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
44a0cf92 160 * @lockref: pointer to lockref structure
2f4f12e5
LT
161 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
162 */
163int lockref_put_or_lock(struct lockref *lockref)
164{
bc08b449
LT
165 CMPXCHG_LOOP(
166 new.count--;
167 if (old.count <= 1)
168 break;
169 ,
170 return 1;
171 );
172
2f4f12e5
LT
173 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);
e7d33bb5
LT
181
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}
e66cf161 191EXPORT_SYMBOL(lockref_mark_dead);
e7d33bb5
LT
192
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++;
360f5479 204 if (old.count < 0)
e7d33bb5
LT
205 return 0;
206 ,
207 return 1;
208 );
209
210 spin_lock(&lockref->lock);
211 retval = 0;
360f5479 212 if (lockref->count >= 0) {
e7d33bb5
LT
213 lockref->count++;
214 retval = 1;
215 }
216 spin_unlock(&lockref->lock);
217 return retval;
218}
219EXPORT_SYMBOL(lockref_get_not_dead);