bcachefs: Leave a buffer in the btree key cache to avoid lock thrashing
authorKent Overstreet <kent.overstreet@linux.dev>
Fri, 7 Jun 2024 18:25:18 +0000 (14:25 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Mon, 10 Jun 2024 17:17:16 +0000 (13:17 -0400)
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/btree_key_cache.c

index 2ad3d27c91e6fe5bd3647586e08e51ed2b4b592a..2d3c0d45c37f9322941fa22b110eb411ac3e9eea 100644 (file)
@@ -915,6 +915,14 @@ static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
        long nr = atomic_long_read(&bc->nr_keys) -
                atomic_long_read(&bc->nr_dirty);
 
+       /*
+        * Avoid hammering our shrinker too much if it's nearly empty - the
+        * shrinker code doesn't take into account how big our cache is, if it's
+        * mostly empty but the system is under memory pressure it causes nasty
+        * lock contention:
+        */
+       nr -= 128;
+
        return max(0L, nr);
 }