Skip to content

Commit

Permalink
cpuidle: Fix kobject memory leaks in error paths
Browse files Browse the repository at this point in the history
[ Upstream commit e5f5a66 ]

Commit c343bf1 ("cpuidle: Fix three reference count leaks")
fixes the cleanup of kobjects; however, it removes kfree() calls
altogether, leading to memory leaks.

Fix those and also defer the initialization of dev->kobj_dev until
after the error check, so that we do not end up with a dangling
pointer.

Fixes: c343bf1 ("cpuidle: Fix three reference count leaks")
Signed-off-by: Anel Orazgaliyeva <anelkz@amazon.de>
Suggested-by: Aman Priyadarshi <apeureka@amazon.de>
[ rjw: Subject edits ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
Anel Orazgaliyeva authored and gregkh committed Nov 18, 2021
1 parent c95380b commit b9a0ff0
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion drivers/cpuidle/sysfs.c
Expand Up @@ -488,6 +488,7 @@ static int cpuidle_add_state_sysfs(struct cpuidle_device *device)
&kdev->kobj, "state%d", i);
if (ret) {
kobject_put(&kobj->kobj);
kfree(kobj);
goto error_state;
}
cpuidle_add_s2idle_attr_group(kobj);
Expand Down Expand Up @@ -619,6 +620,7 @@ static int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
&kdev->kobj, "driver");
if (ret) {
kobject_put(&kdrv->kobj);
kfree(kdrv);
return ret;
}

Expand Down Expand Up @@ -705,17 +707,18 @@ int cpuidle_add_sysfs(struct cpuidle_device *dev)
if (!kdev)
return -ENOMEM;
kdev->dev = dev;
dev->kobj_dev = kdev;

init_completion(&kdev->kobj_unregister);

error = kobject_init_and_add(&kdev->kobj, &ktype_cpuidle, &cpu_dev->kobj,
"cpuidle");
if (error) {
kobject_put(&kdev->kobj);
kfree(kdev);
return error;
}

dev->kobj_dev = kdev;
kobject_uevent(&kdev->kobj, KOBJ_ADD);

return 0;
Expand Down

0 comments on commit b9a0ff0

Please sign in to comment.