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.

Signed-off-by: Ma Ke <make24@iscas.ac.cn>
Cc: stable@vger.kernel.org
Fixes: fd46821e85 ("power: supply: cpcap-battery: Add battery type auto detection for mapphone devices")
Link: https://patch.msgid.link/20260424011013.879639-1-make24@iscas.ac.cn
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
This commit is contained in:
Ma Ke
2026-04-24 09:10:13 +08:00
committed by Sebastian Reichel
parent 68d234144b
commit a2c14ff63e

View File

@@ -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) {