Skip to contentSkip to conversation

gfs2: Stop using rhashtable_walk_peek

PATCH In review Andreas Gruenbacher 6 participants 17 messages Mar 29, 2018 – Apr 12, 2018

Subsystems (4)
lore
Share & tools
lore

Andreas Gruenbacher

author maintainer
#0 lore raw

v2 cover gfs2: Stop using rhashtable_walk_peek

Here's a second version of the patch (now a patch set) to eliminate rhashtable_walk_peek in gfs2.

The first patch introduces lockref_put_not_zero, the inverse of lockref_get_not_zero.

The second patch eliminates rhashtable_walk_peek in gfs2. In gfs2_glock_iter_next, the new lockref function from patch one is used to drop a lockref count as long as the count doesn't drop to zero. This is almost always the case; if there is a risk of dropping the last reference, we must defer that to a work queue because dropping the last reference may sleep.

Thanks,
Andreas

Andreas Gruenbacher (2):
lockref: Add lockref_put_not_zero
gfs2: Stop using rhashtable_walk_peek

 fs/gfs2/glock.c         | 47 ++++++++++++++++++++++++++++-------------------
 include/linux/lockref.h |  1 +
 lib/lockref.c           | 28 ++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 19 deletions(-)
Signature · 1 line
2.14.3
author maintainer
#1 lore raw

PATCH v2 1/2 lockref: Add lockref_put_not_zero

Put a lockref unless the lockref is dead or its count would become zero. This is the same as lockref_put_or_lock except that the lock is never left held.

---
 include/linux/lockref.h |  1 +
 lib/lockref.c           | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/linux/lockref.h +1 -0
index 2eac32095113..99f17cc8e163 100644
@@ -37,6 +37,7 @@ 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_get_or_lock(struct lockref *);
 extern int lockref_put_or_lock(struct lockref *);
 
lib/lockref.c +28 -0
index 47169ed7e964..3d468b53d4c9 100644
@@ -80,6 +80,34 @@ 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_get_or_lock - Increments count unless the count is 0 or dead
  * @lockref: pointer to lockref structure
Signature · 1 line
2.14.3
author maintainer
#2 lore raw

PATCH v2 2/2 gfs2: Stop using rhashtable_walk_peek

In reply to Andreas Gruenbacher

Function rhashtable_walk_peek is problematic because there is no guarantee that the glock previously returned still exists; when that key is deleted, rhashtable_walk_peek can end up returning a different key, which will cause an inconsistent glock dump. Fix this by keeping track of the current glock in the seq file iterator functions instead.

---
 fs/gfs2/glock.c | 47 ++++++++++++++++++++++++++++-------------------
 1 file changed, 28 insertions(+), 19 deletions(-)
