Skip to content

Commit 17f73c9

Browse files
Ye Bintytso
authored andcommitted
ext4: fix the error handling process in extents_kunit_init).
The error processing in extents_kunit_init() is improper, causing resource leakage. Reconstruct the error handling process to prevent potential resource leaks Fixes: cb1e0c1 ("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>
1 parent f9c1f76 commit 17f73c9

1 file changed

Lines changed: 35 additions & 15 deletions

File tree

fs/ext4/extents-test.c

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -225,34 +225,38 @@ static int extents_kunit_init(struct kunit *test)
225225
(struct kunit_ext_test_param *)(test->param_value);
226226
int err;
227227

228-
sb = sget(&ext_fs_type, NULL, ext_set, 0, NULL);
229-
if (IS_ERR(sb))
230-
return PTR_ERR(sb);
231-
232-
sb->s_blocksize = 4096;
233-
sb->s_blocksize_bits = 12;
234-
235228
sbi = kzalloc_obj(struct ext4_sb_info);
236229
if (sbi == NULL)
237230
return -ENOMEM;
238231

232+
sb = sget(&ext_fs_type, NULL, ext_set, 0, NULL);
233+
if (IS_ERR(sb)) {
234+
kfree(sbi);
235+
return PTR_ERR(sb);
236+
}
237+
239238
sbi->s_sb = sb;
240239
sb->s_fs_info = sbi;
241240

241+
sb->s_blocksize = 4096;
242+
sb->s_blocksize_bits = 12;
243+
242244
if (!param || !param->disable_zeroout)
243245
sbi->s_extent_max_zeroout_kb = 32;
244246

247+
err = ext4_es_register_shrinker(sbi);
248+
if (err)
249+
goto out_deactivate;
250+
245251
/* setup the mock inode */
246252
k_ctx.k_ei = kzalloc_obj(struct ext4_inode_info);
247-
if (k_ctx.k_ei == NULL)
248-
return -ENOMEM;
253+
if (k_ctx.k_ei == NULL) {
254+
err = -ENOMEM;
255+
goto out;
256+
}
249257
ei = k_ctx.k_ei;
250258
inode = &ei->vfs_inode;
251259

252-
err = ext4_es_register_shrinker(sbi);
253-
if (err)
254-
return err;
255-
256260
ext4_es_init_tree(&ei->i_es_tree);
257261
rwlock_init(&ei->i_es_lock);
258262
INIT_LIST_HEAD(&ei->i_es_list);
@@ -267,8 +271,10 @@ static int extents_kunit_init(struct kunit *test)
267271
inode->i_sb = sb;
268272

269273
k_ctx.k_data = kzalloc(EXT_DATA_LEN * 4096, GFP_KERNEL);
270-
if (k_ctx.k_data == NULL)
271-
return -ENOMEM;
274+
if (k_ctx.k_data == NULL) {
275+
err = -ENOMEM;
276+
goto out;
277+
}
272278

273279
/*
274280
* set the data area to a junk value
@@ -313,6 +319,20 @@ static int extents_kunit_init(struct kunit *test)
313319
up_write(&sb->s_umount);
314320

315321
return 0;
322+
323+
out:
324+
kfree(k_ctx.k_ei);
325+
k_ctx.k_ei = NULL;
326+
327+
kfree(k_ctx.k_data);
328+
k_ctx.k_data = NULL;
329+
330+
ext4_es_unregister_shrinker(sbi);
331+
out_deactivate:
332+
deactivate_locked_super(sb);
333+
kfree(sbi);
334+
335+
return err;
316336
}
317337

318338
/*

0 commit comments

Comments
 (0)