]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ext4: fix the error handling process in extents_kunit_init).
authorYe Bin <yebin10@huawei.com>
Mon, 30 Mar 2026 13:30:33 +0000 (21:30 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 10 Apr 2026 02:04:32 +0000 (22:04 -0400)
The error processing in extents_kunit_init() is improper, causing
resource leakage.
Reconstruct the error handling process to prevent potential resource
leaks

Fixes: cb1e0c1d1fad ("ext4: kunit tests for extent splitting and conversion")
Signed-off-by: Ye Bin <yebin10@huawei.com>
Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
Reviewed-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
Link: https://patch.msgid.link/20260330133035.287842-4-yebin@huaweicloud.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/extents-test.c

index 3d4663d99eb13c0d578c96797c850dd7d8b56562..4042bc8a95e2f62f0da13236a0c1970e2e00511e 100644 (file)
@@ -225,34 +225,38 @@ static int extents_kunit_init(struct kunit *test)
                (struct kunit_ext_test_param *)(test->param_value);
        int err;
 
-       sb = sget(&ext_fs_type, NULL, ext_set, 0, NULL);
-       if (IS_ERR(sb))
-               return PTR_ERR(sb);
-
-       sb->s_blocksize = 4096;
-       sb->s_blocksize_bits = 12;
-
        sbi = kzalloc_obj(struct ext4_sb_info);
        if (sbi == NULL)
                return -ENOMEM;
 
+       sb = sget(&ext_fs_type, NULL, ext_set, 0, NULL);
+       if (IS_ERR(sb)) {
+               kfree(sbi);
+               return PTR_ERR(sb);
+       }
+
        sbi->s_sb = sb;
        sb->s_fs_info = sbi;
 
+       sb->s_blocksize = 4096;
+       sb->s_blocksize_bits = 12;
+
        if (!param || !param->disable_zeroout)
                sbi->s_extent_max_zeroout_kb = 32;
 
+       err = ext4_es_register_shrinker(sbi);
+       if (err)
+               goto out_deactivate;
+
        /* setup the mock inode */
        k_ctx.k_ei = kzalloc_obj(struct ext4_inode_info);
-       if (k_ctx.k_ei == NULL)
-               return -ENOMEM;
+       if (k_ctx.k_ei == NULL) {
+               err = -ENOMEM;
+               goto out;
+       }
        ei = k_ctx.k_ei;
        inode = &ei->vfs_inode;
 
-       err = ext4_es_register_shrinker(sbi);
-       if (err)
-               return err;
-
        ext4_es_init_tree(&ei->i_es_tree);
        rwlock_init(&ei->i_es_lock);
        INIT_LIST_HEAD(&ei->i_es_list);
@@ -267,8 +271,10 @@ static int extents_kunit_init(struct kunit *test)
        inode->i_sb = sb;
 
        k_ctx.k_data = kzalloc(EXT_DATA_LEN * 4096, GFP_KERNEL);
-       if (k_ctx.k_data == NULL)
-               return -ENOMEM;
+       if (k_ctx.k_data == NULL) {
+               err = -ENOMEM;
+               goto out;
+       }
 
        /*
         * set the data area to a junk value
@@ -313,6 +319,20 @@ static int extents_kunit_init(struct kunit *test)
        up_write(&sb->s_umount);
 
        return 0;
+
+out:
+       kfree(k_ctx.k_ei);
+       k_ctx.k_ei = NULL;
+
+       kfree(k_ctx.k_data);
+       k_ctx.k_data = NULL;
+
+       ext4_es_unregister_shrinker(sbi);
+out_deactivate:
+       deactivate_locked_super(sb);
+       kfree(sbi);
+
+       return err;
 }
 
 /*