Skip to contentSkip to conversation

lockref cleanups

PATCH In review Christoph Hellwig 8 participants 27 messages Jan 15, 2025 – Mar 20, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Hi all,

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.

Diffstat:

 fs/dcache.c             |    3 --
 fs/erofs/zdata.c        |    3 --
 fs/gfs2/quota.c         |    3 --
 include/linux/lockref.h |   26 ++++++++++++++------
 lib/lockref.c           |   60 ++++++++++++------------------------------------
 5 files changed, 36 insertions(+), 59 deletions(-)
#1 lore raw

PATCH 1/8 lockref: remove lockref_put_not_zero

lockref_put_not_zero is not used anywhere, and unless I'm missing something didn't end up being used used at all. Remove it.

---
 include/linux/lockref.h |  1 -
 lib/lockref.c           | 28 ----------------------------
 2 files changed, 29 deletions(-)
include/linux/lockref.h +0 -1
index c3a1f78bc884..e5aa0347f274 100644
@@ -37,7 +37,6 @@ struct lockref {
 extern void lockref_get(struct lockref *);
 extern int lockref_put_return(struct lockref *);
 extern int lockref_get_not_zero(struct lockref *);
-extern int lockref_put_not_zero(struct lockref *);
 extern int lockref_put_or_lock(struct lockref *);
 
 extern void lockref_mark_dead(struct lockref *);
lib/lockref.c +0 -28
index 2afe4c5d8919..a68192c979b3 100644
@@ -81,34 +81,6 @@ int lockref_get_not_zero(struct lockref *lockref)
 }
 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: pointer to lockref structure
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/8 lockref: improve the lockref_get_not_zero description

In reply to Christoph Hellwig

lockref_put_return returns exactly -1 and not "an error" when the lockref is dead or locked.

---
 lib/lockref.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/lockref.c +1 -1
