From: Chao Yu via Linux-f2fs-devel <linux-f2fs-devel@lists.sourceforge.net>
To: jaegeuk@kernel.org
Cc: linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net
Subject: [f2fs-dev] [PATCH v2 03/12] f2fs: cache: introduce shrinker
Date: Mon, 24 Aug 2026 21:47:22 +0800 [thread overview]
Message-ID: <20260824134731.3704352-4-chao@kernel.org> (raw)
In-Reply-To: <20260824134731.3704352-1-chao@kernel.org>
This patch integrates the metadata cache into the F2FS memory shrinker
subsystem to reclaim clean, unreferenced cached blocks under memory
pressure.
It implements f2fs_shrink_cache() using a 3-phase cache reclamin method:
1. isolate clean entries from lru list
2. truncate from radix tree under lock
3. splice un-reclaimed entries back
And hooks the new interface into f2fs_shrink_count() and f2fs_shrink_scan().
Signed-off-by: Chao Yu <chao@kernel.org>
---
fs/f2fs/cache.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++
fs/f2fs/cache.h | 3 ++
fs/f2fs/shrinker.c | 12 +++++++
3 files changed, 97 insertions(+)
diff --git a/fs/f2fs/cache.c b/fs/f2fs/cache.c
index 1eec5e9e5dbe..379f75599bef 100644
--- a/fs/f2fs/cache.c
+++ b/fs/f2fs/cache.c
@@ -530,3 +530,85 @@ void f2fs_destroy_cache(struct f2fs_cached_block_list *cache)
f2fs_put_cache(entry, true);
goto next;
}
+
+static unsigned long f2fs_do_shrink_cache(struct f2fs_cached_block_list *cache,
+ unsigned long nr_to_scan)
+{
+ struct f2fs_cached_block *entry, *next;
+ LIST_HEAD(dispose_list);
+ LIST_HEAD(keep_list);
+ unsigned long freed = 0;
+ unsigned long isolated = 0;
+
+ /* Phase 1: Isolate candidate entries from LRU list into dispose_list */
+ spin_lock(&cache->list_lock);
+ list_for_each_entry_safe(entry, next, &cache->lru_list, list) {
+ if (isolated >= nr_to_scan)
+ break;
+
+ if (f2fs_cache_test_dirty(entry) ||
+ f2fs_cache_test_writeback(entry) ||
+ f2fs_cache_test_locked(entry))
+ continue;
+
+ if (f2fs_cache_refcount(entry) != 1)
+ continue;
+
+ list_move_tail(&entry->list, &dispose_list);
+ isolated++;
+ }
+ spin_unlock(&cache->list_lock);
+
+ /* Phase 2: Process isolated candidates one by one */
+ while (1) {
+ spin_lock(&cache->list_lock);
+ entry = list_first_entry_or_null(&dispose_list,
+ struct f2fs_cached_block, list);
+ if (!entry) {
+ spin_unlock(&cache->list_lock);
+ break;
+ }
+ f2fs_cache_get(entry);
+ list_move_tail(&entry->list, &keep_list);
+ spin_unlock(&cache->list_lock);
+
+ if (!f2fs_trylock_cache(entry)) {
+ f2fs_put_cache(entry, false);
+ continue;
+ }
+
+ /* the entry has been truncated */
+ if (!entry->cache) {
+ f2fs_put_cache(entry, true);
+ continue;
+ }
+ /*
+ * at least there are shrinker, radix tree and another user
+ * has referenced the entry.
+ */
+ if (f2fs_cache_refcount(entry) >= 3) {
+ f2fs_put_cache(entry, true);
+ continue;
+ }
+
+ f2fs_do_truncate_cache(entry, false);
+
+ if (f2fs_put_cache(entry, true))
+ freed++;
+ }
+
+ /* Phase 3: Splice un-reclaimed entries back onto cache->lru_list */
+ if (!list_empty(&keep_list)) {
+ spin_lock(&cache->list_lock);
+ list_splice_tail(&keep_list, &cache->lru_list);
+ spin_unlock(&cache->list_lock);
+ }
+
+ return freed;
+}
+
+unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
+ unsigned long nr_to_scan)
+{
+ return f2fs_do_shrink_cache(META_CACHE(sbi), nr_to_scan);
+}
diff --git a/fs/f2fs/cache.h b/fs/f2fs/cache.h
index 7ee98d276938..618b377590da 100644
--- a/fs/f2fs/cache.h
+++ b/fs/f2fs/cache.h
@@ -184,4 +184,7 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
#define f2fs_truncate_meta_caches(sbi, start, len) \
f2fs_drop_cache_range(META_CACHE(sbi), start, len, true)
+unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
+ unsigned long nr_to_scan);
+
#endif /* _LINUX_F2FS_CACHE_H */
diff --git a/fs/f2fs/shrinker.c b/fs/f2fs/shrinker.c
index 4f6bf5926de4..1755c85849e4 100644
--- a/fs/f2fs/shrinker.c
+++ b/fs/f2fs/shrinker.c
@@ -37,6 +37,11 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,
atomic_read(&eti->total_ext_node);
}
+static unsigned long __count_cache(struct f2fs_sb_info *sbi)
+{
+ return sbi->meta_blocks.num_entries;
+}
+
unsigned long f2fs_shrink_count(struct shrinker *shrink,
struct shrink_control *sc)
{
@@ -68,6 +73,9 @@ unsigned long f2fs_shrink_count(struct shrinker *shrink,
/* count free nids cache entries */
count += __count_free_nids(sbi);
+ /* count generic cache entries */
+ count += __count_cache(sbi);
+
spin_lock(&f2fs_list_lock);
p = p->next;
mutex_unlock(&sbi->umount_mutex);
@@ -120,6 +128,10 @@ unsigned long f2fs_shrink_scan(struct shrinker *shrink,
if (freed < nr)
freed += f2fs_try_to_free_nids(sbi, nr - freed);
+ /* shrink generic cache entries */
+ if (freed < nr)
+ freed += f2fs_shrink_cache(sbi, nr - freed);
+
spin_lock(&f2fs_list_lock);
p = p->next;
list_move_tail(&sbi->s_list, &f2fs_list);
--
2.49.0
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
WARNING: multiple messages have this Message-ID (diff)
From: Chao Yu <chao@kernel.org>
To: jaegeuk@kernel.org
Cc: linux-f2fs-devel@lists.sourceforge.net,
linux-kernel@vger.kernel.org, Chao Yu <chao@kernel.org>
Subject: [PATCH v2 03/12] f2fs: cache: introduce shrinker
Date: Mon, 24 Aug 2026 21:47:22 +0800 [thread overview]
Message-ID: <20260824134731.3704352-4-chao@kernel.org> (raw)
In-Reply-To: <20260824134731.3704352-1-chao@kernel.org>
This patch integrates the metadata cache into the F2FS memory shrinker
subsystem to reclaim clean, unreferenced cached blocks under memory
pressure.
It implements f2fs_shrink_cache() using a 3-phase cache reclamin method:
1. isolate clean entries from lru list
2. truncate from radix tree under lock
3. splice un-reclaimed entries back
And hooks the new interface into f2fs_shrink_count() and f2fs_shrink_scan().
Signed-off-by: Chao Yu <chao@kernel.org>
---
fs/f2fs/cache.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++
fs/f2fs/cache.h | 3 ++
fs/f2fs/shrinker.c | 12 +++++++
3 files changed, 97 insertions(+)
diff --git a/fs/f2fs/cache.c b/fs/f2fs/cache.c
index 1eec5e9e5dbe..379f75599bef 100644
--- a/fs/f2fs/cache.c
+++ b/fs/f2fs/cache.c
@@ -530,3 +530,85 @@ void f2fs_destroy_cache(struct f2fs_cached_block_list *cache)
f2fs_put_cache(entry, true);
goto next;
}
+
+static unsigned long f2fs_do_shrink_cache(struct f2fs_cached_block_list *cache,
+ unsigned long nr_to_scan)
+{
+ struct f2fs_cached_block *entry, *next;
+ LIST_HEAD(dispose_list);
+ LIST_HEAD(keep_list);
+ unsigned long freed = 0;
+ unsigned long isolated = 0;
+
+ /* Phase 1: Isolate candidate entries from LRU list into dispose_list */
+ spin_lock(&cache->list_lock);
+ list_for_each_entry_safe(entry, next, &cache->lru_list, list) {
+ if (isolated >= nr_to_scan)
+ break;
+
+ if (f2fs_cache_test_dirty(entry) ||
+ f2fs_cache_test_writeback(entry) ||
+ f2fs_cache_test_locked(entry))
+ continue;
+
+ if (f2fs_cache_refcount(entry) != 1)
+ continue;
+
+ list_move_tail(&entry->list, &dispose_list);
+ isolated++;
+ }
+ spin_unlock(&cache->list_lock);
+
+ /* Phase 2: Process isolated candidates one by one */
+ while (1) {
+ spin_lock(&cache->list_lock);
+ entry = list_first_entry_or_null(&dispose_list,
+ struct f2fs_cached_block, list);
+ if (!entry) {
+ spin_unlock(&cache->list_lock);
+ break;
+ }
+ f2fs_cache_get(entry);
+ list_move_tail(&entry->list, &keep_list);
+ spin_unlock(&cache->list_lock);
+
+ if (!f2fs_trylock_cache(entry)) {
+ f2fs_put_cache(entry, false);
+ continue;
+ }
+
+ /* the entry has been truncated */
+ if (!entry->cache) {
+ f2fs_put_cache(entry, true);
+ continue;
+ }
+ /*
+ * at least there are shrinker, radix tree and another user
+ * has referenced the entry.
+ */
+ if (f2fs_cache_refcount(entry) >= 3) {
+ f2fs_put_cache(entry, true);
+ continue;
+ }
+
+ f2fs_do_truncate_cache(entry, false);
+
+ if (f2fs_put_cache(entry, true))
+ freed++;
+ }
+
+ /* Phase 3: Splice un-reclaimed entries back onto cache->lru_list */
+ if (!list_empty(&keep_list)) {
+ spin_lock(&cache->list_lock);
+ list_splice_tail(&keep_list, &cache->lru_list);
+ spin_unlock(&cache->list_lock);
+ }
+
+ return freed;
+}
+
+unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
+ unsigned long nr_to_scan)
+{
+ return f2fs_do_shrink_cache(META_CACHE(sbi), nr_to_scan);
+}
diff --git a/fs/f2fs/cache.h b/fs/f2fs/cache.h
index 7ee98d276938..618b377590da 100644
--- a/fs/f2fs/cache.h
+++ b/fs/f2fs/cache.h
@@ -184,4 +184,7 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
#define f2fs_truncate_meta_caches(sbi, start, len) \
f2fs_drop_cache_range(META_CACHE(sbi), start, len, true)
+unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
+ unsigned long nr_to_scan);
+
#endif /* _LINUX_F2FS_CACHE_H */
diff --git a/fs/f2fs/shrinker.c b/fs/f2fs/shrinker.c
index 4f6bf5926de4..1755c85849e4 100644
--- a/fs/f2fs/shrinker.c
+++ b/fs/f2fs/shrinker.c
@@ -37,6 +37,11 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,
atomic_read(&eti->total_ext_node);
}
+static unsigned long __count_cache(struct f2fs_sb_info *sbi)
+{
+ return sbi->meta_blocks.num_entries;
+}
+
unsigned long f2fs_shrink_count(struct shrinker *shrink,
struct shrink_control *sc)
{
@@ -68,6 +73,9 @@ unsigned long f2fs_shrink_count(struct shrinker *shrink,
/* count free nids cache entries */
count += __count_free_nids(sbi);
+ /* count generic cache entries */
+ count += __count_cache(sbi);
+
spin_lock(&f2fs_list_lock);
p = p->next;
mutex_unlock(&sbi->umount_mutex);
@@ -120,6 +128,10 @@ unsigned long f2fs_shrink_scan(struct shrinker *shrink,
if (freed < nr)
freed += f2fs_try_to_free_nids(sbi, nr - freed);
+ /* shrink generic cache entries */
+ if (freed < nr)
+ freed += f2fs_shrink_cache(sbi, nr - freed);
+
spin_lock(&f2fs_list_lock);
p = p->next;
list_move_tail(&sbi->s_list, &f2fs_list);
--
2.49.0
next prev parent reply other threads:[~2026-08-24 13:47 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-24 13:47 [f2fs-dev] [PATCH] f2fs: introduce metadata cache Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
2026-08-24 13:47 ` [f2fs-dev] [PATCH v2 01/12] f2fs: cache: implement " Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
2026-08-25 5:39 ` [f2fs-dev] " Wenjie Qi
2026-08-25 5:39 ` Wenjie Qi
2026-08-25 6:35 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-08-25 6:35 ` Chao Yu
2026-09-22 15:30 ` [f2fs-dev] " patchwork-bot+f2fs--- via Linux-f2fs-devel
2026-08-24 13:47 ` [f2fs-dev] [PATCH v2 02/12] f2fs: cache: initialize meta cache Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
2026-08-24 13:47 ` Chao Yu via Linux-f2fs-devel [this message]
2026-08-24 13:47 ` [PATCH v2 03/12] f2fs: cache: introduce shrinker Chao Yu
2026-08-25 5:42 ` [f2fs-dev] " Wenjie Qi
2026-08-25 5:42 ` Wenjie Qi
2026-08-25 6:36 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-08-25 6:36 ` Chao Yu
2026-08-25 5:42 ` [f2fs-dev] " Wenjie Qi
2026-08-25 5:42 ` Wenjie Qi
2026-08-25 6:37 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-08-25 6:37 ` Chao Yu
2026-08-24 13:47 ` [f2fs-dev] [PATCH v2 04/12] f2fs: cache: introduce writeback thread Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
2026-08-24 13:47 ` [f2fs-dev] [PATCH v2 05/12] f2fs: cache: use meta cache Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
2026-08-25 5:42 ` [f2fs-dev] " Wenjie Qi
2026-08-25 5:42 ` Wenjie Qi
2026-08-25 6:37 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-08-25 6:37 ` Chao Yu
2026-08-24 13:47 ` [f2fs-dev] [PATCH v2 06/12] f2fs: cache: initialize node cache Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
2026-08-24 13:47 ` [f2fs-dev] [PATCH v2 07/12] f2fs: cache: use " Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
2026-08-25 5:43 ` [f2fs-dev] " Wenjie Qi
2026-08-25 5:43 ` Wenjie Qi
2026-08-25 6:38 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-08-25 6:38 ` Chao Yu
2026-08-25 5:43 ` [f2fs-dev] " Wenjie Qi
2026-08-25 5:43 ` Wenjie Qi
2026-08-25 6:40 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-08-25 6:40 ` Chao Yu
2026-08-24 13:47 ` [f2fs-dev] [PATCH v2 08/12] f2fs: cache: initialize compress cache Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
2026-08-24 13:47 ` [f2fs-dev] [PATCH v2 09/12] f2fs: cache: use " Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
2026-08-25 5:43 ` [f2fs-dev] " Wenjie Qi
2026-08-25 5:43 ` Wenjie Qi
2026-08-25 6:42 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-08-25 6:42 ` Chao Yu
2026-08-24 13:47 ` [f2fs-dev] [PATCH v2 10/12] f2fs: cache: support fault injection Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
2026-08-24 13:47 ` [f2fs-dev] [PATCH v2 11/12] f2fs: cache: introduce tracepoints Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
2026-08-24 13:47 ` [f2fs-dev] [PATCH v2 12/12] f2fs: cache: show per-cache usage in debugfs Chao Yu via Linux-f2fs-devel
2026-08-24 13:47 ` Chao Yu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260824134731.3704352-4-chao@kernel.org \
--to=linux-f2fs-devel@lists.sourceforge.net \
--cc=chao@kernel.org \
--cc=jaegeuk@kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.