Skip to contentSkip to conversation

cpuidle: Fix memory leaks

PATCH In review Anel Orazgaliyeva 2 participants 4 messages Sep 6, 2021 – Oct 1, 2021 CPU idle time management framework

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpuidle: Fix memory leaks

Commit c343bf1ba5ef ("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.

---
 drivers/cpuidle/sysfs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/cpuidle/sysfs.c +4 -1
index 53ec9585ccd4..469e18547d06 100644
@@ -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);
@@ -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;
 	}
 
@@ -705,7 +707,6 @@ int cpuidle_add_sysfs(struct cpuidle_device *dev)
 	if (!kdev)
 		return -ENOMEM;
 	kdev->dev = dev;
-	dev->kobj_dev = kdev;
 
 	init_completion(&kdev->kobj_unregister);
 
@@ -713,9 +714,11 @@ int cpuidle_add_sysfs(struct cpuidle_device *dev)
 				   "cpuidle");
 	if (error) {
 		kobject_put(&kdev->kobj);
+		kfree(kdev);
 		return error;
 	}
 
+	dev->kobj_dev = kdev;
 	kobject_uevent(&kdev->kobj, KOBJ_ADD);
 
 	return 0;
Signature · 12 lines
2.32.0




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
#1 lore raw
Quoted 4 lines · Anel Orazgaliyeva

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

Wait, won't the cleanup be done by cpuidle_free_state_kobj()?

Quoted 66 lines

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.

---
 drivers/cpuidle/sysfs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/cpuidle/sysfs.c +4 -2
index 53ec9585ccd4..469e18547d06 100644
@@ -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);
@@ -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;
        }
 
@@ -705,7 +707,6 @@ int cpuidle_add_sysfs(struct cpuidle_device *dev)
        if (!kdev)
                return -ENOMEM;
        kdev->dev = dev;
-       dev->kobj_dev = kdev;
 
        init_completion(&kdev->kobj_unregister);
 
@@ -713,9 +714,11 @@ int cpuidle_add_sysfs(struct cpuidle_device *dev)
                                   "cpuidle");
        if (error) {
                kobject_put(&kdev->kobj);
+               kfree(kdev);
                return error;
        }
 
+       dev->kobj_dev = kdev;
        kobject_uevent(&kdev->kobj, KOBJ_ADD);
 
        return 0;
--

2.32.0

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

#2 lore raw
Quoted 5 lines · Rafael J. Wysocki

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

Quoted 7 lines
Quoted 4 lines · Anel Orazgaliyeva

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

Wait, won't the cleanup be done by cpuidle_free_state_kobj()?

For state cleanup, cpuidle_free_state_kobj() is called on the all the previously created kobjs:

error_state:

        for (i = i - 1; i >= 0; i--)
                cpuidle_free_state_kobj(device, i);

so we still need to cleanup the kobj created in the current iteration.

For overall sysfs, the flow is as follows:

cpuidle_register_device

     ret = cpuidle_add_sysfs(dev);
    if (ret)
        goto out_unregister;

out_unregister:

    __cpuidle_unregister_device(dev);

goto out_unlock;

so when there is an error in cpuidle_add_sysfs() dev doesn’t get freed.

Quoted 64 lines
Quoted 64 lines

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.

---
 drivers/cpuidle/sysfs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/cpuidle/sysfs.c +4 -2
index 53ec9585ccd4..469e18547d06 100644
@@ -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);
@@ -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;
        }
 
@@ -705,7 +707,6 @@ int cpuidle_add_sysfs(struct cpuidle_device *dev)
        if (!kdev)
                return -ENOMEM;
        kdev->dev = dev;
-       dev->kobj_dev = kdev;
 
        init_completion(&kdev->kobj_unregister);
 
@@ -713,9 +714,11 @@ int cpuidle_add_sysfs(struct cpuidle_device *dev)
                                   "cpuidle");
        if (error) {
                kobject_put(&kdev->kobj);
+               kfree(kdev);
                return error;
        }
 
+       dev->kobj_dev = kdev;
        kobject_uevent(&kdev->kobj, KOBJ_ADD);
 
        return 0;
--

2.32.0

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

Quoted 68 lines · Rafael J. Wysocki

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

Quoted 4 lines · Anel Orazgaliyeva

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

Wait, won't the cleanup be done by cpuidle_free_state_kobj()?

Quoted 56 lines

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.

---
 drivers/cpuidle/sysfs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/cpuidle/sysfs.c +4 -2
index 53ec9585ccd4..469e18547d06 100644
@@ -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);
@@ -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;
        }
 
@@ -705,7 +707,6 @@ int cpuidle_add_sysfs(struct cpuidle_device *dev)
        if (!kdev)
                return -ENOMEM;
        kdev->dev = dev;
-       dev->kobj_dev = kdev;
 
        init_completion(&kdev->kobj_unregister);
 
@@ -713,9 +714,11 @@ int cpuidle_add_sysfs(struct cpuidle_device *dev)
                                   "cpuidle");
        if (error) {
                kobject_put(&kdev->kobj);
+               kfree(kdev);
                return error;
        }
 
+       dev->kobj_dev = kdev;
        kobject_uevent(&kdev->kobj, KOBJ_ADD);
 
        return 0;
--

2.32.0

Quoted 10 lines
Quoted 10 lines

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

#3 lore raw
Quoted 30 lines · Anel Orazgaliyeva
Quoted 6 lines · Rafael J. Wysocki

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

Quoted 7 lines
Quoted 4 lines · Anel Orazgaliyeva

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

Wait, won't the cleanup be done by cpuidle_free_state_kobj()?

For state cleanup, cpuidle_free_state_kobj() is called on the all the previously created kobjs:

error_state:

        for (i = i - 1; i >= 0; i--)
                cpuidle_free_state_kobj(device, i);

so we still need to cleanup the kobj created in the current iteration.

For overall sysfs, the flow is as follows:

cpuidle_register_device

     ret = cpuidle_add_sysfs(dev);
    if (ret)
        goto out_unregister;
Quoted 5 lines

out_unregister:

    __cpuidle_unregister_device(dev);

goto out_unlock;

so when there is an error in cpuidle_add_sysfs() dev doesn’t get freed.

OK, applied as 5.16 material, thanks!