index a68192c979b3..b1b042a9a6c8 100644
@@ -86,7 +86,7 @@ EXPORT_SYMBOL(lockref_get_not_zero);
  * @lockref: pointer to lockref structure
  *
  * 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)
 {
Signature · 1 line
2.45.2
#3 lore raw

Re: [PATCH 2/8] lockref: improve the lockref_get_not_zero description

Quoted 2 lines · Christoph Hellwig

lockref_put_return returns exactly -1 and not "an error" when the lockref is dead or locked.

The function name in the subject needs fixing.

Quoted 19 lines
---
 lib/lockref.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/lockref.c +1 -2
index a68192c979b3..b1b042a9a6c8 100644
@@ -86,7 +86,7 @@ EXPORT_SYMBOL(lockref_get_not_zero);
  * @lockref: pointer to lockref structure
  *
  * 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)
 {
--
Quoted 1 line

2.45.2

Thanks,
Andreas

#4 lore raw

PATCH 3/8 lockref: use bool for false/true returns

In reply to Christoph Hellwig

Replace int used as bool with the actual bool type for return values that can only be true or false.

---
 include/linux/lockref.h |  6 +++---
 lib/lockref.c           | 30 ++++++++++++++----------------
 2 files changed, 17 insertions(+), 19 deletions(-)
include/linux/lockref.h +3 -3
index e5aa0347f274..3d770e1bdbad 100644
@@ -36,11 +36,11 @@ struct lockref {
 
 extern void lockref_get(struct lockref *);
 extern int lockref_put_return(struct lockref *);
-extern int lockref_get_not_zero(struct lockref *);
-extern int lockref_put_or_lock(struct lockref *);
+bool lockref_get_not_zero(struct lockref *lockref);
+bool lockref_put_or_lock(struct lockref *lockref);
 
 extern void lockref_mark_dead(struct lockref *);
-extern int lockref_get_not_dead(struct lockref *);
+bool lockref_get_not_dead(struct lockref *lockref);
 
 /* Must be called under spinlock for reliable results */
 static inline bool __lockref_is_dead(const struct lockref *l)
lib/lockref.c +14 -16
index b1b042a9a6c8..5d8e3ef3860e 100644
@@ -58,23 +58,22 @@ EXPORT_SYMBOL(lockref_get);
  * @lockref: pointer to lockref structure
  * 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(
 		new.count++;
 		if (old.count <= 0)
-			return 0;
+			return false;
 	,
-		return 1;
+		return true;
 	);
 
 	spin_lock(&lockref->lock);
-	retval = 0;
 	if (lockref->count > 0) {
 		lockref->count++;
-		retval = 1;
+		retval = true;
 	}
 	spin_unlock(&lockref->lock);
 	return retval;
@@ -106,22 +105,22 @@ EXPORT_SYMBOL(lockref_put_return);
  * @lockref: pointer to lockref structure
  * 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(
 		new.count--;
 		if (old.count <= 1)
 			break;
 	,
-		return 1;
+		return true;
 	);
 
 	spin_lock(&lockref->lock);
 	if (lockref->count <= 1)
-		return 0;
+		return false;
 	lockref->count--;
 	spin_unlock(&lockref->lock);
-	return 1;
+	return true;
 }
 EXPORT_SYMBOL(lockref_put_or_lock);
 
@@ -141,23 +140,22 @@ EXPORT_SYMBOL(lockref_mark_dead);
  * @lockref: pointer to lockref structure
  * 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(
 		new.count++;
 		if (old.count < 0)
-			return 0;
+			return false;
 	,
-		return 1;
+		return true;
 	);
 
 	spin_lock(&lockref->lock);
-	retval = 0;
 	if (lockref->count >= 0) {
 		lockref->count++;
-		retval = 1;
+		retval = true;
 	}
 	spin_unlock(&lockref->lock);
 	return retval;
Signature · 1 line
2.45.2
#5 lore raw

Re: [PATCH 3/8] lockref: use bool for false/true returns

Quoted 3 lines · Christoph Hellwig

Replace int used as bool with the actual bool type for return values that can only be true or false.

[snip]

Quoted 15 lines
lib/lockref.c +4 -4
-int lockref_get_not_zero(struct lockref *lockref)
+bool lockref_get_not_zero(struct lockref *lockref)
 {
-	int retval;
+	bool retval = false;
 
 	CMPXCHG_LOOP(
 		new.count++;
 		if (old.count <= 0)
-			return 0;
+			return false;
 	,
-		return 1;
+		return true;
 	);

lib/lockref.c EXPORT_SYMBOL(lockref_get); · line 79

Quoted 10 lines
lib/lockref.c +1 -2
 
 	spin_lock(&lockref->lock);
-	retval = 0;
 	if (lockref->count > 0) {
 		lockref->count++;
-		retval = 1;
+		retval = true;
 	}
 	spin_unlock(&lockref->lock);
 	return retval;

While this looks perfectly sane, it worsens codegen around the atomic on x86-64 at least with gcc 13.3.0. It bisected to this commit and confirmed top of next-20250318 with this reverted undoes it.

The expected state looks like this:
f0 48 0f b1 13 lock cmpxchg %rdx,(%rbx)
75 0e jne ffffffff81b33626 <lockref_get_not_dead+0x46>

However, with the above patch I see:

       f0 48 0f b1 13          lock cmpxchg %rdx,(%rbx)
       40 0f 94 c5             sete   %bpl
       40 84 ed                test   %bpl,%bpl
       74 09                   je     ffffffff81b33636 <lockref_get_not_dead+0x46>

This is not the end of the world, but also really does not need to be there.

Given that the patch is merely a cosmetic change, I would suggest I gets dropped.

#6 lore raw

Re: [PATCH 3/8] lockref: use bool for false/true returns

Quoted 47 lines · Mateusz Guzik
Quoted 3 lines · Christoph Hellwig

Replace int used as bool with the actual bool type for return values that can only be true or false.

[snip]

Quoted 15 lines
lib/lockref.c +4 -4
-int lockref_get_not_zero(struct lockref *lockref)
+bool lockref_get_not_zero(struct lockref *lockref)
 {
-     int retval;
+     bool retval = false;
 
      CMPXCHG_LOOP(
              new.count++;
              if (old.count <= 0)
-                     return 0;
+                     return false;
      ,
-             return 1;
+             return true;
      );

lib/lockref.c EXPORT_SYMBOL(lockref_get); · line 79

Quoted 10 lines
lib/lockref.c +1 -2
 
      spin_lock(&lockref->lock);
-     retval = 0;
      if (lockref->count > 0) {
              lockref->count++;
-             retval = 1;
+             retval = true;
      }
      spin_unlock(&lockref->lock);
      return retval;

While this looks perfectly sane, it worsens codegen around the atomic on x86-64 at least with gcc 13.3.0. It bisected to this commit and confirmed top of next-20250318 with this reverted undoes it.

The expected state looks like this:
f0 48 0f b1 13 lock cmpxchg %rdx,(%rbx)
75 0e jne ffffffff81b33626 <lockref_get_not_dead+0x46>

However, with the above patch I see:

       f0 48 0f b1 13          lock cmpxchg %rdx,(%rbx)
       40 0f 94 c5             sete   %bpl
       40 84 ed                test   %bpl,%bpl
       74 09                   je     ffffffff81b33636 <lockref_get_not_dead+0x46>
Quoted 5 lines

This is not the end of the world, but also really does not need to be there.

Given that the patch is merely a cosmetic change, I would suggest I gets dropped.

fwiw I confirmed clang does *not* have the problem, I don't know about gcc 14.

Maybe I'll get around to testing it, but first I'm gonna need to carve out the custom asm into a standalone testcase.

Regardless, 13 suffering the problem is imo a good enough reason to whack the change.

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
#7 lore raw

Re: [PATCH 3/8] lockref: use bool for false/true returns

Quoted 7 lines · Mateusz Guzik

fwiw I confirmed clang does *not* have the problem, I don't know about gcc 14.

Maybe I'll get around to testing it, but first I'm gonna need to carve out the custom asm into a standalone testcase.

Regardless, 13 suffering the problem is imo a good enough reason to whack the change.

Reverting a change because a specific compiler generates sligtly worse code without even showing it has any real life impact feels like I'm missing something important.

#8 lore raw

Re: [PATCH 3/8] lockref: use bool for false/true returns

Quoted 7 lines · Christoph Hellwig
Quoted 5 lines · Mateusz Guzik

fwiw I confirmed clang does *not* have the problem, I don't know about gcc 14.

Maybe I'll get around to testing it, but first I'm gonna need to carve out the custom asm into a standalone testcase.

Quoted 7 lines
Quoted 2 lines

Regardless, 13 suffering the problem is imo a good enough reason to whack the change.

Reverting a change because a specific compiler generates sligtly worse code without even showing it has any real life impact feels like I'm missing something important.

The change is cosmetic and has an unintended impact on code gen, which imo already puts a question mark on it.

Neither the change itself nor the resulting impact are of note and in that case I would err on just not including it for the time being, but that's just my $0.03.

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
#9 lore raw

Re: [PATCH 3/8] lockref: use bool for false/true returns

Quoted 6 lines · Mateusz Guzik

The change is cosmetic and has an unintended impact on code gen, which imo already puts a question mark on it.

Neither the change itself nor the resulting impact are of note and in that case I would err on just not including it for the time being, but that's just my $0.03.

Im not sure how you came up with that opinion but as you might have guessed I don't agree at all. Having the proper types is more than just cosmetic, on the other hand the code generation change you see can easily be argued as cosmetic. You've also completely ignored the request to show any indication that it actually matters the slightest.

#10 lore raw

PATCH 4/8 lockref: drop superfluous externs

In reply to Christoph Hellwig

Drop the superfluous externs from the remaining prototypes in lockref.h.

---
 include/linux/lockref.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/lockref.h +3 -3
index 3d770e1bdbad..f821f46e9fb4 100644
@@ -34,12 +34,12 @@ struct lockref {
 	};
 };
 
-extern void lockref_get(struct lockref *);
-extern int lockref_put_return(struct lockref *);
+void lockref_get(struct lockref *lockref);
+int lockref_put_return(struct lockref *lockref);
 bool lockref_get_not_zero(struct lockref *lockref);
 bool lockref_put_or_lock(struct lockref *lockref);
 
-extern void lockref_mark_dead(struct lockref *);
+void lockref_mark_dead(struct lockref *lockref);
 bool lockref_get_not_dead(struct lockref *lockref);
 
 /* Must be called under spinlock for reliable results */
Signature · 1 line
2.45.2
#11 lore raw

PATCH 5/8 lockref: add a lockref_init helper

In reply to Christoph Hellwig

Add a helper to initialize the lockdep, that is initialize the spinlock and set a value. Having to open code them isn't a big deal, but having an initializer feels right for a proper primitive.

---
 include/linux/lockref.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/linux/lockref.h +11 -0
index f821f46e9fb4..c39f119659ba 100644
@@ -34,6 +34,17 @@ struct lockref {
 	};
 };
 
+/**
+ * lockref_init - Initialize a lockref
+ * @lockref: pointer to lockref structure
+ * @count: initial count
+ */
+static inline void lockref_init(struct lockref *lockref, unsigned int count)
+{
+	spin_lock_init(&lockref->lock);
+	lockref->count = count;
+}
+
 void lockref_get(struct lockref *lockref);
 int lockref_put_return(struct lockref *lockref);
 bool lockref_get_not_zero(struct lockref *lockref);
Signature · 1 line
2.45.2
#12 lore raw

PATCH 6/8 dcache: use lockref_init for d_lockref

In reply to Christoph Hellwig
---
 fs/dcache.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/dcache.c +1 -2
index b4d5e9e1e43d..1a01d7a6a7a9 100644
@@ -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 */
 	smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
 
-	dentry->d_lockref.count = 1;
 	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);
 	dentry->d_inode = NULL;
 	dentry->d_parent = dentry;
