Skip to content

Commit c859df5

Browse files
committed
Merge patch series "lockref cleanups"
Christoph Hellwig <[email protected]> says: This series has a bunch of cosmetic cleanups for the lockref code I came up with when reading the code in preparation of adding a new user of it. * patches from https://lore.kernel.org/r/[email protected]: gfs2: use lockref_init for qd_lockref erofs: use lockref_init for pcl->lockref dcache: use lockref_init for d_lockref lockref: add a lockref_init helper lockref: drop superfluous externs lockref: use bool for false/true returns lockref: improve the lockref_get_not_zero description lockref: remove lockref_put_not_zero Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
2 parents 4f3b63e + 3e652eb commit c859df5

File tree

5 files changed

+36
-59
lines changed

5 files changed

+36
-59
lines changed

fs/dcache.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,9 +1681,8 @@ static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
16811681
/* Make sure we always see the terminating NUL character */
16821682
smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
16831683

1684-
dentry->d_lockref.count = 1;
16851684
dentry->d_flags = 0;
1686-
spin_lock_init(&dentry->d_lock);
1685+
lockref_init(&dentry->d_lockref, 1);
16871686
seqcount_spinlock_init(&dentry->d_seq, &dentry->d_lock);
16881687
dentry->d_inode = NULL;
16891688
dentry->d_parent = dentry;

fs/erofs/zdata.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,7 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
747747
if (IS_ERR(pcl))
748748
return PTR_ERR(pcl);
749749

750-
spin_lock_init(&pcl->lockref.lock);
751-
pcl->lockref.count = 1; /* one ref for this request */
750+
lockref_init(&pcl->lockref, 1); /* one ref for this request */
752751
pcl->algorithmformat = map->m_algorithmformat;
753752
pcl->length = 0;
754753
pcl->partial = true;

fs/gfs2/quota.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,7 @@ static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, str
236236
return NULL;
237237

238238
qd->qd_sbd = sdp;
239-
qd->qd_lockref.count = 0;
240-
spin_lock_init(&qd->qd_lockref.lock);
239+
lockref_init(&qd->qd_lockref, 0);
241240
qd->qd_id = qid;
242241
qd->qd_slot = -1;
243242
INIT_LIST_HEAD(&qd->qd_lru);

include/linux/lockref.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,24 @@ struct lockref {
3434
};
3535
};
3636

37-
extern void lockref_get(struct lockref *);
38-
extern int lockref_put_return(struct lockref *);
39-
extern int lockref_get_not_zero(struct lockref *);
40-
extern int lockref_put_not_zero(struct lockref *);
41-
extern int lockref_put_or_lock(struct lockref *);
42-
43-
extern void lockref_mark_dead(struct lockref *);
44-
extern int lockref_get_not_dead(struct lockref *);
37+
/**
38+
* lockref_init - Initialize a lockref
39+
* @lockref: pointer to lockref structure
40+
* @count: initial count
41+
*/
42+
static inline void lockref_init(struct lockref *lockref, unsigned int count)
43+
{
44+
spin_lock_init(&lockref->lock);
45+
lockref->count = count;
46+
}
47+
48+
void lockref_get(struct lockref *lockref);
49+
int lockref_put_return(struct lockref *lockref);
50+
bool lockref_get_not_zero(struct lockref *lockref);
51+
bool lockref_put_or_lock(struct lockref *lockref);
52+
53+
void lockref_mark_dead(struct lockref *lockref);
54+
bool lockref_get_not_dead(struct lockref *lockref);
4555

4656
/* Must be called under spinlock for reliable results */
4757
static inline bool __lockref_is_dead(const struct lockref *l)

lib/lockref.c

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -58,63 +58,34 @@ EXPORT_SYMBOL(lockref_get);
5858
* @lockref: pointer to lockref structure
5959
* Return: 1 if count updated successfully or 0 if count was zero
6060
*/
61-
int lockref_get_not_zero(struct lockref *lockref)
61+
bool lockref_get_not_zero(struct lockref *lockref)
6262
{
63-
int retval;
63+
bool retval = false;
6464

6565
CMPXCHG_LOOP(
6666
new.count++;
6767
if (old.count <= 0)
68-
return 0;
68+
return false;
6969
,
70-
return 1;
70+
return true;
7171
);
7272

7373
spin_lock(&lockref->lock);
74-
retval = 0;
7574
if (lockref->count > 0) {
7675
lockref->count++;
77-
retval = 1;
76+
retval = true;
7877
}
7978
spin_unlock(&lockref->lock);
8079
return retval;
8180
}
8281
EXPORT_SYMBOL(lockref_get_not_zero);
8382

84-
/**
85-
* lockref_put_not_zero - Decrements count unless count <= 1 before decrement
86-
* @lockref: pointer to lockref structure
87-
* Return: 1 if count updated successfully or 0 if count would become zero
88-
*/
89-
int lockref_put_not_zero(struct lockref *lockref)
90-
{
91-
int retval;
92-
93-
CMPXCHG_LOOP(
94-
new.count--;
95-
if (old.count <= 1)
96-
return 0;
97-
,
98-
return 1;
99-
);
100-
101-
spin_lock(&lockref->lock);
102-
retval = 0;
103-
if (lockref->count > 1) {
104-
lockref->count--;
105-
retval = 1;
106-
}
107-
spin_unlock(&lockref->lock);
108-
return retval;
109-
}
110-
EXPORT_SYMBOL(lockref_put_not_zero);
111-
11283
/**
11384
* lockref_put_return - Decrement reference count if possible
11485
* @lockref: pointer to lockref structure
11586
*
11687
* Decrement the reference count and return the new value.
117-
* If the lockref was dead or locked, return an error.
88+
* If the lockref was dead or locked, return -1.
11889
*/
11990
int lockref_put_return(struct lockref *lockref)
12091
{
@@ -134,22 +105,22 @@ EXPORT_SYMBOL(lockref_put_return);
134105
* @lockref: pointer to lockref structure
135106
* Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
136107
*/
137-
int lockref_put_or_lock(struct lockref *lockref)
108+
bool lockref_put_or_lock(struct lockref *lockref)
138109
{
139110
CMPXCHG_LOOP(
140111
new.count--;
141112
if (old.count <= 1)
142113
break;
143114
,
144-
return 1;
115+
return true;
145116
);
146117

147118
spin_lock(&lockref->lock);
148119
if (lockref->count <= 1)
149-
return 0;
120+
return false;
150121
lockref->count--;
151122
spin_unlock(&lockref->lock);
152-
return 1;
123+
return true;
153124
}
154125
EXPORT_SYMBOL(lockref_put_or_lock);
155126

@@ -169,23 +140,22 @@ EXPORT_SYMBOL(lockref_mark_dead);
169140
* @lockref: pointer to lockref structure
170141
* Return: 1 if count updated successfully or 0 if lockref was dead
171142
*/
172-
int lockref_get_not_dead(struct lockref *lockref)
143+
bool lockref_get_not_dead(struct lockref *lockref)
173144
{
174-
int retval;
145+
bool retval = false;
175146

176147
CMPXCHG_LOOP(
177148
new.count++;
178149
if (old.count < 0)
179-
return 0;
150+
return false;
180151
,
181-
return 1;
152+
return true;
182153
);
183154

184155
spin_lock(&lockref->lock);
185-
retval = 0;
186156
if (lockref->count >= 0) {
187157
lockref->count++;
188-
retval = 1;
158+
retval = true;
189159
}
190160
spin_unlock(&lockref->lock);
191161
return retval;

0 commit comments

Comments
 (0)