[PATCH 7/7] f2fs: cache: fix to initialize f2fs_writeback info at mount time

From: Chao Yu

Date: Thu Oct 22 2026 - 08:58:12 EST


From: Chao Yu <chao@xxxxxxxxxx>

f2fs_start_cache_wb_thread() initializes cache_wb_wq and f2fs_writeback
tunables every time f2fs_writeback thread is started, e.g. in remount,
which causes below race conditions:

1. cache_wb_interval, cache_wb_dirty_threshold and
cache_wb_total_threshold configured via sysfs will be reset to
default value after remount.

2. remount will reinitialize cache_wb_wq, which may be used by another
thread, e.g. f2fs_wake_up_cache_wb(), concurrently.

Let's introduce f2fs_init_cache_wb_info() to initialize them only once
in f2fs_fill_super(), then f2fs_start_cache_wb_thread() only needs to
reset cache_wb_throttled, and READ_ONCE()/WRITE_ONCE() is enough for
cache_wb_task.

Cc: stable@xxxxxxxxxx
Fixes: f45afeabd5df ("f2fs: cache: introduce writeback thread")
Fixes: eeadc4807c95 ("f2fs: cache: wake up f2fs_writeback when exceeding threshold")
Signed-off-by: Chao Yu <chao@xxxxxxxxxx>
---
fs/f2fs/cache.c | 23 ++++++++++++++---------
fs/f2fs/cache.h | 1 +
fs/f2fs/super.c | 1 +
3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/fs/f2fs/cache.c b/fs/f2fs/cache.c
index ddf94b5791e7..6cac7662fc1a 100644
--- a/fs/f2fs/cache.c
+++ b/fs/f2fs/cache.c
@@ -844,8 +844,7 @@ void f2fs_wake_up_cache_wb(struct f2fs_sb_info *sbi)
{
struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;

- /* pairs with smp_store_release() in f2fs_start_cache_wb_thread() */
- if (!smp_load_acquire(&cache_thread->cache_wb_task))
+ if (!READ_ONCE(cache_thread->cache_wb_task))
return;

if (!f2fs_should_wake_up_cache_wb(sbi))
@@ -855,6 +854,17 @@ void f2fs_wake_up_cache_wb(struct f2fs_sb_info *sbi)
wake_up(&cache_thread->cache_wb_wq);
}

+void f2fs_init_cache_wb_info(struct f2fs_sb_info *sbi)
+{
+ struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;
+
+ init_waitqueue_head(&cache_thread->cache_wb_wq);
+ cache_thread->cache_wb_interval = DEF_DIRTY_CACHE_TIMEOUT;
+ cache_thread->cache_wb_dirty_threshold = DEF_CACHE_WB_DIRTY_THRESH;
+ cache_thread->cache_wb_total_threshold = 0;
+ cache_thread->cache_wb_throttled = false;
+}
+
int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi)
{
struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;
@@ -865,11 +875,7 @@ int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi)
if (cache_thread->cache_wb_task)
return 0;

- init_waitqueue_head(&cache_thread->cache_wb_wq);
- cache_thread->cache_wb_interval = DEF_DIRTY_CACHE_TIMEOUT;
- cache_thread->cache_wb_dirty_threshold = DEF_CACHE_WB_DIRTY_THRESH;
- cache_thread->cache_wb_total_threshold = 0;
- cache_thread->cache_wb_throttled = false;
+ WRITE_ONCE(cache_thread->cache_wb_throttled, false);
snprintf(name, sizeof(name), "f2fs_writeback-%u:%u",
MAJOR(dev), MINOR(dev));

@@ -877,8 +883,7 @@ int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi)
if (IS_ERR(task))
return PTR_ERR(task);

- /* pairs with smp_load_acquire() in f2fs_wake_up_cache_wb() */
- smp_store_release(&cache_thread->cache_wb_task, task);
+ WRITE_ONCE(cache_thread->cache_wb_task, task);
return 0;
}

diff --git a/fs/f2fs/cache.h b/fs/f2fs/cache.h
index 5408727dd321..cd017c1b188f 100644
--- a/fs/f2fs/cache.h
+++ b/fs/f2fs/cache.h
@@ -245,6 +245,7 @@ struct f2fs_cache_kthread {
bool cache_wb_throttled; /* last round made no progress */
};

+void f2fs_init_cache_wb_info(struct f2fs_sb_info *sbi);
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);
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index ba2ec6123251..12a051ba51ac 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -5356,6 +5356,7 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
f2fs_init_cache(sbi, META_CACHE(sbi), F2FS_META_CACHE);
f2fs_init_cache(sbi, NODE_CACHE(sbi), F2FS_NODE_CACHE);
f2fs_init_cache(sbi, COMPRESS_CACHE(sbi), F2FS_COMPRESS_CACHE);
+ f2fs_init_cache_wb_info(sbi);

err = f2fs_get_valid_checkpoint(sbi);
if (err) {
--
2.49.0