Signature · 1 line
2.45.2
#13 lore raw

Re: [PATCH 6/8] dcache: use lockref_init for d_lockref

Quoted 10 lines · Christoph Hellwig
---
 fs/dcache.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/dcache.c +0 -0
index b4d5e9e1e43d..1a01d7a6a7a9 100644

fs/dcache.c static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) · line 1686

Quoted 6 lines
fs/dcache.c +0 -2
@@ -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 */
 	smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
 
-	dentry->d_lockref.count = 1;
 	dentry->d_flags = 0;
-	spin_lock_init(&dentry->d_lock);

Looks wrong - dentry->d_lock is not part of dentry->d_lockref...

-Dave.

Quoted 9 lines
fs/dcache.c +1 -0
+	lockref_init(&dentry->d_lockref, 1);
 	seqcount_spinlock_init(&dentry->d_seq, &dentry->d_lock);
 	dentry->d_inode = NULL;
 	dentry->d_parent = dentry;
Signature · 1 line
2.45.2
Signature · 2 lines
Dave Chinner
[email protected]
maintainer
#14 lore raw

Re: [PATCH 6/8] dcache: use lockref_init for d_lockref

Quoted 14 lines · Dave Chinner
Quoted 13 lines · Christoph Hellwig
---
 fs/dcache.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/dcache.c +0 -0
