Skip to contentSkip to conversation

gfs2: glock shrinker reform

PATCH In review Bob Peterson 2 participants 2 messages Sep 7, 2023 GFS2 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gfs2: glock shrinker reform

Before this patch the gfs2 glock shrinker had several problems. Inode glocks would often be put onto the LRU with a reference count of 2, in the EX state, until a demote request came in from another node.

When the shrinker called count_objects, gfs2_glock_shrink_count returned the number of glocks on the LRU, which is wrong. According to shrinker.h it should return "value for the number of freeable objects" but not all of glocks on the LRU are freeable.

The shrinker itself only freed glocks if the reference count was zero. But if the reference count ever got to zero, __gfs2_glock_put would have already removed them from the LRU and freed them (via lm_put_lock).

The net result is that, under memory pressure, the vfs shrinker asked gfs2 how many glocks were eligible to be freed, and it responded "All of them on the LRU." But when it tried to free them, the reference counts were 1 or 2, so none were freed. Since none were freed, the vfs shrinker would repeatedly call the gfs2 shrinker hundreds or thousands of times, none of which did any real work.

This patch changes gfs2_scan_glock_lru so glocks are counted as freeable if their reference count is <= 2 AND if they are in the Unlocked state or can be demoted. A parameter was added to indicate whether the eligible glocks should be freed or merely counted.

The gfs2_glock_shrink_count function then calls it with false, and if none are returned, returns SHRINK_EMPTY as per the shrinker requirements. The gfs2_glock_shrink_scan calls it with true, and frees any that qualify.

---
 fs/gfs2/glock.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
fs/gfs2/glock.c +16 -9
index 9cbf8d98489a..f31c2e040595 100644
@@ -1990,17 +1990,18 @@ __acquires(&lru_lock)
 /**
  * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
  * @nr: The number of entries to scan
+ * @do_free: if true, eligible glocks are freed, if false, they are counted.
  *
  * This function selects the entries on the LRU which are able to
  * be demoted, and then kicks off the process by calling
  * gfs2_dispose_glock_lru() above.
  */
 
