Skip to contentSkip to conversation

power: supply: cpcap-battery: Fix missing nvmem_device_put() causing reference leak

PATCH Applied Ma Ke 2 participants 2 messages Apr 24 – Jun 3

Subsystems (2)
lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH 1/1 power: supply: cpcap-battery: Fix missing nvmem_device_put() causing reference leak

In cpcap_battery_detect_battery_type(), the reference to an nvmem device obtained via nvmem_device_find() is not released with nvmem_device_put() on the success or read-failure paths, causing a permanent reference leak. The driver’s retry logic on subsequent battery property reads can compound this leak, preventing the nvmem device from ever being freed.

Found by code review.

Fixes fd46821e85de ("power: supply: cpcap-battery: Add battery type auto detection for mapphone devices")
---
 drivers/power/supply/cpcap-battery.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/power/supply/cpcap-battery.c +7 -4
index 7b7bdce3162f..59c741993ef8 100644
@@ -439,10 +439,13 @@ static void cpcap_battery_detect_battery_type(struct cpcap_battery_ddata *ddata)
 	if (IS_ERR_OR_NULL(nvmem)) {
 		ddata->check_nvmem = true;
 		dev_info_once(ddata->dev, "Can not find battery nvmem device. Assuming generic lipo battery\n");
-	} else if (nvmem_device_read(nvmem, 2, 1, &battery_id) < 0) {
-		battery_id = 0;
-		ddata->check_nvmem = true;
-		dev_warn(ddata->dev, "Can not read battery nvmem device. Assuming generic lipo battery\n");
+	} else {
+		if (nvmem_device_read(nvmem, 2, 1, &battery_id) < 0) {
+			battery_id = 0;
+			ddata->check_nvmem = true;
+			dev_warn(ddata->dev, "Can not read battery nvmem device. Assuming generic lipo battery\n");
+		}
+		nvmem_device_put(nvmem);
 	}
 
 	switch (battery_id) {
Signature · 1 line
2.43.0
#1 lore raw
Quoted 8 lines · Ma Ke

In cpcap_battery_detect_battery_type(), the reference to an nvmem device obtained via nvmem_device_find() is not released with nvmem_device_put() on the success or read-failure paths, causing a permanent reference leak. The driver’s retry logic on subsequent battery property reads can compound this leak, preventing the nvmem device from ever being freed.

[...]

Applied, thanks!

[1/1] power: supply: cpcap-battery: Fix missing nvmem_device_put() causing reference leak

      commit: a2c14ff63e0e02e3c832385e523e9cc81301171c

Best regards,

Signature · 1 line
Sebastian Reichel <[email protected]>