From: Kent Overstreet Date: Fri, 7 Jun 2024 18:25:18 +0000 (-0400) Subject: bcachefs: Leave a buffer in the btree key cache to avoid lock thrashing X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=bf2b356afdcafa18db1b409f7039059d1fd6f25f;p=users%2Fjedix%2Flinux-maple.git bcachefs: Leave a buffer in the btree key cache to avoid lock thrashing Signed-off-by: Kent Overstreet --- diff --git a/fs/bcachefs/btree_key_cache.c b/fs/bcachefs/btree_key_cache.c index 2ad3d27c91e6..2d3c0d45c37f 100644 --- a/fs/bcachefs/btree_key_cache.c +++ b/fs/bcachefs/btree_key_cache.c @@ -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); }