]> git.baikalelectronics.ru Git - kernel.git/commitdiff
f2fs: initialize locks earlier in f2fs_fill_super()
authorTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Tue, 8 Nov 2022 22:04:42 +0000 (07:04 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 9 Feb 2023 10:28:27 +0000 (11:28 +0100)
commit 92b4cf5b48955a4bdd15fe4e2067db8ebd87f04c upstream.

syzbot is reporting lockdep warning at f2fs_handle_error() [1], for
spin_lock(&sbi->error_lock) is called before spin_lock_init() is called.
For safe locking in error handling, move initialization of locks (and
obvious structures) in f2fs_fill_super() to immediately after memory
allocation.

Link: https://syzkaller.appspot.com/bug?extid=40642be9b7e0bb28e0df
Reported-by: syzbot <syzbot+40642be9b7e0bb28e0df@syzkaller.appspotmail.com>
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Tested-by: syzbot <syzbot+40642be9b7e0bb28e0df@syzkaller.appspotmail.com>
Reviewed-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/f2fs/super.c

index 67d51f527606192567f25ca9dee51200aec5a234..eaabb85cb4ddb9d6c20d94162137c65026dd6960 100644 (file)
@@ -4095,6 +4095,24 @@ try_onemore:
 
        sbi->sb = sb;
 
+       /* initialize locks within allocated memory */
+       init_f2fs_rwsem(&sbi->gc_lock);
+       mutex_init(&sbi->writepages);
+       init_f2fs_rwsem(&sbi->cp_global_sem);
+       init_f2fs_rwsem(&sbi->node_write);
+       init_f2fs_rwsem(&sbi->node_change);
+       spin_lock_init(&sbi->stat_lock);
+       init_f2fs_rwsem(&sbi->cp_rwsem);
+       init_f2fs_rwsem(&sbi->quota_sem);
+       init_waitqueue_head(&sbi->cp_wait);
+       spin_lock_init(&sbi->error_lock);
+
+       for (i = 0; i < NR_INODE_TYPE; i++) {
+               INIT_LIST_HEAD(&sbi->inode_list[i]);
+               spin_lock_init(&sbi->inode_lock[i]);
+       }
+       mutex_init(&sbi->flush_lock);
+
        /* Load the checksum driver */
        sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
        if (IS_ERR(sbi->s_chksum_driver)) {
@@ -4118,6 +4136,8 @@ try_onemore:
        sb->s_fs_info = sbi;
        sbi->raw_super = raw_super;
 
+       memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS);
+
        /* precompute checksum seed for metadata */
        if (f2fs_sb_has_inode_chksum(sbi))
                sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
@@ -4174,26 +4194,14 @@ try_onemore:
 
        /* init f2fs-specific super block info */
        sbi->valid_super_block = valid_super_block;
-       init_f2fs_rwsem(&sbi->gc_lock);
-       mutex_init(&sbi->writepages);
-       init_f2fs_rwsem(&sbi->cp_global_sem);
-       init_f2fs_rwsem(&sbi->node_write);
-       init_f2fs_rwsem(&sbi->node_change);
 
        /* disallow all the data/node/meta page writes */
        set_sbi_flag(sbi, SBI_POR_DOING);
-       spin_lock_init(&sbi->stat_lock);
 
        err = f2fs_init_write_merge_io(sbi);
        if (err)
                goto free_bio_info;
 
-       spin_lock_init(&sbi->error_lock);
-       memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS);
-
-       init_f2fs_rwsem(&sbi->cp_rwsem);
-       init_f2fs_rwsem(&sbi->quota_sem);
-       init_waitqueue_head(&sbi->cp_wait);
        init_sb_info(sbi);
 
        err = f2fs_init_iostat(sbi);
@@ -4271,12 +4279,6 @@ try_onemore:
        limit_reserve_root(sbi);
        adjust_unusable_cap_perc(sbi);
 
-       for (i = 0; i < NR_INODE_TYPE; i++) {
-               INIT_LIST_HEAD(&sbi->inode_list[i]);
-               spin_lock_init(&sbi->inode_lock[i]);
-       }
-       mutex_init(&sbi->flush_lock);
-
        f2fs_init_extent_cache_info(sbi);
 
        f2fs_init_ino_entry_info(sbi);