f2fs: fix to avoid race during access gc_thread pointer
PATCH v2 5/5 f2fs: fix to avoid race during access gc_thread pointer
Thread A Thread B Thread C
- f2fs_remount
- stop_gc_thread
- f2fs_sbi_store
- issue_discard_thread
sbi->gc_thread = NULL; sbi->gc_thread->gc_wake = 1 access sbi->gc_thread->gc_urgent
Previously, we allocate memory for sbi->gc_thread based on background gc thread mount option, the memory can be released if we turn off that mount option, but still there are several places access gc_thread pointer without considering race condition, result in NULL point dereference.
In order to fix this issue, keep gc_thread structure valid in sbi all the time instead of alloc/free it dynamically.
Signed-off-by Chao Yu <[email protected]>
---
v2: avoid double destroy_gc_context.
fs/f2fs/debug.c | 3 +-- fs/f2fs/f2fs.h | 7 +++++++ fs/f2fs/gc.c | 58 +++++++++++++++++++++++++++++++++---------------------- fs/f2fs/segment.c | 4 ++-- fs/f2fs/super.c | 11 +++++++++-- fs/f2fs/sysfs.c | 8 ++++---- 6 files changed, 58 insertions(+), 33 deletions(-)
fs/f2fs/debug.c +1 -2
@@ -223,8 +223,7 @@ static void update_mem_info(struct f2fs_sb_info *sbi) si->cache_mem =0 ;/* build gc */ -if (sbi->gc_thread) - si->cache_mem +=sizeof (struct f2fs_gc_kthread ); + si->cache_mem +=sizeof (struct f2fs_gc_kthread );/* build merge flush thread */ if (SM_I (sbi)->fcc_info)
fs/f2fs/f2fs.h +7 -0
@@ -1412,6 +1412,11 @@ static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)return (struct sit_info *)(SM_I (sbi)->sit_info); } +static inline struct f2fs_gc_kthread *GC_I (struct f2fs_sb_info *sbi) +{ +return (struct f2fs_gc_kthread *)(sbi->gc_thread); +} +static inline struct free_segmap_info *FREE_I (struct f2fs_sb_info *sbi) {return (struct free_segmap_info *)(SM_I (sbi)->free_info);
@@ -2954,6 +2959,8 @@ bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);/* * gc.c */ +int init_gc_context (struct f2fs_sb_info *sbi); +void destroy_gc_context (struct f2fs_sb_info * sbi);int start_gc_thread (struct f2fs_sb_info *sbi);void stop_gc_thread (struct f2fs_sb_info *sbi);block_t start_bidx_of_node (unsigned int node_ofs,struct inode *inode);
fs/f2fs/gc.c +35 -23
101 lines not on this page · raw message
fs/f2fs/segment.c +2 -2
@@ -177,7 +177,7 @@ bool need_SSR(struct f2fs_sb_info *sbi)if (test_opt (sbi,LFS ))return false; -if (sbi->gc_thread && sbi->gc_thread->gc_urgent) +if (GC_I (sbi)->gc_urgent)return true; return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
@@ -1422,7 +1422,7 @@ static int issue_discard_thread(void *data)if (dcc->discard_wake) dcc->discard_wake =0 ; -if (sbi->gc_thread && sbi->gc_thread->gc_urgent) +if (GC_I (sbi)->gc_urgent)init_discard_policy (&dpolicy,DPOLICY_FORCE ,1 );sb_start_intwrite (sbi->sb);
fs/f2fs/super.c +9 -2
@@ -1044,6 +1044,7 @@ static void f2fs_put_super(struct super_block *sb)kfree (sbi->raw_super);destroy_device_list (sbi); +destroy_gc_context (sbi);mempool_destroy (sbi->write_io_dummy);#ifdef CONFIG_QUOTA for (i =0 ; i <MAXQUOTAS ; i++)
@@ -1476,11 +1477,11 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)* option. Also sync the filesystem. */ if ((*flags &SB_RDONLY ) || !test_opt (sbi,BG_GC )) { -if (sbi->gc_thread) { +if (GC_I (sbi)->f2fs_gc_task) {stop_gc_thread (sbi); need_restart_gc = true; } - }else if (!sbi->gc_thread) { + }else if (!GC_I (sbi)->f2fs_gc_task) { err =start_gc_thread (sbi);if (err) goto restore_opts;
@@ -2771,6 +2772,10 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent) goto free_meta_inode; } + err =init_gc_context (sbi); +if (err) + goto free_checkpoint; +/* Initialize device list */ err =f2fs_scan_devices (sbi);if (err) {
@@ -2981,6 +2986,8 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)destroy_segment_manager (sbi); free_devices:destroy_device_list (sbi); +destroy_gc_context (sbi); +free_checkpoint:kfree (sbi->ckpt); free_meta_inode:make_bad_inode (sbi->meta_inode);
fs/f2fs/sysfs.c +4 -4
@@ -46,7 +46,7 @@ struct f2fs_attr {static unsigned char *__struct_ptr (struct f2fs_sb_info *sbi,int struct_type) {if (struct_type ==GC_THREAD ) -return (unsigned char *)sbi->gc_thread; +return (unsigned char *)GC_I (sbi);else if (struct_type ==SM_INFO )return (unsigned char *)SM_I (sbi);else if (struct_type ==DCC_INFO )
@@ -252,9 +252,9 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,if (!strcmp (a->attr.name,"iostat_enable" ) && *ui ==0 )f2fs_reset_iostat (sbi); -if (!strcmp (a->attr.name,"gc_urgent" ) && t ==1 && sbi->gc_thread) { - sbi->gc_thread->gc_wake =1 ; -wake_up_interruptible_all (&sbi->gc_thread->gc_wait_queue_head); +if (!strcmp (a->attr.name,"gc_urgent" ) && t ==1 ) { +GC_I (sbi)->gc_wake =1 ; +wake_up_interruptible_all (&GC_I (sbi)->gc_wait_queue_head);wake_up_discard_thread (sbi, true); }
Signature · 1 line
2.15.0.55.gc2ece9dc4de6