index b4d5e9e1e43d..1a01d7a6a7a9 100644
@@ -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 */
 	smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
 
Quoted 5 lines

fs/dcache.c static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) · line 1686

Quoted 3 lines
fs/dcache.c +0 -2
-	dentry->d_lockref.count = 1;
 	dentry->d_flags = 0;
-	spin_lock_init(&dentry->d_lock);

Looks wrong - dentry->d_lock is not part of dentry->d_lockref...

include/linux/dcache.h:80:#define d_lock d_lockref.lock

#15 lore raw

Re: [PATCH 6/8] dcache: use lockref_init for d_lockref

Quoted 15 lines · Al Viro
Quoted 14 lines · Dave Chinner
Quoted 13 lines · Christoph Hellwig
---
 fs/dcache.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/dcache.c +0 -0
index b4d5e9e1e43d..1a01d7a6a7a9 100644
@@ -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 */
 	smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
 
Quoted 7 lines
Quoted 5 lines

fs/dcache.c static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) · line 1686

Quoted 3 lines
fs/dcache.c +0 -2
-	dentry->d_lockref.count = 1;
 	dentry->d_flags = 0;
-	spin_lock_init(&dentry->d_lock);

Looks wrong - dentry->d_lock is not part of dentry->d_lockref...

include/linux/dcache.h:80:#define d_lock d_lockref.lock

Ah, I missed that subtlety (obviously). Carry on!

-Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#16 lore raw

