blob: 47169ed7e964f53dbc7735a3ad5b19c492878f45 [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 { \
12 struct lockref old; \
13 BUILD_BUG_ON(sizeof(old) != 8); \
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -080014 old.lock_count = READ_ONCE(lockref->lock_count); \
Linus Torvaldsbc08b442013-09-02 12:12:15 -070015 while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) { \
16 struct lockref new = old, prev = old; \
17 CODE \
Will Deacond2212b42013-09-26 17:27:00 +010018 old.lock_count = cmpxchg64_relaxed(&lockref->lock_count, \
19 old.lock_count, \
20 new.lock_count); \
Linus Torvaldsbc08b442013-09-02 12:12:15 -070021 if (likely(old.lock_count == prev.lock_count)) { \
22 SUCCESS; \
23 } \
Christian Borntraegerf2f09a42016-10-25 11:03:14 +020024 cpu_relax(); \
Linus Torvaldsbc08b442013-09-02 12:12:15 -070025 } \
26} while (0)
27
28#else
29
30#define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
31
32#endif
33
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070034/**
35 * lockref_get - Increments reference count unconditionally
Linus Torvalds44a0cf92013-09-07 15:30:29 -070036 * @lockref: pointer to lockref structure
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070037 *
38 * This operation is only valid if you already hold a reference
39 * to the object, so you know the count cannot be zero.
40 */
41void lockref_get(struct lockref *lockref)
42{
Linus Torvaldsbc08b442013-09-02 12:12:15 -070043 CMPXCHG_LOOP(
44 new.count++;
45 ,
46 return;
47 );
48
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070049 spin_lock(&lockref->lock);
50 lockref->count++;
51 spin_unlock(&lockref->lock);
52}
53EXPORT_SYMBOL(lockref_get);
54
55/**
Linus Torvalds360f5472015-01-09 15:19:03 -080056 * lockref_get_not_zero - Increments count unless the count is 0 or dead
Linus Torvalds44a0cf92013-09-07 15:30:29 -070057 * @lockref: pointer to lockref structure
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070058 * Return: 1 if count updated successfully or 0 if count was zero
59 */
60int lockref_get_not_zero(struct lockref *lockref)
61{
Linus Torvaldsbc08b442013-09-02 12:12:15 -070062 int retval;
63
64 CMPXCHG_LOOP(
65 new.count++;
Linus Torvalds360f5472015-01-09 15:19:03 -080066 if (old.count <= 0)
Linus Torvaldsbc08b442013-09-02 12:12:15 -070067 return 0;
68 ,
69 return 1;
70 );
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070071
72 spin_lock(&lockref->lock);
Linus Torvaldsbc08b442013-09-02 12:12:15 -070073 retval = 0;
Linus Torvalds360f5472015-01-09 15:19:03 -080074 if (lockref->count > 0) {
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070075 lockref->count++;
76 retval = 1;
77 }
78 spin_unlock(&lockref->lock);
79 return retval;
80}
81EXPORT_SYMBOL(lockref_get_not_zero);
82
83/**
Linus Torvalds360f5472015-01-09 15:19:03 -080084 * lockref_get_or_lock - Increments count unless the count is 0 or dead
Linus Torvalds44a0cf92013-09-07 15:30:29 -070085 * @lockref: pointer to lockref structure
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070086 * Return: 1 if count updated successfully or 0 if count was zero
87 * and we got the lock instead.
88 */
89int lockref_get_or_lock(struct lockref *lockref)
90{
Linus Torvaldsbc08b442013-09-02 12:12:15 -070091 CMPXCHG_LOOP(
92 new.count++;
Linus Torvalds360f5472015-01-09 15:19:03 -080093 if (old.count <= 0)
Linus Torvaldsbc08b442013-09-02 12:12:15 -070094 break;
95 ,
96 return 1;
97 );
98
Linus Torvalds2f4f12e2013-09-02 11:58:20 -070099 spin_lock(&lockref->lock);
Linus Torvalds360f5472015-01-09 15:19:03 -0800100 if (lockref->count <= 0)
Linus Torvalds2f4f12e2013-09-02 11:58:20 -0700101 return 0;
102 lockref->count++;
103 spin_unlock(&lockref->lock);
104 return 1;
105}
106EXPORT_SYMBOL(lockref_get_or_lock);
107
108/**
Linus Torvalds360f5472015-01-09 15:19:03 -0800109 * lockref_put_return - Decrement reference count if possible
110 * @lockref: pointer to lockref structure
111 *
112 * Decrement the reference count and return the new value.
113 * If the lockref was dead or locked, return an error.
114 */
115int lockref_put_return(struct lockref *lockref)
116{
117 CMPXCHG_LOOP(
118 new.count--;
119 if (old.count <= 0)
120 return -1;
121 ,
122 return new.count;
123 );
124 return -1;
125}
126EXPORT_SYMBOL(lockref_put_return);
127
128/**
Linus Torvalds2f4f12e2013-09-02 11:58:20 -0700129 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
Linus Torvalds44a0cf92013-09-07 15:30:29 -0700130 * @lockref: pointer to lockref structure
Linus Torvalds2f4f12e2013-09-02 11:58:20 -0700131 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
132 */
133int lockref_put_or_lock(struct lockref *lockref)
134{
Linus Torvaldsbc08b442013-09-02 12:12:15 -0700135 CMPXCHG_LOOP(
136 new.count--;
137 if (old.count <= 1)
138 break;
139 ,
140 return 1;
141 );
142
Linus Torvalds2f4f12e2013-09-02 11:58:20 -0700143 spin_lock(&lockref->lock);
144 if (lockref->count <= 1)
145 return 0;
146 lockref->count--;
147 spin_unlock(&lockref->lock);
148 return 1;
149}
150EXPORT_SYMBOL(lockref_put_or_lock);
Linus Torvaldse7d33bb2013-09-07 15:49:18 -0700151
152/**
153 * lockref_mark_dead - mark lockref dead
154 * @lockref: pointer to lockref structure
155 */
156void lockref_mark_dead(struct lockref *lockref)
157{
158 assert_spin_locked(&lockref->lock);
159 lockref->count = -128;
160}
Steven Whitehousee66cf162013-10-15 15:18:08 +0100161EXPORT_SYMBOL(lockref_mark_dead);
Linus Torvaldse7d33bb2013-09-07 15:49:18 -0700162
163/**
164 * lockref_get_not_dead - Increments count unless the ref is dead
165 * @lockref: pointer to lockref structure
166 * Return: 1 if count updated successfully or 0 if lockref was dead
167 */
168int lockref_get_not_dead(struct lockref *lockref)
169{
170 int retval;
171
172 CMPXCHG_LOOP(
173 new.count++;
Linus Torvalds360f5472015-01-09 15:19:03 -0800174 if (old.count < 0)
Linus Torvaldse7d33bb2013-09-07 15:49:18 -0700175 return 0;
176 ,
177 return 1;
178 );
179
180 spin_lock(&lockref->lock);
181 retval = 0;
Linus Torvalds360f5472015-01-09 15:19:03 -0800182 if (lockref->count >= 0) {
Linus Torvaldse7d33bb2013-09-07 15:49:18 -0700183 lockref->count++;
184 retval = 1;
185 }
186 spin_unlock(&lockref->lock);
187 return retval;
188}
189EXPORT_SYMBOL(lockref_get_not_dead);