Merge patch series "lockref cleanups"

Christoph Hellwig <hch@lst.de> 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/20250115094702.504610-1-hch@lst.de:
  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/20250115094702.504610-1-hch@lst.de
Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
Christian Brauner 2025-01-15 11:50:29 +01:00
commit c859df526b
No known key found for this signature in database
GPG key ID: 91C61BC06578DCA2
5 changed files with 35 additions and 58 deletions

View file

@ -1681,9 +1681,8 @@ static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
/* Make sure we always see the terminating NUL character */ /* Make sure we always see the terminating NUL character */
smp_store_release(&dentry->d_name.name, dname); /* ^^^ */ smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
dentry->d_lockref.count = 1;
dentry->d_flags = 0; dentry->d_flags = 0;
spin_lock_init(&dentry->d_lock); lockref_init(&dentry->d_lockref, 1);
seqcount_spinlock_init(&dentry->d_seq, &dentry->d_lock); seqcount_spinlock_init(&dentry->d_seq, &dentry->d_lock);
dentry->d_inode = NULL; dentry->d_inode = NULL;
dentry->d_parent = dentry; dentry->d_parent = dentry;

View file

@ -747,8 +747,7 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
if (IS_ERR(pcl)) if (IS_ERR(pcl))
return PTR_ERR(pcl); return PTR_ERR(pcl);
spin_lock_init(&pcl->lockref.lock); lockref_init(&pcl->lockref, 1); /* one ref for this request */
pcl->lockref.count = 1; /* one ref for this request */
pcl->algorithmformat = map->m_algorithmformat; pcl->algorithmformat = map->m_algorithmformat;
pcl->length = 0; pcl->length = 0;
pcl->partial = true; pcl->partial = true;

View file

@ -236,8 +236,7 @@ static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, str
return NULL; return NULL;
qd->qd_sbd = sdp; qd->qd_sbd = sdp;
qd->qd_lockref.count = 0; lockref_init(&qd->qd_lockref, 0);
spin_lock_init(&qd->qd_lockref.lock);
qd->qd_id = qid; qd->qd_id = qid;
qd->qd_slot = -1; qd->qd_slot = -1;
INIT_LIST_HEAD(&qd->qd_lru); INIT_LIST_HEAD(&qd->qd_lru);

View file

@ -34,14 +34,24 @@ struct lockref {
}; };
}; };
extern void lockref_get(struct lockref *); /**
extern int lockref_put_return(struct lockref *); * lockref_init - Initialize a lockref
extern int lockref_get_not_zero(struct lockref *); * @lockref: pointer to lockref structure
extern int lockref_put_not_zero(struct lockref *); * @count: initial count
extern int lockref_put_or_lock(struct lockref *); */
static inline void lockref_init(struct lockref *lockref, unsigned int count)
{
spin_lock_init(&lockref->lock);
lockref->count = count;
}
extern void lockref_mark_dead(struct lockref *); void lockref_get(struct lockref *lockref);
extern int lockref_get_not_dead(struct lockref *); int lockref_put_return(struct lockref *lockref);
bool lockref_get_not_zero(struct lockref *lockref);
bool lockref_put_or_lock(struct lockref *lockref);
void lockref_mark_dead(struct lockref *lockref);
bool lockref_get_not_dead(struct lockref *lockref);
/* Must be called under spinlock for reliable results */ /* Must be called under spinlock for reliable results */
static inline bool __lockref_is_dead(const struct lockref *l) static inline bool __lockref_is_dead(const struct lockref *l)

View file