PATCH 7/8 erofs: use lockref_init for pcl->lockref

In reply to Christoph Hellwig
---
 fs/erofs/zdata.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/erofs/zdata.c +1 -2
index 19ef4ff2a134..254f6ad2c336 100644
@@ -747,8 +747,7 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
 	if (IS_ERR(pcl))
 		return PTR_ERR(pcl);
 
-	spin_lock_init(&pcl->lockref.lock);
-	pcl->lockref.count = 1;		/* one ref for this request */
+	lockref_init(&pcl->lockref, 1); /* one ref for this request */
 	pcl->algorithmformat = map->m_algorithmformat;
 	pcl->length = 0;
 	pcl->partial = true;
Signature · 1 line
2.45.2
#18 lore raw

PATCH 8/8 gfs2: use lockref_init for qd_lockref

In reply to Christoph Hellwig
---
 fs/gfs2/quota.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/gfs2/quota.c +1 -2
index 72b48f6f5561..58bc5013ca49 100644
@@ -236,8 +236,7 @@ static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, str
 		return NULL;
 
 	qd->qd_sbd = sdp;
-	qd->qd_lockref.count = 0;
-	spin_lock_init(&qd->qd_lockref.lock);
+	lockref_init(&qd->qd_lockref, 0);
 	qd->qd_id = qid;
 	qd->qd_slot = -1;
 	INIT_LIST_HEAD(&qd->qd_lru);
Signature · 1 line
2.45.2
#19 lore raw

Re: [PATCH 8/8] gfs2: use lockref_init for qd_lockref

Quoted 10 lines · Christoph Hellwig
---
 fs/gfs2/quota.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/gfs2/quota.c +0 -0
index 72b48f6f5561..58bc5013ca49 100644

fs/gfs2/quota.c static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, str · line 239

Quoted 6 lines
fs/gfs2/quota.c +1 -2
@@ -236,8 +236,7 @@ static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, str
                return NULL;
 
        qd->qd_sbd = sdp;
-       qd->qd_lockref.count = 0;
-       spin_lock_init(&qd->qd_lockref.lock);
+       lockref_init(&qd->qd_lockref, 0);

Hmm, initializing count to 0 seems to be the odd case and it's fairly simple to change gfs2 to work with an initial value of 1. I wonder if lockref_init() should really have a count argument.

Quoted 5 lines
fs/gfs2/quota.c +0 -1
        qd->qd_id = qid;
        qd->qd_slot = -1;
        INIT_LIST_HEAD(&qd->qd_lru);
--

2.45.2

Thanks,
Andreas

#20 lore raw

Re: [PATCH 8/8] gfs2: use lockref_init for qd_lockref

Quoted 4 lines · Andreas Gruenbacher
Quoted 4 lines

+++ b/fs/gfs2/quota.c

fs/gfs2/quota.c +0 -0
@@ -236,8 +236,7 @@ static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, str
                return NULL;
 
Quoted 8 lines

fs/gfs2/quota.c static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, str · line 239

Quoted 4 lines
fs/gfs2/quota.c +1 -2
        qd->qd_sbd = sdp;
-       qd->qd_lockref.count = 0;
-       spin_lock_init(&qd->qd_lockref.lock);
+       lockref_init(&qd->qd_lockref, 0);

Hmm, initializing count to 0 seems to be the odd case and it's fairly simple to change gfs2 to work with an initial value of 1. I wonder if lockref_init() should really have a count argument.

Well, if you can fix it to start with 1 we could start out with 1 as the default. FYI, I also didn't touch the other gfs2 lockref because it initialize the lock in the slab init_once callback and the count on every initialization.

#21 lore raw

Re: [PATCH 8/8] gfs2: use lockref_init for qd_lockref

Quoted 4 lines · Christoph Hellwig

Well, if you can fix it to start with 1 we could start out with 1 as the default. FYI, I also didn't touch the other gfs2 lockref because it initialize the lock in the slab init_once callback and the count on every initialization.

Sure, can you add the below patch before the lockref_init conversion?

Thanks,
Andreas

--

gfs2: Prepare for converting to lockref_init

First, move initializing the glock lockref spin lock from gfs2_init_glock_once() to gfs2_glock_get().

