[PATCH 3/3] hwmon: lenovo-ec-sensors: Use devm_request_region for automatic cleanup

From: Kean

Date: Wed May 13 2026 - 21:15:45 EST


Replace manual request_region()/release_region() with
devm_request_region(). This lets the device-managed framework
handle I/O region lifetime automatically and fixes:

- A double release_region() when probe fails after acquiring the
I/O region: the probe error path releases it, and then
lenovo_ec_init() releases it again on the same error path.

- A release-after-free in lenovo_ec_exit() where release_region()
was called after platform_device_unregister(), which has already
released the I/O region via the platform device removal path.

- Missing release_region() in lenovo_ec_probe() on the DMI match
failure path, which leaked the I/O region.

Remove all manual release_region() calls that are now handled
automatically by the devm framework.

Signed-off-by: Kean <rh_king@xxxxxxx>

Reviewed-by: Mark Pearson <mpearson-lenovo@xxxxxxxxx>
---
drivers/hwmon/lenovo-ec-sensors.c | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/drivers/hwmon/lenovo-ec-sensors.c b/drivers/hwmon/lenovo-ec-sensors.c
index b0f2a04ce679..ea74bddbad5a 100644
--- a/drivers/hwmon/lenovo-ec-sensors.c
+++ b/drivers/hwmon/lenovo-ec-sensors.c
@@ -519,8 +519,8 @@ static int lenovo_ec_probe(struct platform_device *pdev)
if (!ec_data)
return -ENOMEM;

- if (!request_region(IO_REGION_START, IO_REGION_LENGTH, "LNV-WKS")) {
- pr_err(":request fail\n");
+ if (!devm_request_region(dev, IO_REGION_START, IO_REGION_LENGTH, "LNV-WKS")) {
+ dev_err(dev, "Failed to request I/O region.\n");
return -EIO;
}

@@ -541,7 +541,6 @@ static int lenovo_ec_probe(struct platform_device *pdev)
(inb_p(MCHP_EMI0_EC_DATA_BYTE1) != 'C') ||
(inb_p(MCHP_EMI0_EC_DATA_BYTE2) != 'H') ||
(inb_p(MCHP_EMI0_EC_DATA_BYTE3) != 'P')) {
- release_region(IO_REGION_START, IO_REGION_LENGTH);
return -ENODEV;
}

@@ -579,7 +578,8 @@ static int lenovo_ec_probe(struct platform_device *pdev)
lenovo_ec_chip_info.info = lenovo_ec_hwmon_info_p8;
break;
default:
- release_region(IO_REGION_START, IO_REGION_LENGTH);
+ dev_err(dev, "Unsupported platform type %ld\n",
+ (long)dmi_id->driver_data);
return -ENODEV;
}

@@ -608,10 +608,8 @@ static int __init lenovo_ec_init(void)
platform_create_bundle(&lenovo_ec_sensors_platform_driver,
lenovo_ec_probe, NULL, 0, NULL, 0);

- if (IS_ERR(lenovo_ec_sensors_platform_device)) {
- release_region(IO_REGION_START, IO_REGION_LENGTH);
+ if (IS_ERR(lenovo_ec_sensors_platform_device))
return PTR_ERR(lenovo_ec_sensors_platform_device);
- }

return 0;
}
@@ -619,7 +617,6 @@ module_init(lenovo_ec_init);

static void __exit lenovo_ec_exit(void)
{
- release_region(IO_REGION_START, IO_REGION_LENGTH);
platform_device_unregister(lenovo_ec_sensors_platform_device);
platform_driver_unregister(&lenovo_ec_sensors_platform_driver);
}
--
2.47.3