]> Git Repo - linux.git/blame - lib/lockref.c
Merge tag 'devicetree-fixes-for-6.14-1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.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; \
bc08b449
LT
26 } \
27} while (0)
28
29#else
30
31#define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
32
33#endif
34
2f4f12e5
LT
35/**
36 * lockref_get - Increments reference count unconditionally
44a0cf92 37 * @lockref: pointer to lockref structure
2f4f12e5
LT
38 *
39 * This operation is only valid if you already hold a reference
40 * to the object, so you know the count cannot be zero.
41 */
42void lockref_get(struct lockref *lockref)
43{
bc08b449
LT
44 CMPXCHG_LOOP(
45 new.count++;
46 ,
47 return;
48 );
49
2f4f12e5
LT
50 spin_lock(&lockref->lock);
51 lockref->count++;
52 spin_unlock(&lockref->lock);
53}
54EXPORT_SYMBOL(lockref_get);
55
56/**
360f5479 57 * lockref_get_not_zero - Increments count unless the count is 0 or dead
44a0cf92 58 * @lockref: pointer to lockref structure
2f4f12e5
LT
59 * Return: 1 if count updated successfully or 0 if count was zero
60 */
6d2868d5 61bool lockref_get_not_zero(struct lockref *lockref)
2f4f12e5 62{
6d2868d5 63 bool retval = false;
bc08b449
LT
64
65 CMPXCHG_LOOP(
66 new.count++;
360f5479 67 if (old.count <= 0)
6d2868d5 68 return false;
bc08b449 69 ,
6d2868d5 70 return true;
bc08b449 71 );
2f4f12e5
LT
72
73 spin_lock(&lockref->lock);
360f5479 74 if (lockref->count > 0) {
2f4f12e5 75 lockref->count++;
6d2868d5 76 retval = true;
2f4f12e5
LT
77 }
78 spin_unlock(&lockref->lock);
79 return retval;
80}
81EXPORT_SYMBOL(lockref_get_not_zero);
82
360f5479
LT
83/**
84 * lockref_put_return - Decrement reference count if possible
85 * @lockref: pointer to lockref structure
86 *
87 * Decrement the reference count and return the new value.
d60f2280 88 * If the lockref was dead or locked, return -1.
360f5479
LT
89 */
90int lockref_put_return(struct lockref *lockref)
91{
92 CMPXCHG_LOOP(
93 new.count--;
94 if (old.count <= 0)
95 return -1;
96 ,
97 return new.count;
98 );
99 return -1;
100}
101EXPORT_SYMBOL(lockref_put_return);
102
2f4f12e5
LT
103/**
104 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
44a0cf92 105 * @lockref: pointer to lockref structure
2f4f12e5
LT
106 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
107 */
6d2868d5 108bool lockref_put_or_lock(struct lockref *lockref)
2f4f12e5 109{
bc08b449
LT
110 CMPXCHG_LOOP(
111 new.count--;
112 if (old.count <= 1)
113 break;
114 ,
6d2868d5 115 return true;
bc08b449
LT
116 );
117
2f4f12e5
LT
118 spin_lock(&lockref->lock);
119 if (lockref->count <= 1)
6d2868d5 120 return false;
2f4f12e5
LT
121 lockref->count--;
122 spin_unlock(&lockref->lock);
6d2868d5 123 return true;
2f4f12e5
LT
124}
125EXPORT_SYMBOL(lockref_put_or_lock);
e7d33bb5
LT
126
127/**
128 * lockref_mark_dead - mark lockref dead
129 * @lockref: pointer to lockref structure
130 */
131void lockref_mark_dead(struct lockref *lockref)
132{
133 assert_spin_locked(&lockref->lock);
134 lockref->count = -128;
135}
e66cf161 136EXPORT_SYMBOL(lockref_mark_dead);
e7d33bb5
LT
137
138/**
139 * lockref_get_not_dead - Increments count unless the ref is dead
140 * @lockref: pointer to lockref structure
141 * Return: 1 if count updated successfully or 0 if lockref was dead
142 */
6d2868d5 143bool lockref_get_not_dead(struct lockref *lockref)
e7d33bb5 144{
6d2868d5 145 bool retval = false;
e7d33bb5
LT
146
147 CMPXCHG_LOOP(
148 new.count++;
360f5479 149 if (old.count < 0)
6d2868d5 150 return false;
e7d33bb5 151 ,
6d2868d5 152 return true;
e7d33bb5
LT
153 );
154
155 spin_lock(&lockref->lock);
360f5479 156 if (lockref->count >= 0) {
e7d33bb5 157 lockref->count++;
6d2868d5 158 retval = true;
e7d33bb5
LT
159 }
160 spin_unlock(&lockref->lock);
161 return retval;
162}
163EXPORT_SYMBOL(lockref_get_not_dead);
This page took 0.655245 seconds and 5 git commands to generate.