add patch create-ext4_feat-kobject-dynamically
[ext4-patch-queue.git] / release-kobject-kset-even-when-init-register-fail
blobb78ca883d9070e450594a970cd516ad3397a2f10
1 ext4: release kobject/kset even when init/register fail
3 From: Riccardo Schirone <sirmy15@gmail.com>
5 Even when kobject_init_and_add/kset_register fail, the kobject has been
6 already initialized and the refcount set to 1. Thus it is necessary to
7 release the kobject/kset, to avoid the memory associated with it hanging
8 around forever.
10 Signed-off-by: Riccardo Schirone <sirmy15@gmail.com>
11 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
12 ---
13  fs/ext4/sysfs.c | 15 +++++++++++----
14  1 file changed, 11 insertions(+), 4 deletions(-)
16 diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
17 index 48c7a7d55ed3..b096e157934f 100644
18 --- a/fs/ext4/sysfs.c
19 +++ b/fs/ext4/sysfs.c
20 @@ -395,8 +395,11 @@ int ext4_register_sysfs(struct super_block *sb)
21         init_completion(&sbi->s_kobj_unregister);
22         err = kobject_init_and_add(&sbi->s_kobj, &ext4_sb_ktype, NULL,
23                                    "%s", sb->s_id);
24 -       if (err)
25 +       if (err) {
26 +               kobject_put(&sbi->s_kobj);
27 +               wait_for_completion(&sbi->s_kobj_unregister);
28                 return err;
29 +       }
31         if (ext4_proc_root)
32                 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
33 @@ -429,15 +432,19 @@ int __init ext4_init_sysfs(void)
34         kobject_set_name(&ext4_kset.kobj, "ext4");
35         ext4_kset.kobj.parent = fs_kobj;
36         ret = kset_register(&ext4_kset);
37 -       if (ret)
38 +       if (ret) {
39 +               kset_unregister(&ext4_kset);
40                 return ret;
41 +       }
43         ret = kobject_init_and_add(&ext4_feat, &ext4_feat_ktype,
44                                    NULL, "features");
45 -       if (ret)
46 +       if (ret) {
47 +               kobject_put(&ext4_feat);
48                 kset_unregister(&ext4_kset);
49 -       else
50 +       } else {
51                 ext4_proc_root = proc_mkdir(proc_dirname, NULL);
52 +       }
53         return ret;
54  }
56 -- 
57 2.14.3