Skip to content

Commit f913880

Browse files
Jaegeuk Kimpundiramit
authored andcommitted
f2fs: fix to access nullified flush_cmd_control pointer
commit 5eba8c5d1fb3af28b2073ba5228d4998196c1bcc upstream. f2fs_sync_file() remount_ro - f2fs_readonly - destroy_flush_cmd_control - f2fs_issue_flush - no fcc pointer! So, this patch doesn't free fcc in this case, but just stop its kernel thread which sends flush commands. Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 3cc247f commit f913880

3 files changed

Lines changed: 29 additions & 11 deletions

File tree

fs/f2fs/f2fs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2150,7 +2150,7 @@ void f2fs_balance_fs(struct f2fs_sb_info *, bool);
21502150
void f2fs_balance_fs_bg(struct f2fs_sb_info *);
21512151
int f2fs_issue_flush(struct f2fs_sb_info *);
21522152
int create_flush_cmd_control(struct f2fs_sb_info *);
2153-
void destroy_flush_cmd_control(struct f2fs_sb_info *);
2153+
void destroy_flush_cmd_control(struct f2fs_sb_info *, bool);
21542154
void invalidate_blocks(struct f2fs_sb_info *, block_t);
21552155
bool is_checkpointed_data(struct f2fs_sb_info *, block_t);
21562156
void refresh_sit_entry(struct f2fs_sb_info *, block_t, block_t);

fs/f2fs/segment.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,13 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi)
486486
if (!fcc->dispatch_list)
487487
wake_up(&fcc->flush_wait_queue);
488488

489-
wait_for_completion(&cmd.wait);
490-
atomic_dec(&fcc->submit_flush);
489+
if (fcc->f2fs_issue_flush) {
490+
wait_for_completion(&cmd.wait);
491+
atomic_dec(&fcc->submit_flush);
492+
} else {
493+
llist_del_all(&fcc->issue_list);
494+
atomic_set(&fcc->submit_flush, 0);
495+
}
491496

492497
return cmd.ret;
493498
}
@@ -498,13 +503,19 @@ int create_flush_cmd_control(struct f2fs_sb_info *sbi)
498503
struct flush_cmd_control *fcc;
499504
int err = 0;
500505

506+
if (SM_I(sbi)->cmd_control_info) {
507+
fcc = SM_I(sbi)->cmd_control_info;
508+
goto init_thread;
509+
}
510+
501511
fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
502512
if (!fcc)
503513
return -ENOMEM;
504514
atomic_set(&fcc->submit_flush, 0);
505515
init_waitqueue_head(&fcc->flush_wait_queue);
506516
init_llist_head(&fcc->issue_list);
507517
SM_I(sbi)->cmd_control_info = fcc;
518+
init_thread:
508519
fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
509520
"f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
510521
if (IS_ERR(fcc->f2fs_issue_flush)) {
@@ -517,14 +528,20 @@ int create_flush_cmd_control(struct f2fs_sb_info *sbi)
517528
return err;
518529
}
519530

520-
void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
531+
void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
521532
{
522533
struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
523534

524-
if (fcc && fcc->f2fs_issue_flush)
525-
kthread_stop(fcc->f2fs_issue_flush);
526-
kfree(fcc);
527-
SM_I(sbi)->cmd_control_info = NULL;
535+
if (fcc && fcc->f2fs_issue_flush) {
536+
struct task_struct *flush_thread = fcc->f2fs_issue_flush;
537+
538+
fcc->f2fs_issue_flush = NULL;
539+
kthread_stop(flush_thread);
540+
}
541+
if (free) {
542+
kfree(fcc);
543+
SM_I(sbi)->cmd_control_info = NULL;
544+
}
528545
}
529546

530547
static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
@@ -2658,7 +2675,7 @@ void destroy_segment_manager(struct f2fs_sb_info *sbi)
26582675

26592676
if (!sm_info)
26602677
return;
2661-
destroy_flush_cmd_control(sbi);
2678+
destroy_flush_cmd_control(sbi, true);
26622679
destroy_dirty_segmap(sbi);
26632680
destroy_curseg(sbi);
26642681
destroy_free_segmap(sbi);

fs/f2fs/super.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,8 +1103,9 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
11031103
* or if flush_merge is not passed in mount option.
11041104
*/
11051105
if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
1106-
destroy_flush_cmd_control(sbi);
1107-
} else if (!SM_I(sbi)->cmd_control_info) {
1106+
clear_opt(sbi, FLUSH_MERGE);
1107+
destroy_flush_cmd_control(sbi, false);
1108+
} else {
11081109
err = create_flush_cmd_control(sbi);
11091110
if (err)
11101111
goto restore_gc;

0 commit comments

Comments
 (0)