@ -58,63 +58,34 @@ EXPORT_SYMBOL(lockref_get);
* @lockref: pointer to lockref structure * @lockref: pointer to lockref structure
* Return: 1 if count updated successfully or 0 if count was zero * Return: 1 if count updated successfully or 0 if count was zero
*/ */
int lockref_get_not_zero(struct lockref *lockref) bool lockref_get_not_zero(struct lockref *lockref)
{ {
int retval; bool retval = false;
CMPXCHG_LOOP( CMPXCHG_LOOP(
new.count++; new.count++;
if (old.count <= 0) if (old.count <= 0)
return 0; return false;
, ,
return 1; return true;
); );
spin_lock(&lockref->lock); spin_lock(&lockref->lock);
retval = 0;
if (lockref->count > 0) { if (lockref->count > 0) {
lockref->count++; lockref->count++;
retval = 1; retval = true;
} }
spin_unlock(&lockref->lock); spin_unlock(&lockref->lock);
return retval; return retval;
} }
EXPORT_SYMBOL(lockref_get_not_zero); EXPORT_SYMBOL(lockref_get_not_zero);
/**
* lockref_put_not_zero - Decrements count unless count <= 1 before decrement
* @lockref: pointer to lockref structure
* Return: 1 if count updated successfully or 0 if count would become zero
*/
int lockref_put_not_zero(struct lockref *lockref)
{
int retval;
CMPXCHG_LOOP(
new.count--;
if (old.count <= 1)
return 0;
,
return 1;
);
spin_lock(&lockref->lock);
retval = 0;
if (lockref->count > 1) {
lockref->count--;
retval = 1;
}
spin_unlock(&lockref->lock);
return retval;
}
EXPORT_SYMBOL(lockref_put_not_zero);
/** /**
* lockref_put_return - Decrement reference count if possible * lockref_put_return - Decrement reference count if possible
* @lockref: pointer to lockref structure * @lockref: pointer to lockref structure
* *
* Decrement the reference count and return the new value. * Decrement the reference count and return the new value.
* If the lockref was dead or locked, return an error. * If the lockref was dead or locked, return -1.
*/ */
int lockref_put_return(struct lockref *lockref) int lockref_put_return(struct lockref *lockref)
{ {
@ -134,22 +105,22 @@ EXPORT_SYMBOL(lockref_put_return);
* @lockref: pointer to lockref structure * @lockref: pointer to lockref structure
* Return: 1 if count updated successfully or 0 if count <= 1 and lock taken * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
*/ */
int lockref_put_or_lock(struct lockref *lockref) bool lockref_put_or_lock(struct lockref *lockref)
{ {
CMPXCHG_LOOP( CMPXCHG_LOOP(
new.count--; new.count--;
if (old.count <= 1) if (old.count <= 1)
break; break;
, ,
return 1; return true;
); );
spin_lock(&lockref->lock); spin_lock(&lockref->lock);
if (lockref->count <= 1) if (lockref->count <= 1)
return 0; return false;
lockref->count--; lockref->count--;
spin_unlock(&lockref->lock); spin_unlock(&lockref->lock);
return 1; return true;
} }
EXPORT_SYMBOL(lockref_put_or_lock); EXPORT_SYMBOL(lockref_put_or_lock);
@ -169,23 +140,22 @@ EXPORT_SYMBOL(lockref_mark_dead);
* @lockref: pointer to lockref structure * @lockref: pointer to lockref structure
* Return: 1 if count updated successfully or 0 if lockref was dead * Return: 1 if count updated successfully or 0 if lockref was dead
*/ */
int lockref_get_not_dead(struct lockref *lockref) bool lockref_get_not_dead(struct lockref *lockref)
{ {
int retval; bool retval = false;
CMPXCHG_LOOP( CMPXCHG_LOOP(
new.count++; new.count++;
if (old.count < 0) if (old.count < 0)
return 0; return false;
, ,
return 1; return true;
); );
spin_lock(&lockref->lock); spin_lock(&lockref->lock);
retval = 0;
if (lockref->count >= 0) { if (lockref->count >= 0) {
lockref->count++; lockref->count++;
retval = 1; retval = true;
} }
spin_unlock(&lockref->lock); spin_unlock(&lockref->lock);
return retval; return retval;