Merge tag 'thermal-6.1-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-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
360f5479
LT
113/**
114 * lockref_put_return - Decrement reference count if possible
115 * @lockref: pointer to lockref structure
116 *
117 * Decrement the reference count and return the new value.
118 * If the lockref was dead or locked, return an error.
119 */
120int lockref_put_return(struct lockref *lockref)
121{
122 CMPXCHG_LOOP(
123 new.count--;
124 if (old.count <= 0)
125 return -1;
126 ,
127 return new.count;
128 );
129 return -1;
130}
131EXPORT_SYMBOL(lockref_put_return);
132
2f4f12e5
LT
133/**
134 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
44a0cf92 135 * @lockref: pointer to lockref structure
2f4f12e5
LT
136 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
137 */
138int lockref_put_or_lock(struct lockref *lockref)
139{
bc08b449
LT
140 CMPXCHG_LOOP(
141 new.count--;
142 if (old.count <= 1)
143 break;
144 ,
145 return 1;
146 );
147
2f4f12e5
LT
148 spin_lock(&lockref->lock);
149 if (lockref->count <= 1)
150 return 0;
151 lockref->count--;
152 spin_unlock(&lockref->lock);
153 return 1;
154}
155EXPORT_SYMBOL(lockref_put_or_lock);
e7d33bb5
LT
156
157/**
158 * lockref_mark_dead - mark lockref dead
159 * @lockref: pointer to lockref structure
160 */
161void lockref_mark_dead(struct lockref *lockref)
162{
163 assert_spin_locked(&lockref->lock);
164 lockref->count = -128;
165}
e66cf161 166EXPORT_SYMBOL(lockref_mark_dead);
e7d33bb5
LT
167
168/**
169 * lockref_get_not_dead - Increments count unless the ref is dead
170 * @lockref: pointer to lockref structure
171 * Return: 1 if count updated successfully or 0 if lockref was dead
172 */
173int lockref_get_not_dead(struct lockref *lockref)
174{
175 int retval;
176
177 CMPXCHG_LOOP(
178 new.count++;
360f5479 179 if (old.count < 0)
e7d33bb5
LT
180 return 0;
181 ,
182 return 1;
183 );
184
185 spin_lock(&lockref->lock);
186 retval = 0;
360f5479 187 if (lockref->count >= 0) {
e7d33bb5
LT
188 lockref->count++;
189 retval = 1;
190 }
191 spin_unlock(&lockref->lock);
192 return retval;
193}
194EXPORT_SYMBOL(lockref_get_not_dead);