f2fs: cache: wake up f2fs_writeback when exceeding threshold
Chao Yu
maintainerPATCH v3 3/5 f2fs: cache: wake up f2fs_writeback when exceeding threshold
From: Chao Yu <[email protected]>
This patch introduces cache_wb_dirty_threshold and cache_wb_total_threshold sysfs nodes under /sys/fs/f2fs/<disk>/, and wakes up the f2fs_writeback kthread when dirty meta/node caches exceed cache_wb_dirty_threshold and total cached entries exceed cache_wb_total_threshold; a zero threshold disables the corresponding check. By default, cache_wb_dirty_threshold is 8192 and cache_wb_total_threshold is 0.
If both thresholds are zero, f2fs_writeback only runs periodically, unless checkpoint is disabled, as nothing else will flush dirty caches then.
f2fs_writeback won't be woken up unless there are enough dirty caches for f2fs_write_{meta,node}_caches() to gather. And if thresholds are exceeded but one round fails to write any caches, e.g. due to cp_error, freezing, frozen fs, or lock contention, threshold based wakeup is throttled until a later round makes progress, in order to avoid busy-looping. To detect progress, introduce a new parameter for f2fs_write_{meta,node}_caches() and f2fs_writeback_node_caches() to return the number of caches submitted for writeback.
Thanks Daeho and Wenjie for help and suggestion on this patch.
---
v3:
- fix to throttle cache writeback in f2fs_writeback
- fix to handle chckpoint disable case correctly
Documentation/ABI/testing/sysfs-fs-f2fs | 21 ++++ fs/f2fs/cache.c | 124 ++++++++++++++++++++++-- fs/f2fs/cache.h | 6 ++ fs/f2fs/checkpoint.c | 10 +- fs/f2fs/f2fs.h | 7 +- fs/f2fs/node.c | 11 ++- fs/f2fs/sysfs.c | 8 +- 7 files changed, 171 insertions(+), 16 deletions(-)
Documentation/ABI/testing/sysfs-fs-f2fs +21 -0
@@ -1027,3 +1027,24 @@ Contact: "Chao Yu" <[email protected]> Description: This is a writable entry to control writeback interval of f2fs_writeback-x:y, the range is [100, 30000], by default the value is 5000, unit is ms. + +What: /sys/fs/f2fs/<disk>/cache_wb_dirty_threshold +Date: October 2026 +Contact: "Chao Yu" <[email protected]> +Description: This is a writable entry for metadata cache. f2fs_writeback-x:y + will be woken up for writeback once the number of dirty meta and + node caches reaches this value. 0 disables this check. By default + the value is 8192, unit is cache entries. + +What: /sys/fs/f2fs/<disk>/cache_wb_total_threshold +Date: October 2026 +Contact: "Chao Yu" <[email protected]> +Description: This is a writable entry for metadata cache. f2fs_writeback-x:y + will be woken up for writeback only if the total number of cached + entries also reaches this value. 0 disables this check. By default + the value is 0, unit is cache entries. + + If both cache_wb_dirty_threshold and cache_wb_total_threshold are + 0, f2fs_writeback-x:y only runs every cache_wb_interval, unless + checkpoint is disabled, in which case it will still be woken up + once there are enough dirty caches to be gathered for writeback.
fs/f2fs/cache.c +118 -6
177 lines not on this page · raw message
fs/f2fs/cache.h +6 -0
@@ -234,13 +234,19 @@ unsigned long f2fs_shrink_node_cache(struct f2fs_sb_info *sbi,#define MIN_DIRTY_CACHE_TIMEOUT 100#define MAX_DIRTY_CACHE_TIMEOUT 30000 +#define DEF_CACHE_WB_DIRTY_THRESH (8192) +struct f2fs_cache_kthread {struct task_struct *cache_wb_task;wait_queue_head_t cache_wb_wq;unsigned int cache_wb_interval; +unsigned int cache_wb_dirty_threshold; +unsigned int cache_wb_total_threshold; +bool cache_wb_throttled;/* last round made no progress */ };int f2fs_start_cache_wb_thread (struct f2fs_sb_info *sbi);void f2fs_stop_cache_wb_thread (struct f2fs_sb_info *sbi); +void f2fs_wake_up_cache_wb (struct f2fs_sb_info *sbi);#endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/checkpoint.c +7 -3
@@ -581,10 +581,11 @@ static bool __f2fs_write_meta_cache(struct f2fs_cached_block *entry,return false; } -void f2fs_write_meta_caches (struct f2fs_sb_info *sbi) +void f2fs_write_meta_caches (struct f2fs_sb_info *sbi,long *nr_written) {struct f2fs_lock_context lc;long nr_to_write =LONG_MAX ; +long nwritten;if (unlikely (is_sbi_flag_set (sbi,SBI_POR_DOING )))return ;
@@ -599,8 +600,11 @@ void f2fs_write_meta_caches(struct f2fs_sb_info *sbi)return ; nr_to_write =adjust_flush_cache_number (sbi,META ); -f2fs_sync_meta_caches (sbi, nr_to_write, false,FS_META_IO ); + nwritten =f2fs_sync_meta_caches (sbi, nr_to_write, false,FS_META_IO );f2fs_up_write_trace (&sbi->cp_global_sem, &lc); + +if (nr_written) + *nr_written = nwritten; } long f2fs_sync_meta_caches(struct f2fs_sb_info *sbi, long nr_to_write,
@@ -1553,7 +1557,7 @@ static int block_operations(struct f2fs_sb_info *sbi)f2fs_up_write (&sbi->node_write);atomic_inc (&sbi->wb_sync_req[NODE ]); err =f2fs_writeback_node_caches (sbi,LONG_MAX , - true, false,FS_CP_NODE_IO ); + true, false,FS_CP_NODE_IO ,NULL );atomic_dec (&sbi->wb_sync_req[NODE ]);if (err) {f2fs_up_write (&sbi->node_change);
fs/f2fs/f2fs.h +4 -3
@@ -4132,7 +4132,7 @@ int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);int f2fs_truncate_xattr_node (struct inode *inode);int f2fs_wait_on_node_caches_writeback (struct f2fs_sb_info *sbi,unsigned int seq_id); -int f2fs_write_node_caches (struct f2fs_sb_info *sbi); +int f2fs_write_node_caches (struct f2fs_sb_info *sbi,long *nr_written);int f2fs_remove_inode_cache (struct inode *inode);struct f2fs_cached_block *f2fs_new_inode_cache (struct inode *inode);struct f2fs_cached_block *f2fs_new_node_cache (struct dnode_of_data *dn,unsigned int ofs);
@@ -4151,7 +4151,8 @@ void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);int f2fs_fsync_node_caches (struct f2fs_sb_info *sbi,struct inode *inode,bool atomic,unsigned int *seq_id);int f2fs_writeback_node_caches (struct f2fs_sb_info *sbi,long nr_to_write, -bool sync,bool do_balance,enum iostat_type io_type); +bool sync,bool do_balance,enum iostat_type io_type, +long *nr_written);int f2fs_build_free_nids (struct f2fs_sb_info *sbi,bool sync,bool mount);bool f2fs_alloc_nid (struct f2fs_sb_info *sbi,nid_t *nid);void f2fs_alloc_nid_done (struct f2fs_sb_info *sbi,nid_t nid);
@@ -4317,7 +4318,7 @@ int f2fs_ra_meta_caches(struct f2fs_sb_info *sbi, block_t start, int nrpages, int type, bool sync);void f2fs_ra_meta_caches_cond (struct f2fs_sb_info *sbi,pgoff_t index,unsigned int ra_blocks); -void f2fs_write_meta_caches (struct f2fs_sb_info *sbi); +void f2fs_write_meta_caches (struct f2fs_sb_info *sbi,long *nr_written);long f2fs_sync_meta_caches (struct f2fs_sb_info *sbi,long nr_to_write,bool sync,enum iostat_type io_type);void f2fs_add_ino_entry (struct f2fs_sb_info *sbi,nid_t ino,int type);
fs/f2fs/node.c +8 -3
@@ -2111,7 +2111,8 @@ void f2fs_flush_inline_data(struct f2fs_sb_info *sbi) }int f2fs_writeback_node_caches (struct f2fs_sb_info *sbi,long nr_to_write, -bool sync,bool do_balance,enum iostat_type io_type) +bool sync,bool do_balance,enum iostat_type io_type, +long *nr_written) {pgoff_t index;struct f2fs_cached_block *entries[F2FS_ONSTACK_CACHES ];
@@ -2231,6 +2232,9 @@ int f2fs_writeback_node_caches(struct f2fs_sb_info *sbi, long nr_to_write,trace_f2fs_write_caches (sbi, nr_to_write, nwritten,NODE ); +if (nr_written) + *nr_written = nwritten; +if (unlikely (f2fs_cp_error (sbi)))return -EIO ;return ret;
@@ -2271,7 +2275,7 @@ int f2fs_wait_on_node_caches_writeback(struct f2fs_sb_info *sbi,return f2fs_cp_error (sbi) ? -EIO :0 ; } -int f2fs_write_node_caches (struct f2fs_sb_info *sbi) +int f2fs_write_node_caches (struct f2fs_sb_info *sbi,long *nr_written) {struct blk_plug plug;long nr_to_write =LONG_MAX ;
@@ -2296,7 +2300,8 @@ int f2fs_write_node_caches(struct f2fs_sb_info *sbi) nr_to_write =adjust_flush_cache_number (sbi,NODE );blk_start_plug (&plug); -f2fs_writeback_node_caches (sbi, nr_to_write, false, true,FS_NODE_IO ); +f2fs_writeback_node_caches (sbi, nr_to_write, false, true,FS_NODE_IO , + nr_written);blk_finish_plug (&plug);return 0 ; }
fs/f2fs/sysfs.c +7 -1
@@ -1019,7 +1019,9 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a, a->struct_type == GC_THREAD);bool thread_entry = !strcmp (a->attr.name,"ckpt_thread_ioprio" ) || !strcmp (a->attr.name,"critical_task_priority" ) || - !strcmp (a->attr.name,"cache_wb_interval" ); + !strcmp (a->attr.name,"cache_wb_interval" ) || + !strcmp (a->attr.name,"cache_wb_dirty_threshold" ) || + !strcmp (a->attr.name,"cache_wb_total_threshold" );if (gc_entry || thread_entry) {if (!down_read_trylock (&sbi->sb->s_umount))
@@ -1363,6 +1365,8 @@ ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);/* WB_THREAD ATTR */ WB_THREAD_RW_ATTR (cache_wb_interval, cache_wb_interval); +WB_THREAD_RW_ATTR (cache_wb_dirty_threshold, cache_wb_dirty_threshold); +WB_THREAD_RW_ATTR (cache_wb_total_threshold, cache_wb_total_threshold);F2FS_GENERAL_RO_ATTR (dirty_segments);F2FS_GENERAL_RO_ATTR (free_segments);
@@ -1552,6 +1556,8 @@ static struct attribute *f2fs_attrs[] = {ATTR_LIST (adjust_lock_priority),ATTR_LIST (critical_task_priority),ATTR_LIST (cache_wb_interval), +ATTR_LIST (cache_wb_dirty_threshold), +ATTR_LIST (cache_wb_total_threshold),NULL , };ATTRIBUTE_GROUPS (f2fs);
Signature · 1 line
2.49.0
Hi Chao,
Quoted 2 lines
f2fs_writebackwon't be woken up unless there are enough dirty caches for f2fs_write_{meta,node}_caches() to gather.
The collectable-cache check and no-progress throttling address my concerns.
Best regards,
Wenjie
This patch was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:
Hello:
This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:
Quoted 10 lines · On Mon, 12 Oct 2026 09:17:37 +0000 you wrote:
From: Chao Yu <[email protected]>
This patch introduces
cache_wb_dirty_thresholdandcache_wb_total_thresholdsysfs nodes under /sys/fs/f2fs/<disk>/, and wakes up thef2fs_writebackkthread when dirty meta/node caches exceedcache_wb_dirty_thresholdand total cached entries exceedcache_wb_total_threshold; a zero threshold disables the corresponding check. By default,cache_wb_dirty_thresholdis 8192 andcache_wb_total_thresholdis 0.[...]
Here is the summary with links:
- [f2fs-dev,v3,3/5] f2fs: cache: wake up f2fs_writeback when exceeding threshold
https://git.kernel.org/jaegeuk/f2fs/c/eeadc4807c95
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html