f2fs: fix memleak of kobject
authorChao Yu <yuchao0@huawei.com>
Mon, 30 Dec 2019 09:41:41 +0000 (17:41 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 24 Feb 2020 07:36:53 +0000 (08:36 +0100)
[ Upstream commit fe396ad8e7526f059f7b8c7290d33a1b84adacab ]

If kobject_init_and_add() failed, caller needs to invoke kobject_put()
to release kobject explicitly.

Signed-off-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/f2fs/sysfs.c

index 8544c0ab7b32bdd8195de33297edd61033dc49a8..170934430d7d74220efa4789c353da7f09f8c45f 100644 (file)
@@ -729,10 +729,12 @@ int __init f2fs_init_sysfs(void)
 
        ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
                                   NULL, "features");
-       if (ret)
+       if (ret) {
+               kobject_put(&f2fs_feat);
                kset_unregister(&f2fs_kset);
-       else
+       } else {
                f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
+       }
        return ret;
 }
 
@@ -753,8 +755,11 @@ int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
        init_completion(&sbi->s_kobj_unregister);
        err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
                                "%s", sb->s_id);
-       if (err)
+       if (err) {
+               kobject_put(&sbi->s_kobj);
+               wait_for_completion(&sbi->s_kobj_unregister);
                return err;
+       }
 
        if (f2fs_proc_root)
                sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);