-static long gfs2_scan_glock_lru(int nr)
+static long gfs2_scan_glock_lru(int nr, bool do_free)
 {
 	struct gfs2_glock *gl, *next;
 	LIST_HEAD(dispose);
-	long freed = 0;
+	long freeable = 0;
 
 	spin_lock(&lru_lock);
 	list_for_each_entry_safe(gl, next, &lru_list, gl_lru) {
@@ -2010,10 +2011,14 @@ static long gfs2_scan_glock_lru(int nr)
 		if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
 			if (!spin_trylock(&gl->gl_lockref.lock))
 				continue;
-			if (!gl->gl_lockref.count) {
-				list_move(&gl->gl_lru, &dispose);
-				atomic_dec(&lru_count);
-				freed++;
+			if (gl->gl_lockref.count <= 2 &&
+			    (gl->gl_state == LM_ST_UNLOCKED ||
+			     demote_ok(gl))) {
+				if (do_free) {
+					list_move(&gl->gl_lru, &dispose);
+					atomic_dec(&lru_count);
+				}
+				freeable++;
 			}
 			spin_unlock(&gl->gl_lockref.lock);
 		}
@@ -2022,7 +2027,7 @@ static long gfs2_scan_glock_lru(int nr)
 		gfs2_dispose_glock_lru(&dispose);
 	spin_unlock(&lru_lock);
 
-	return freed;
+	return freeable;
 }
 
 static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
@@ -2030,13 +2035,15 @@ static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
 {
 	if (!(sc->gfp_mask & __GFP_FS))
 		return SHRINK_STOP;
-	return gfs2_scan_glock_lru(sc->nr_to_scan);
+	return gfs2_scan_glock_lru(sc->nr_to_scan, true);
 }
 
 static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
 					     struct shrink_control *sc)
 {
-	return vfs_pressure_ratio(atomic_read(&lru_count));
+	long freeable = gfs2_scan_glock_lru(INT_MAX, false);
+
+	return freeable ? vfs_pressure_ratio(freeable) : SHRINK_EMPTY;
 }
 
 static struct shrinker glock_shrinker = {
Signature · 1 line
2.41.0
#1 lore raw
Quoted 3 lines · Bob Peterson

Before this patch the gfs2 glock shrinker had several problems. Inode glocks would often be put onto the LRU with a reference count of 2, in the EX state, until a demote request came in from another node.

What are those references being held for?

Quoted 8 lines

When the shrinker called count_objects, gfs2_glock_shrink_count returned the number of glocks on the LRU, which is wrong. According to shrinker.h it should return "value for the number of freeable objects" but not all of glocks on the LRU are freeable.

The shrinker itself only freed glocks if the reference count was zero. But if the reference count ever got to zero, __gfs2_glock_put would have already removed them from the LRU and freed them (via lm_put_lock).

So apparently this means being in the cache counts as one reference. That doesn't seem to be documented.

Quoted 11 lines

The net result is that, under memory pressure, the vfs shrinker asked gfs2 how many glocks were eligible to be freed, and it responded "All of them on the LRU." But when it tried to free them, the reference counts were 1 or 2, so none were freed. Since none were freed, the vfs shrinker would repeatedly call the gfs2 shrinker hundreds or thousands of times, none of which did any real work.

This patch changes gfs2_scan_glock_lru so glocks are counted as freeable if their reference count is <= 2 AND if they are in the Unlocked state or can be demoted. A parameter was added to indicate whether the eligible glocks should be freed or merely counted.

This looks highly suspicious and either not thought through, or severely lacking in documentation. What sense does it make to try disposing of a glock that's still referenced?

Once that is fixed, we can make sure we're always holding a reference on the sd_jindex and sd_rindex glocks throughout, and the go_demote_ok glock operation can be removed.

Can this code be changed to manage the LRU in a more sensible way, by only putting objects on the LRU when they can actually be disposed of?

With this patch, the value of lru_count isn't ever looked at, so the counter is now useless.

Thanks,
Andreas

Quoted 85 lines

The gfs2_glock_shrink_count function then calls it with false, and if none are returned, returns SHRINK_EMPTY as per the shrinker requirements. The gfs2_glock_shrink_scan calls it with true, and frees any that qualify.

---
 fs/gfs2/glock.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
fs/gfs2/glock.c +16 -10
index 9cbf8d98489a..f31c2e040595 100644
@@ -1990,17 +1990,18 @@ __acquires(&lru_lock)
 /**
  * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
  * @nr: The number of entries to scan
+ * @do_free: if true, eligible glocks are freed, if false, they are counted.
  *
  * This function selects the entries on the LRU which are able to
  * be demoted, and then kicks off the process by calling
  * gfs2_dispose_glock_lru() above.
  */
 
-static long gfs2_scan_glock_lru(int nr)
+static long gfs2_scan_glock_lru(int nr, bool do_free)
 {
        struct gfs2_glock *gl, *next;
        LIST_HEAD(dispose);
-       long freed = 0;
+       long freeable = 0;
 
        spin_lock(&lru_lock);
        list_for_each_entry_safe(gl, next, &lru_list, gl_lru) {
@@ -2010,10 +2011,14 @@ static long gfs2_scan_glock_lru(int nr)
                if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
                        if (!spin_trylock(&gl->gl_lockref.lock))
                                continue;
-                       if (!gl->gl_lockref.count) {
-                               list_move(&gl->gl_lru, &dispose);
-                               atomic_dec(&lru_count);
-                               freed++;
+                       if (gl->gl_lockref.count <= 2 &&
+                           (gl->gl_state == LM_ST_UNLOCKED ||
+                            demote_ok(gl))) {
+                               if (do_free) {
+                                       list_move(&gl->gl_lru, &dispose);
+                                       atomic_dec(&lru_count);
+                               }
+                               freeable++;
                        }
                        spin_unlock(&gl->gl_lockref.lock);
                }
@@ -2022,7 +2027,7 @@ static long gfs2_scan_glock_lru(int nr)
                gfs2_dispose_glock_lru(&dispose);
        spin_unlock(&lru_lock);
 
-       return freed;
+       return freeable;
 }
 
 static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
@@ -2030,13 +2035,15 @@ static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
 {
        if (!(sc->gfp_mask & __GFP_FS))
                return SHRINK_STOP;
-       return gfs2_scan_glock_lru(sc->nr_to_scan);
+       return gfs2_scan_glock_lru(sc->nr_to_scan, true);
 }
 
 static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
                                             struct shrink_control *sc)
 {
-       return vfs_pressure_ratio(atomic_read(&lru_count));
+       long freeable = gfs2_scan_glock_lru(INT_MAX, false);
+
+       return freeable ? vfs_pressure_ratio(freeable) : SHRINK_EMPTY;
 }
 
 static struct shrinker glock_shrinker = {
--

2.41.0