bcachefs: Make bch2_btree_cache_scan() try harder
authorKent Overstreet <kent.overstreet@gmail.com>
Thu, 3 Mar 2022 16:04:01 +0000 (11:04 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:26 +0000 (17:09 -0400)
Previously, when bch2_btree_cache_scan() attempted to reclaim a node but
failed (because trylock failed, because it was dirty, etc.), it would
count that against the number of nodes it was scanning and attempting to
free. This patch changes that behaviour, so that now we only count nodes
that we then don't free if they have the accessed bit (which we also
clear).

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
fs/bcachefs/btree_cache.c

index 5f96c5d1a064aeb9a79f9b8436f7f262890fc6ef..0976b9d7a619231c8b1cd6d5c20ddb0a4f05e9d3 100644 (file)
@@ -327,17 +327,13 @@ static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
        }
 restart:
        list_for_each_entry_safe(b, t, &bc->live, list) {
-               touched++;
-
-               if (touched >= nr) {
-                       /* Save position */
-                       if (&t->list != &bc->live)
-                               list_move_tail(&bc->live, &t->list);
-                       break;
+               /* tweak this */
+               if (btree_node_accessed(b)) {
+                       clear_btree_node_accessed(b);
+                       goto touched;
                }
 
-               if (!btree_node_accessed(b) &&
-                   !btree_node_reclaim(c, b)) {
+               if (!btree_node_reclaim(c, b)) {
                        /* can't call bch2_btree_node_hash_remove under lock  */
                        freed++;
                        if (&t->list != &bc->live)
@@ -358,8 +354,18 @@ restart:
                        else if (!mutex_trylock(&bc->lock))
                                goto out;
                        goto restart;
-               } else
-                       clear_btree_node_accessed(b);
+               } else {
+                       continue;
+               }
+touched:
+               touched++;
+
+               if (touched >= nr) {
+                       /* Save position */
+                       if (&t->list != &bc->live)
+                               list_move_tail(&bc->live, &t->list);
+                       break;
+               }
        }
 
        mutex_unlock(&bc->lock);