Skip to contentSkip to conversation

f2fs: cache: wake up f2fs_writeback when exceeding threshold

PATCH Superseded Chao Yu 2 participants 3 messages Oct 9 – Oct 12 F2FS file system

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Chao Yu

author maintainer
#0 lore raw

PATCH v2 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 allows waking up the f2fs_writeback kthread when both thresholds are exceeded.

By default, cache_wb_total_threshold is zero, so if dirty cache number exceeds cache_wb_dirty_threshold, it will trigger writebacking.

---
v2:
- don't wake up f2fs_writeback if both thresholds are zero.

 Documentation/ABI/testing/sysfs-fs-f2fs | 14 +++++++
 fs/f2fs/cache.c                         | 55 ++++++++++++++++++++++++-
 fs/f2fs/cache.h                         |  5 +++
 fs/f2fs/sysfs.c                         |  8 +++-
 4 files changed, 80 insertions(+), 2 deletions(-)
Documentation/ABI/testing/sysfs-fs-f2fs +14 -0
index 5b196806b3bc..f50739f90ae9 100644
@@ -1027,3 +1027,17 @@ 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, it is used to control
+		dirty cache threshold of to wake up f2fs_writeback-x:y for writeback,
+		by default the value is 8192, unit is blocks.
+
+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, it is used to control
+		total cache threshold to wake up f2fs_writeback-x:y for writeback,
+		by default the value is 0, unit is blocks.
fs/f2fs/cache.c +54 -1

85 lines not on this page · raw message

fs/f2fs/cache.h +5 -0
index 3df3a562c46f..7a94fb8139d3 100644
@@ -234,13 +234,18 @@ 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;
 };
 
 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/sysfs.c +7 -1
index afce74744d4d..95f8dc0c4218 100644
@@ -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
#1 lore raw

Hi Chao,

fs/f2fs/cache.c unsigned long f2fs_shrink_node_cache(struct f2fs_sb_info *sbi, · line 720

Quoted 5 lines
fs/f2fs/cache.c +5 -0
+	if (dirty_threshold && nr_dirty < dirty_threshold)
+		return false;
+
+	/* don't wake up f2fs_writeback if both thresholds are zero */
+	return total_threshold || dirty_threshold;

There is a related case with only the total threshold enabled. With cache_wb_dirty_threshold=0 and cache_wb_total_threshold=1, clean cached entries keep the predicate true even when there are no dirty meta/node caches. Writeback does not necessarily evict those entries, so the thread can keep looping without sleeping.

I checked this in QEMU using dev-test plus my both-zero fix, which has the same predicate for these fixed threshold values. In a three-second idle interval, the writeback thread used about three CPU seconds; the total cache count was 22 and the dirty count was zero at both sample points.

Should the thread wait when no dirty caches remain, even if the total threshold is still met, or is different behavior intended for this mode?

Best regards,
Wenjie

author maintainer
#2 lore raw

Hi Wenjie,

Thanks a lot for the review.

Quoted 14 lines · Wenjie Qi

Hi Chao,

fs/f2fs/cache.c unsigned long f2fs_shrink_node_cache(struct f2fs_sb_info *sbi, · line 720

Quoted 5 lines
fs/f2fs/cache.c +5 -0
+	if (dirty_threshold && nr_dirty < dirty_threshold)
+		return false;
+
+	/* don't wake up f2fs_writeback if both thresholds are zero */
+	return total_threshold || dirty_threshold;

There is a related case with only the total threshold enabled. With cache_wb_dirty_threshold=0 and cache_wb_total_threshold=1, clean cached entries keep the predicate true even when there are no dirty meta/node caches. Writeback does not necessarily evict those entries, so the thread can keep looping without sleeping.

Quoted 8 lines

I checked this in QEMU using dev-test plus my both-zero fix, which has the same predicate for these fixed threshold values. In a three-second idle interval, the writeback thread used about three CPU seconds; the total cache count was 22 and the dirty count was zero at both sample points.

Should the thread wait when no dirty caches remain, even if the total threshold is still met, or is different behavior intended for this mode?

Thanks for the test, yes, it should wait. In v3, the thread won't be woken up unless there are enough dirty caches for f2fs_write_{meta,node}_caches() to gather (i.e. dirty count reaches nr_caches_to_skip()), regardless of the total threshold. Also, threshold based wakeup will be throttled once a round fails to write anything.

With your setting (dirty_threshold=0, total_threshold=1), I can see the writeback thread uses 0.0% cpu in idle state, w/ either few or thousands of clean cached entries, and w/ dirty caches below the threshold under checkpoint=disable.

Thanks,

Quoted 3 lines

Best regards,
Wenjie