fs/gfs2/glock.c +28 -19
index 82fb5583445c..097bd3c0f270 100644
@@ -1923,28 +1923,37 @@ void gfs2_glock_exit(void)
 
 static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi, loff_t n)
 {
-	if (n == 0)
-		gi->gl = rhashtable_walk_peek(&gi->hti);
-	else {
-		gi->gl = rhashtable_walk_next(&gi->hti);
-		n--;
+	struct gfs2_glock *gl = gi->gl;
+
+	if (gl) {
+		if (n == 0)
+			return;
+		if (!lockref_put_not_zero(&gl->gl_lockref))
+			gfs2_glock_queue_put(gl);
 	}
 	for (;;) {
-		if (IS_ERR_OR_NULL(gi->gl)) {
-			if (!gi->gl)
-				return;
-			if (PTR_ERR(gi->gl) != -EAGAIN) {
-				gi->gl = NULL;
-				return;
+		gl = rhashtable_walk_next(&gi->hti);
+		if (IS_ERR_OR_NULL(gl)) {
+			if (gl == ERR_PTR(-EAGAIN)) {
+				n = 1;
+				continue;
 			}
-			n = 0;
-		} else if (gi->sdp == gi->gl->gl_name.ln_sbd &&
-			   !__lockref_is_dead(&gi->gl->gl_lockref)) {
-			if (!n--)
-				break;
+			gl = NULL;
+			break;
+		}
+		if (gl->gl_name.ln_sbd != gi->sdp)
+			continue;
+		if (n <= 1) {
+			if (!lockref_get_not_dead(&gl->gl_lockref))
+				continue;
+			break;
+		} else {
+			if (__lockref_is_dead(&gl->gl_lockref))
+				continue;
+			n--;
 		}
-		gi->gl = rhashtable_walk_next(&gi->hti);
 	}
+	gi->gl = gl;
 }
 
 static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
@@ -1988,7 +1997,6 @@ static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
 {
 	struct gfs2_glock_iter *gi = seq->private;
 
-	gi->gl = NULL;
 	rhashtable_walk_stop(&gi->hti);
 }
 
@@ -2076,7 +2084,8 @@ static int gfs2_glocks_release(struct inode *inode, struct file *file)
 	struct seq_file *seq = file->private_data;
 	struct gfs2_glock_iter *gi = seq->private;
 
-	gi->gl = NULL;
+	if (gi->gl)
+		gfs2_glock_put(gi->gl);
 	rhashtable_walk_exit(&gi->hti);
 	return seq_release_private(inode, file);
 }
Signature · 1 line
2.14.3
#3 lore raw
In reply to Andreas Gruenbacher

Hi,

Can we solve the problem another way, by not taking refs on the glocks when we are iterating over them for the debugfs files? I assume that is the main issue here.

We didn't used to take refs since the rcu locking was enough during the walk itself. We used to only keep track of the hash bucket and offset within the bucket when we dropped the rcu lock between calls to the iterator. I may have lost track of why that approach did not work?

Steve.

Quoted 25 lines · Andreas Gruenbacher

Here's a second version of the patch (now a patch set) to eliminate rhashtable_walk_peek in gfs2.

The first patch introduces lockref_put_not_zero, the inverse of lockref_get_not_zero.

The second patch eliminates rhashtable_walk_peek in gfs2. In gfs2_glock_iter_next, the new lockref function from patch one is used to drop a lockref count as long as the count doesn't drop to zero. This is almost always the case; if there is a risk of dropping the last reference, we must defer that to a work queue because dropping the last reference may sleep.

Thanks,
Andreas

Andreas Gruenbacher (2):
lockref: Add lockref_put_not_zero
gfs2: Stop using rhashtable_walk_peek

  fs/gfs2/glock.c         | 47 ++++++++++++++++++++++++++++-------------------
  include/linux/lockref.h |  1 +
  lib/lockref.c           | 28 ++++++++++++++++++++++++++++
  3 files changed, 57 insertions(+), 19 deletions(-)
#5 lore raw
In reply to Andreas Gruenbacher
Quoted 6 lines · Andreas Gruenbacher

Here's a second version of the patch (now a patch set) to eliminate rhashtable_walk_peek in gfs2.

The first patch introduces lockref_put_not_zero, the inverse of lockref_get_not_zero.

Quoted 6 lines

The second patch eliminates rhashtable_walk_peek in gfs2. In gfs2_glock_iter_next, the new lockref function from patch one is used to drop a lockref count as long as the count doesn't drop to zero. This is almost always the case; if there is a risk of dropping the last reference, we must defer that to a work queue because dropping the last reference may sleep.

In light of Neil's latest patch, do we still need this?

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#4 lore raw
In reply to Steven Whitehouse
Quoted 10 lines · Steven Whitehouse

Hi,

Can we solve the problem another way, by not taking refs on the glocks when we are iterating over them for the debugfs files? I assume that is the main issue here.

We didn't used to take refs since the rcu locking was enough during the walk itself. We used to only keep track of the hash bucket and offset within the bucket when we dropped the rcu lock between calls to the iterator. I may have lost track of why that approach did not work?

That doesn't work because when a glock doesn't fit into one read, we need to make sure that the next read will continue with the same glock or else we'll end up with a corrupted dump. And rhashtable_walk_peek cannot guarantee that.

I've done some minimal performance testing and the additional ref taking only impacted the performance in the 10% range or less, so it doesn't really matter.

Andreas

author maintainer
#6 lore raw
In reply to Herbert Xu
Quoted 7 lines · Herbert Xu
Quoted 6 lines · Andreas Gruenbacher

Here's a second version of the patch (now a patch set) to eliminate rhashtable_walk_peek in gfs2.

The first patch introduces lockref_put_not_zero, the inverse of lockref_get_not_zero.

Quoted 8 lines
Quoted 6 lines

The second patch eliminates rhashtable_walk_peek in gfs2. In gfs2_glock_iter_next, the new lockref function from patch one is used to drop a lockref count as long as the count doesn't drop to zero. This is almost always the case; if there is a risk of dropping the last reference, we must defer that to a work queue because dropping the last reference may sleep.

In light of Neil's latest patch, do we still need this?

For all I know, Neil's latest plan is to get rhashtable_walk_peek replaced and removed because it is unfixable. This patch removes the one and only user.

Thanks,
Andreas

#7 lore raw
Quoted 4 lines · Andreas Gruenbacher

For all I know, Neil's latest plan is to get rhashtable_walk_peek replaced and removed because it is unfixable. This patch removes the one and only user.

His latest patch makes rhashtable_walk_peek stable in the face of removals.

https://patchwork.ozlabs.org/patch/892534/

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#8 lore raw
Quoted 10 lines · Herbert Xu
Quoted 4 lines · Andreas Gruenbacher

For all I know, Neil's latest plan is to get rhashtable_walk_peek replaced and removed because it is unfixable. This patch removes the one and only user.

His latest patch makes rhashtable_walk_peek stable in the face of removals.

https://patchwork.ozlabs.org/patch/892534/

Ok, I can slightly update my patch description. The problem still remains that glocks can be deleted from the rhashtable between stop/start, and that needs to be fixed in gfs2. Once that's done, keeping track of the current glock comes for free and we won't need rhashtable_walk_peek anymore.

Should rhashtable_walk_peek be kept around even if there are no more users? I have my doubts.

Thanks,
Andreas

#9 lore raw
Quoted 3 lines · Andreas Gruenbacher

Should rhashtable_walk_peek be kept around even if there are no more users? I have my doubts.

Absolutely. All netlink dumps using rhashtable_walk_next are buggy and need to switch over to rhashtable_walk_peek. As otherwise the object that triggers the out-of-space condition will be skipped upon resumption.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#10 lore raw
Quoted 9 lines · Herbert Xu
Quoted 3 lines · Andreas Gruenbacher

Should rhashtable_walk_peek be kept around even if there are no more users? I have my doubts.

Absolutely. All netlink dumps using rhashtable_walk_next are buggy and need to switch over to rhashtable_walk_peek. As otherwise the object that triggers the out-of-space condition will be skipped upon resumption.

Do we really need a rhashtable_walk_peek() interface? I imagine that a seqfile ->start function can do:

  if (*ppos == 0 && last_pos != 0) {
  	rhashtable_walk_exit(&iter);
        rhashtable_walk_enter(&table, &iter);
        last_pos = 0;
  }
  rhashtable_walk_start(&iter);
  if (*ppos == last_pos && iter.p)
  	return iter.p;
  last_pos = *ppos;
  return rhashtable_walk_next(&iter)

and the ->next function just does

  last_pos = *ppos;
  *ppos += 1;
  do p = rhashtable_walk_next(&iter); while (IS_ERR(p));
  return p;

It might be OK to have a function call instead of expecting people to use iter.p directly.

static inline void *rhashtable_walk_prev(struct rhashtable_iter *iter)

{
	return iter->p;
}

Thoughts?

Thanks,
NeilBrown
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/cluster-devel/attachments/20180403/dc7b3c88/attachment.sig>

#11 lore raw
Quoted 4 lines · NeilBrown

Do we really need a rhashtable_walk_peek() interface? I imagine that a seqfile ->start function can do:

Quoted 9 lines
  if (*ppos == 0 && last_pos != 0) {
  	rhashtable_walk_exit(&iter);
        rhashtable_walk_enter(&table, &iter);
        last_pos = 0;
  }
  rhashtable_walk_start(&iter);
  if (*ppos == last_pos && iter.p)
  	return iter.p;
  last_pos = *ppos;

We don't want users poking into the internals of iter. If you're suggesting we could simplify rhashtable_walk_peek to just this after your patch then yes possibly. You also need to ensure that not only seqfs users continue to work but also netlink dump users which are slightly different.

Quoted 2 lines

It might be OK to have a function call instead of expecting people to use iter.p directly.

Yes that would definitely be the preferred option.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Acked-by on the v2 series
#12 lore raw
In reply to Andreas Gruenbacher

----- Original Message -----

Quoted 20 lines

Here's a second version of the patch (now a patch set) to eliminate rhashtable_walk_peek in gfs2.

The first patch introduces lockref_put_not_zero, the inverse of lockref_get_not_zero.

The second patch eliminates rhashtable_walk_peek in gfs2. In gfs2_glock_iter_next, the new lockref function from patch one is used to drop a lockref count as long as the count doesn't drop to zero. This is almost always the case; if there is a risk of dropping the last reference, we must defer that to a work queue because dropping the last reference may sleep.

Thanks,
Andreas

Andreas Gruenbacher (2):
lockref: Add lockref_put_not_zero
gfs2: Stop using rhashtable_walk_peek

Quoted 7 lines
 fs/gfs2/glock.c         | 47 ++++++++++++++++++++++++++++-------------------
 include/linux/lockref.h |  1 +
 lib/lockref.c           | 28 ++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 19 deletions(-)

--
2.14.3

Hi,

The patches look good. The big question is whether to add them to this merge window while it's still open. Opinions?

Regards,

Bob Peterson

#13 lore raw
Quoted 3 lines · Bob Peterson

The patches look good. The big question is whether to add them to this merge window while it's still open. Opinions?

We're still hashing out the rhashtable interface so I don't think now is the time to rush things.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#14 lore raw

Herbert Xu <[email protected]> schrieb am Mi. 4. Apr. 2018 um 17:51:

Quoted 7 lines · Bob Peterson
Quoted 3 lines

The patches look good. The big question is whether to add them to this merge window while it's still open. Opinions?

We're still hashing out the rhashtable interface so I don't think now is the time to rush things.

Fair enough. No matter how rhashtable_walk_peek changes, we?ll still need these two patches to fix the glock dump though.

Thanks,
Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/cluster-devel/attachments/20180404/d8bd445e/attachment.htm>

#15 lore raw
Quoted 8 lines · Andreas Gr?nbacher

Herbert Xu <[email protected]> schrieb am Mi. 4. Apr. 2018 um 17:51:

Quoted 5 lines · Bob Peterson
Quoted 3 lines

The patches look good. The big question is whether to add them to this merge window while it's still open. Opinions?

Quoted 6 lines
Quoted 2 lines

We're still hashing out the rhashtable interface so I don't think now is the time to rush things.

Fair enough. No matter how rhashtable_walk_peek changes, we?ll still need these two patches to fix the glock dump though.

Those two patches look fine to me and don't depend on changes to rhashtable, so it is up to you when they go upstream.

However, I think the code can be substantially simplified, particularly once we make rhashtable a little cleverer.
So this is what I'll probably be doing for a similar situation in lustre....

Having examined seqfile closely, it is apparent that if ->start never changes *ppos, and if ->next always increments it (except maybe on error) then

1/ ->next is only ever given a 'pos' that was returned by the previous
call to ->start or ->next. So it should *always* return the next object, after the one previously returned by ->start or ->next. It never needs to 'seek'. The 'traverse()' function in seq_file.c does any seeking needed. ->next needs to increase 'pos' and when walking a dynamic list, it is easiest if it just increments it.

2/ ->start is only called with a pos of:
0 - in this case it should rewind to the start the last pos passed to ->start of ->next
In this case it should return the same thing that was returned last time. If it no longer exists, then the following one should be returned.
one more than the last pos passed to ->start or ->next
In this case it should return the object after the last one returned.

The proposed enhancement to rhashtable_walk* is to add a rhashtable_walk_prev() which returns the previously returned object, if it is still in the table, or NULL. It also enhances rhashtable_walk_start() so that if the previously returned object is still in the table, it is preserved as the current cursor. This means that if you take some action to ensure that the previously returned object remains in the table until the next ->start, then you can reliably walk the table with no duplicates or omissions (unless a concurrent rehash causes duplicates)
If you don't keep the object in the table and it gets removed, then the 'skip' counter is used to find your place, and you might get duplicates or omissions.

NeilBrown
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/cluster-devel/attachments/20180406/2e047ed6/attachment.sig>

#16 lore raw
In reply to Andreas Gruenbacher

----- Original Message -----

Quoted 20 lines

Here's a second version of the patch (now a patch set) to eliminate rhashtable_walk_peek in gfs2.

The first patch introduces lockref_put_not_zero, the inverse of lockref_get_not_zero.

The second patch eliminates rhashtable_walk_peek in gfs2. In gfs2_glock_iter_next, the new lockref function from patch one is used to drop a lockref count as long as the count doesn't drop to zero. This is almost always the case; if there is a risk of dropping the last reference, we must defer that to a work queue because dropping the last reference may sleep.

Thanks,
Andreas

Andreas Gruenbacher (2):
lockref: Add lockref_put_not_zero
gfs2: Stop using rhashtable_walk_peek

Quoted 7 lines
 fs/gfs2/glock.c         | 47 ++++++++++++++++++++++++++++-------------------
 include/linux/lockref.h |  1 +
 lib/lockref.c           | 28 ++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 19 deletions(-)

--
2.14.3

Hi,

Thanks. These two patches are now pushed to the for-next branch of the linux-gfs2 tree:

https://git.kernel.org/pub/scm/linux/kernel/git/gfs2/linux-gfs2.git/commit/?h=for-next&id=450b1f6f56350c630e795f240dc5a77aa8aa2419
https://git.kernel.org/pub/scm/linux/kernel/git/gfs2/linux-gfs2.git/commit/?h=for-next&id=3fd5d3ad35dc44aaf0f28d60cc0eb75887bff54d

Regards,

Bob Peterson
Red Hat File Systems