f2fs: start discard thread after mount recovery
PATCH v2 1/1 f2fs: start discard thread after mount recovery
create_discard_cmd_control() is called from f2fs_build_segment_manager(), before f2fs_build_node_manager() and mount recovery have completed. It currently starts the discard thread immediately, so issue_discard_thread() can run while f2fs_fill_super() is still initializing mount-time state.
This is not the failure-unwind case where free_nm stops the discard thread before f2fs_destroy_node_manager() frees nm_info. The window is
earlier: the thread may run while f2fs_build_node_manager() has published sbi->nm_info but init_node_manager() is still initializing it. After commit d6d2b491a82e, issue_discard_thread() may call f2fs_available_free_memory() and read fields such as nm_i->ram_thresh.
The same early-start window also lets the discard thread observe the superblock read-only state while mount recovery is still making temporary SB_RDONLY transitions.
Keep the discard command control available early, but start the discard thread later in f2fs_fill_super(), after node-manager initialization and mount recovery have completed.
--- fs/f2fs/segment.c | 17 ++++------------- fs/f2fs/super.c | 12 ++++++++++++ 2 files changed, 16 insertions(+), 13 deletions(-)
fs/f2fs/segment.c +4 -13
@@ -2302,12 +2302,10 @@ int f2fs_start_discard_thread(struct f2fs_sb_info *sbi)static int create_discard_cmd_control (struct f2fs_sb_info *sbi) {struct discard_cmd_control *dcc; -int err =0 , i; +int i; -if (SM_I (sbi)->dcc_info) { - dcc =SM_I (sbi)->dcc_info; - goto init_thread; - } +if (SM_I (sbi)->dcc_info) +return 0 ; dcc =f2fs_kzalloc (sbi,sizeof (struct discard_cmd_control ),GFP_KERNEL );if (!dcc)
@@ -2344,14 +2342,7 @@ static int create_discard_cmd_control(struct f2fs_sb_info *sbi)init_waitqueue_head (&dcc->discard_wait_queue);SM_I (sbi)->dcc_info = dcc; -init_thread: - err =f2fs_start_discard_thread (sbi); -if (err) { -kfree (dcc); -SM_I (sbi)->dcc_info =NULL ; - } - -return err; +return 0 ; }static void destroy_discard_cmd_control (struct f2fs_sb_info *sbi)
fs/f2fs/super.c +12 -0
@@ -5340,6 +5340,15 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)f2fs_tuning_parameters (sbi); +/* +* After POR and mount-time recovery, we can run the discard thread. It +* reads node-manager memory thresholds and the superblock read-only +* state, so keep it out of the fill_super() initialization window. +*/ + err =f2fs_start_discard_thread (sbi); +if (err) + goto leave_shrinker; +f2fs_notice (sbi,"Mounted with checkpoint version = %llx" ,cur_cp_version (F2FS_CKPT (sbi)));f2fs_update_time (sbi,CP_TIME );
@@ -5349,6 +5358,9 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) sbi->umount_lock_holder =NULL ;return 0 ; +leave_shrinker: +f2fs_leave_shrinker (sbi); +f2fs_stop_gc_thread (sbi); sync_free_meta:/* safe to flush all the data */ sync_filesystem (sbi->sb);
Signature · 1 line
2.43.0
Chao Yu
maintainerQuoted 12 lines · Cen Zhang
create_discard_cmd_control()is called fromf2fs_build_segment_manager(), beforef2fs_build_node_manager()and mount recovery have completed. It currently starts the discard thread immediately, soissue_discard_thread()can run whilef2fs_fill_super()is still initializing mount-time state.This is not the failure-unwind case where
free_nmstops the discard thread beforef2fs_destroy_node_manager()freesnm_info. The window is
earlier: the thread may run whilef2fs_build_node_manager()has publishedsbi->nm_infobutinit_node_manager()is still initializing it. After commitd6d2b491a82e,issue_discard_thread()may callf2fs_available_free_memory()and read fields such asnm_i->ram_thresh.
Quoted 9 lines
The same early-start window also lets the discard thread observe the superblock read-only state while mount recovery is still making temporary
SB_RDONLYtransitions.Keep the discard command control available early, but start the discard thread later in
f2fs_fill_super(), after node-manager initialization and mount recovery have completed.
Quoted 1 line
Fixes d6d2b491a82e1e411a6766fbfb87c697d8701554 ("f2fs: allow to change discard policy based on cached discard cmds")
otherwise, ./scripts/checkpatch.pl will complain as below:
ERROR: Please use git commit description style 'commit <12+ chars of sha1> ("<title line>")' - ie: 'commit d6d2b491a82e ("f2fs: allow to change discard policy based on cached discard cmds")'
Thanks,
Quoted 72 lines
Signed-off-by Cen Zhang <[email protected]>--- fs/f2fs/segment.c | 17 ++++------------- fs/f2fs/super.c | 12 ++++++++++++ 2 files changed, 16 insertions(+), 13 deletions(-)fs/f2fs/segment.c +4
-13@@ -2302,12 +2302,10 @@ int f2fs_start_discard_thread(struct f2fs_sb_info *sbi)static int create_discard_cmd_control (struct f2fs_sb_info *sbi) {struct discard_cmd_control *dcc; -int err =0 , i; +int i; -if (SM_I (sbi)->dcc_info) { - dcc =SM_I (sbi)->dcc_info; - goto init_thread; - } +if (SM_I (sbi)->dcc_info) +return 0 ; dcc =f2fs_kzalloc (sbi,sizeof (struct discard_cmd_control ),GFP_KERNEL );if (!dcc)@@ -2344,14 +2342,7 @@ static int create_discard_cmd_control(struct f2fs_sb_info *sbi)init_waitqueue_head (&dcc->discard_wait_queue);SM_I (sbi)->dcc_info = dcc; -init_thread: - err =f2fs_start_discard_thread (sbi); -if (err) { -kfree (dcc); -SM_I (sbi)->dcc_info =NULL ; - } - -return err; +return 0 ; }static void destroy_discard_cmd_control (struct f2fs_sb_info *sbi)fs/f2fs/super.c +12
-0@@ -5340,6 +5340,15 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)f2fs_tuning_parameters (sbi); +/* +* After POR and mount-time recovery, we can run the discard thread. It +* reads node-manager memory thresholds and the superblock read-only +* state, so keep it out of the fill_super() initialization window. +*/ + err =f2fs_start_discard_thread (sbi); +if (err) + goto leave_shrinker; +f2fs_notice (sbi,"Mounted with checkpoint version = %llx" ,cur_cp_version (F2FS_CKPT (sbi)));f2fs_update_time (sbi,CP_TIME );@@ -5349,6 +5358,9 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) sbi->umount_lock_holder =NULL ;return 0 ; +leave_shrinker: +f2fs_leave_shrinker (sbi); +f2fs_stop_gc_thread (sbi); sync_free_meta:/* safe to flush all the data */ sync_filesystem (sbi->sb);