Skip to contentSkip to conversation

power: supply: ab8500: Fix memory leak in ab8500_chargalg_sysfs_init

PATCH In review Miaoqian Lin 2 participants 2 messages Jan 28, 2022 – Jan 29, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 power: supply: ab8500: Fix memory leak in ab8500_chargalg_sysfs_init

kobject_init_and_add() takes reference even when it fails. According to the doc of kobject_init_and_add()

If this function returns an error, kobject_put() must be called to properly clean up the memory associated with the object.

Fix this issue by adding kobject_put().

Fixes c5b64a990e7f ("power: supply: ab8500: Rename charging algorithm symbols")
---
 drivers/power/supply/ab8500_chargalg.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/power/supply/ab8500_chargalg.c +3 -1
index c4a2fe07126c..658139797509 100644
@@ -1907,8 +1907,10 @@ static int ab8500_chargalg_sysfs_init(struct ab8500_chargalg *di)
 	ret = kobject_init_and_add(&di->chargalg_kobject,
 		&ab8500_chargalg_ktype,
 		NULL, "ab8500_chargalg");
-	if (ret < 0)
+	if (ret < 0) {
 		dev_err(di->dev, "failed to create sysfs entry\n");
+		kobject_put(&di->chargalg_kobject);
+	}
 
 	return ret;
 }
Signature · 1 line
2.17.1
#1 lore raw
Quoted 9 lines · Miaoqian Lin

kobject_init_and_add() takes reference even when it fails. According to the doc of kobject_init_and_add()

If this function returns an error, kobject_put() must be called to properly clean up the memory associated with the object.

Fix this issue by adding kobject_put().

Fixes c5b64a990e7f ("power: supply: ab8500: Rename charging algorithm symbols")

Really? I think this code is older.

drivers/power/supply/ab8500_chargalg.c static int ab8500_chargalg_sysfs_init(struct ab8500_chargalg *di) · line 1913

Quoted 9 lines
drivers/power/supply/ab8500_chargalg.c +3 -1
@@ -1907,8 +1907,10 @@ static int ab8500_chargalg_sysfs_init(struct ab8500_chargalg *di)
        ret = kobject_init_and_add(&di->chargalg_kobject,
                &ab8500_chargalg_ktype,
                NULL, "ab8500_chargalg");
-       if (ret < 0)
+       if (ret < 0) {
                dev_err(di->dev, "failed to create sysfs entry\n");
+               kobject_put(&di->chargalg_kobject);
+       }

I wouldn't bother with this.

This code is getting deleted in this patch:
https://lore.kernel.org/linux-pm/[email protected]/T/#u

Yours,
Linus Walleij