Skip to contentSkip to conversation

power: supply: ab8500: Fix memory leak in ab8500_fg_sysfs_init

PATCH In review Miaoqian Lin 3 participants 3 messages Jan 24, 2022 – Feb 11, 2022

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

PATCH 1/1 power: supply: ab8500: Fix memory leak in ab8500_fg_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 memory leak by calling kobject_put().

Fixes 8c0984e5a753 ("power: move power supply drivers to power/supply")
---
 drivers/power/supply/ab8500_fg.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/power/supply/ab8500_fg.c +3 -1
index 05fe9724ba50..57799a8079d4 100644
@@ -2545,8 +2545,10 @@ static int ab8500_fg_sysfs_init(struct ab8500_fg *di)
 	ret = kobject_init_and_add(&di->fg_kobject,
 		&ab8500_fg_ktype,
 		NULL, "battery");
-	if (ret < 0)
+	if (ret < 0) {
+		kobject_put(&di->fg_kobject);
 		dev_err(di->dev, "failed to create sysfs entry\n");
+	}
 
 	return ret;
 }
Signature · 1 line
2.17.1
#1 lore raw

[+cc Linus Walleij]

Hi,

Quoted 11 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 memory leak by calling kobject_put().

Fixes 8c0984e5a753 ("power: move power supply drivers to power/supply")

---

Thanks, queued power-supply's for-next branch.

Linus, if I'm not mistaken the custom sysfs ABI (i.e. everything related to ab8500_fg_sysfs_init() can just be removed since the same thing is already exposed standardized via POWER_SUPPLY_PROP_CHARGE_FULL and POWER_SUPPLY_PROP_CHARGE_NOW?

-- Sebastian

Quoted 22 lines
 drivers/power/supply/ab8500_fg.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/power/supply/ab8500_fg.c +3 -1
index 05fe9724ba50..57799a8079d4 100644
@@ -2545,8 +2545,10 @@ static int ab8500_fg_sysfs_init(struct ab8500_fg *di)
 	ret = kobject_init_and_add(&di->fg_kobject,
 		&ab8500_fg_ktype,
 		NULL, "battery");
-	if (ret < 0)
+	if (ret < 0) {
+		kobject_put(&di->fg_kobject);
 		dev_err(di->dev, "failed to create sysfs entry\n");
+	}
 
 	return ret;
 }
Signature · 1 line
2.17.1
#2 lore raw
Quoted 10 lines · Sebastian Reichel
Quoted 3 lines
Fixes 8c0984e5a753 ("power: move power supply drivers to power/supply")

---

Thanks, queued power-supply's for-next branch.

Linus, if I'm not mistaken the custom sysfs ABI (i.e. everything related to ab8500_fg_sysfs_init() can just be removed since the same thing is already exposed standardized via POWER_SUPPLY_PROP_CHARGE_FULL and POWER_SUPPLY_PROP_CHARGE_NOW?

Yes, I delete other weird custom sysfs, so I'll cook a patch to get rid of this too.

Yours,
Linus Walleij