Second, in qd_alloc(), initialize the lockref count to 1 to cover the common case. Compensate for that in gfs2_quota_init() by adjusting the count back down to 0; this case occurs only when mounting the filesystem rw.

---
 fs/gfs2/glock.c | 3 ++-
 fs/gfs2/main.c  | 1 -
 fs/gfs2/quota.c | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
fs/gfs2/glock.c +2 -1
index 8c4c1f871a88..22ff77cdd827 100644
@@ -1201,8 +1201,9 @@ int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
 	if (glops->go_instantiate)
 		gl->gl_flags |= BIT(GLF_INSTANTIATE_NEEDED);
 	gl->gl_name = name;
-	lockdep_set_subclass(&gl->gl_lockref.lock, glops->go_subclass);
 	gl->gl_lockref.count = 1;
+	spin_lock_init(&gl->gl_lockref.lock);
+	lockdep_set_subclass(&gl->gl_lockref.lock, glops->go_subclass);
 	gl->gl_state = LM_ST_UNLOCKED;
 	gl->gl_target = LM_ST_UNLOCKED;
 	gl->gl_demote_state = LM_ST_EXCLUSIVE;
fs/gfs2/main.c +0 -1
index 04cadc02e5a6..0727f60ad028 100644
@@ -51,7 +51,6 @@ static void gfs2_init_glock_once(void *foo)
 {
 	struct gfs2_glock *gl = foo;
 
-	spin_lock_init(&gl->gl_lockref.lock);
 	INIT_LIST_HEAD(&gl->gl_holders);
 	INIT_LIST_HEAD(&gl->gl_lru);
 	INIT_LIST_HEAD(&gl->gl_ail_list);
fs/gfs2/quota.c +2 -2
index 72b48f6f5561..df78eb8f35f9 100644
@@ -236,7 +236,7 @@ static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, str
 		return NULL;
 
 	qd->qd_sbd = sdp;
-	qd->qd_lockref.count = 0;
+	qd->qd_lockref.count = 1;
 	spin_lock_init(&qd->qd_lockref.lock);
 	qd->qd_id = qid;
 	qd->qd_slot = -1;
@@ -298,7 +298,6 @@ static int qd_get(struct gfs2_sbd *sdp, struct kqid qid,
 	spin_lock_bucket(hash);
 	*qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
 	if (qd == NULL) {
-		new_qd->qd_lockref.count++;
 		*qdp = new_qd;
 		list_add(&new_qd->qd_list, &sdp->sd_quota_list);
 		hlist_bl_add_head_rcu(&new_qd->qd_hlist, &qd_hash_table[hash]);
@@ -1451,6 +1450,7 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 			if (qd == NULL)
 				goto fail_brelse;
 
+			qd->qd_lockref.count = 0;
 			set_bit(QDF_CHANGE, &qd->qd_flags);
 			qd->qd_change = qc_change;
 			qd->qd_slot = slot;
Signature · 1 line
2.47.1
#22 lore raw

Re: [PATCH 8/8] gfs2: use lockref_init for qd_lockref

Quoted 18 lines · Andreas Gruenbacher
Quoted 4 lines · Christoph Hellwig

Well, if you can fix it to start with 1 we could start out with 1 as the default. FYI, I also didn't touch the other gfs2 lockref because it initialize the lock in the slab init_once callback and the count on every initialization.

Sure, can you add the below patch before the lockref_init conversion?

Thanks,
Andreas

--

gfs2: Prepare for converting to lockref_init

First, move initializing the glock lockref spin lock from gfs2_init_glock_once() to gfs2_glock_get().

Quoted 7 lines

Second, in qd_alloc(), initialize the lockref count to 1 to cover the common case. Compensate for that in gfs2_quota_init() by adjusting the count back down to 0; this case occurs only when mounting the filesystem rw.

---

Can you send this as a proper separae patch, please?

#23 lore raw

Re: [PATCH 8/8] gfs2: use lockref_init for qd_lockref

Quoted 24 lines · Christian Brauner
Quoted 23 lines · Andreas Gruenbacher
Quoted 4 lines · Christoph Hellwig

Well, if you can fix it to start with 1 we could start out with 1 as the default. FYI, I also didn't touch the other gfs2 lockref because it initialize the lock in the slab init_once callback and the count on every initialization.

Sure, can you add the below patch before the lockref_init conversion?

Thanks,
Andreas

--

gfs2: Prepare for converting to lockref_init

First, move initializing the glock lockref spin lock from gfs2_init_glock_once() to gfs2_glock_get().

Second, in qd_alloc(), initialize the lockref count to 1 to cover the common case. Compensate for that in gfs2_quota_init() by adjusting the count back down to 0; this case occurs only when mounting the filesystem rw.

Quoted 4 lines
Quoted 2 lines

---

Can you send this as a proper separae patch, please?

Do you want this particular version which applies before Christoph's patches or something that applies on top of Christoph's patches?

Thanks,
Andreas

#24 lore raw

Re: [PATCH 8/8] gfs2: use lockref_init for qd_lockref

Quoted 25 lines · Andreas Gruenbacher
Quoted 24 lines · Christian Brauner
Quoted 23 lines · Andreas Gruenbacher
Quoted 4 lines · Christoph Hellwig

Well, if you can fix it to start with 1 we could start out with 1 as the default. FYI, I also didn't touch the other gfs2 lockref because it initialize the lock in the slab init_once callback and the count on every initialization.

Sure, can you add the below patch before the lockref_init conversion?

Thanks,
Andreas

--

gfs2: Prepare for converting to lockref_init

First, move initializing the glock lockref spin lock from gfs2_init_glock_once() to gfs2_glock_get().

Second, in qd_alloc(), initialize the lockref count to 1 to cover the common case. Compensate for that in gfs2_quota_init() by adjusting the count back down to 0; this case occurs only when mounting the filesystem rw.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

---

Can you send this as a proper separae patch, please?

Do you want this particular version which applies before Christoph's patches or something that applies on top of Christoph's patches?

On top, if you don't mind. Thank you!

maintainer Applied
#25 lore raw
In reply to Christoph Hellwig
Quoted 4 lines · Christoph Hellwig

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.

Diffstat:

Quoted 8 lines
 fs/dcache.c             |    3 --
 fs/erofs/zdata.c        |    3 --
 fs/gfs2/quota.c         |    3 --
 include/linux/lockref.h |   26 ++++++++++++++------
 lib/lockref.c           |   60 ++++++++++++------------------------------------
 5 files changed, 36 insertions(+), 59 deletions(-)

[...]

Looks good, thanks!

---

Applied to the vfs-6.14.misc branch of the vfs/vfs.git tree. Patches in the vfs-6.14.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-6.14.misc

[1/8] lockref: remove lockref_put_not_zero

      https://git.kernel.org/vfs/vfs/c/74b5da771c89
[2/8] lockref: improve the lockref_get_not_zero description
      https://git.kernel.org/vfs/vfs/c/8c7568356d74
[3/8] lockref: use bool for false/true returns
      https://git.kernel.org/vfs/vfs/c/57bd981b2db7
[4/8] lockref: drop superfluous externs
      https://git.kernel.org/vfs/vfs/c/80e2823cbe59
[5/8] lockref: add a lockref_init helper
      https://git.kernel.org/vfs/vfs/c/5f0c395edf59
[6/8] dcache: use lockref_init for d_lockref
      https://git.kernel.org/vfs/vfs/c/24706068b7b6
[7/8] erofs: use lockref_init for pcl->lockref
      https://git.kernel.org/vfs/vfs/c/160a93170d53
[8/8] gfs2: use lockref_init for qd_lockref
      https://git.kernel.org/vfs/vfs/c/0ef3858b15e3
Reviewed-by
#26 lore raw
In reply to Christoph Hellwig
Quoted 5 lines · Christoph Hellwig

Hi all,

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.

Quoted 8 lines

Diffstat:

 fs/dcache.c             |    3 --
 fs/erofs/zdata.c        |    3 --
 fs/gfs2/quota.c         |    3 --
 include/linux/lockref.h |   26 ++++++++++++++------
 lib/lockref.c           |   60 ++++++++++++------------------------------------
 5 files changed, 36 insertions(+), 59 deletions(-)

Aside from the commit log problem Andreas pointed out in patch #2, this looks good. Assuming that's fixed: