f2fs: cache: shrink meta and node caches in f2fs_balance_fs_bg

This patch introduces META_BLOCK and NODE_BLOCK memory types to monitor
meta and node cache usage against available free memory, and integrates
them into f2fs_balance_fs_bg().

In f2fs_balance_fs_bg(), shrink 128 clean cached blocks of each types when
memory is insufficient (cache memory usage is larger than 50% of low memory).

Signed-off-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
This commit is contained in:
Chao Yu 2026-10-07 11:49:46 +00:00 committed by Jaegeuk Kim
parent e881f4730f
commit 7f35dce566
5 changed files with 38 additions and 0 deletions

View File

@ -669,6 +669,18 @@ unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
return freed;
}
unsigned long f2fs_shrink_meta_cache(struct f2fs_sb_info *sbi,
unsigned long nr_to_scan)
{
return f2fs_do_shrink_cache(META_CACHE(sbi), nr_to_scan);
}
unsigned long f2fs_shrink_node_cache(struct f2fs_sb_info *sbi,
unsigned long nr_to_scan)
{
return f2fs_do_shrink_cache(NODE_CACHE(sbi), nr_to_scan);
}
static int f2fs_cache_writeback_kthread(void *data)
{
struct f2fs_sb_info *sbi = data;

View File

@ -84,6 +84,8 @@ enum f2fs_cache_request_flag {
#define F2FS_ONSTACK_CACHES (32)
#define METADATA_CACHE_SHRINK_NUMBER (128)
#define F2FS_CACHE_FLAG_TEST_FUNC(name, flagname) \
static inline bool f2fs_cache_test_##name( \
const struct f2fs_cached_block *entry) \
@ -223,6 +225,10 @@ void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,
unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
unsigned long nr_to_scan);
unsigned long f2fs_shrink_meta_cache(struct f2fs_sb_info *sbi,
unsigned long nr_to_scan);
unsigned long f2fs_shrink_node_cache(struct f2fs_sb_info *sbi,
unsigned long nr_to_scan);
#define DEF_DIRTY_CACHE_TIMEOUT 5000
#define MIN_DIRTY_CACHE_TIMEOUT 100

View File

@ -123,6 +123,16 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
#else
res = false;
#endif
} else if (type == META_BLOCK) {
mem_size = (META_CACHE(sbi)->num_entries *
(sizeof(struct f2fs_cached_block) +
F2FS_BLKSIZE(sbi))) >> PAGE_SHIFT;
res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
} else if (type == NODE_BLOCK) {
mem_size = (NODE_CACHE(sbi)->num_entries *
(sizeof(struct f2fs_cached_block) +
F2FS_BLKSIZE(sbi))) >> PAGE_SHIFT;
res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
} else {
if (!bdi_wb_dirty_exceeded(sbi->sb->s_bdi))
return true;

View File

@ -158,6 +158,8 @@ enum mem_type {
AGE_EXTENT_CACHE, /* indicates age extent cache */
DISCARD_CACHE, /* indicates memory of cached discard cmds */
COMPRESS_BLOCK, /* indicates memory of cached compressed blocks */
META_BLOCK, /* indicates memory of cached meta blocks */
NODE_BLOCK, /* indicates memory of cached node blocks */
BASE_CHECK, /* check kernel status */
};

View File

@ -510,6 +510,14 @@ void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg)
f2fs_shrink_age_extent_tree(sbi,
AGE_EXTENT_CACHE_SHRINK_NUMBER);
/* try to shrink meta cache when there is no enough memory */
if (!f2fs_available_free_memory(sbi, META_BLOCK))
f2fs_shrink_meta_cache(sbi, METADATA_CACHE_SHRINK_NUMBER);
/* try to shrink node cache when there is no enough memory */
if (!f2fs_available_free_memory(sbi, NODE_BLOCK))
f2fs_shrink_node_cache(sbi, METADATA_CACHE_SHRINK_NUMBER);
/* check the # of cached NAT entries */
if (!f2fs_available_free_memory(sbi, NAT_ENTRIES))
f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK(sbi));