[PATCH 2/3] hwmon: scmi: Remove redundant devm_kfree call

a.shimko posted 3 patches 2 months ago
[PATCH 2/3] hwmon: scmi: Remove redundant devm_kfree call
Posted by a.shimko 2 months ago
From: Artem Shimko <artyom.shimko@gmail.com>

Fix potential resource management issue by:
- Removing unnecessary devm_kfree() call in error path
- Relying on devres automatic cleanup
- Preserving all error handling logic

Rationale:
- Memory was allocated with devm_kzalloc()
- devm_ thermal registration manages its own resources
- Double-free could occur during probe failure

Signed-off-by: Artem Shimko <artyom.shimko@gmail.com>
---
 drivers/hwmon/scmi-hwmon.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/drivers/hwmon/scmi-hwmon.c b/drivers/hwmon/scmi-hwmon.c
index a3b5b5c0ec25..d03174922e65 100644
--- a/drivers/hwmon/scmi-hwmon.c
+++ b/drivers/hwmon/scmi-hwmon.c
@@ -211,8 +211,6 @@ static int scmi_thermal_sensor_register(struct device *dev,
 	tzd = devm_thermal_of_zone_register(dev, th_sensor->info->id, th_sensor,
 					    &scmi_hwmon_thermal_ops);
 	if (IS_ERR(tzd)) {
-		devm_kfree(dev, th_sensor);
-
 		if (PTR_ERR(tzd) != -ENODEV)
 			return PTR_ERR(tzd);
 
-- 
2.43.0
Re: [PATCH 2/3] hwmon: scmi: Remove redundant devm_kfree call
Posted by Guenter Roeck 2 months ago
On 8/5/25 05:43, a.shimko wrote:
> From: Artem Shimko <artyom.shimko@gmail.com>
> 
> Fix potential resource management issue by:
> - Removing unnecessary devm_kfree() call in error path
> - Relying on devres automatic cleanup
> - Preserving all error handling logic
> 
> Rationale:
> - Memory was allocated with devm_kzalloc()
> - devm_ thermal registration manages its own resources
> - Double-free could occur during probe failure

The reason for calling devm_kfree() is to avoid holding the memory
unnecessarily until the driver deregisters. There is no double free since
the code calls devm_kfree(), not kfree().

The reasoning is wrong, and the patch does not add value.

Guenter

> 
> Signed-off-by: Artem Shimko <artyom.shimko@gmail.com>
> ---
>   drivers/hwmon/scmi-hwmon.c | 2 --
>   1 file changed, 2 deletions(-)
> 
> diff --git a/drivers/hwmon/scmi-hwmon.c b/drivers/hwmon/scmi-hwmon.c
> index a3b5b5c0ec25..d03174922e65 100644
> --- a/drivers/hwmon/scmi-hwmon.c
> +++ b/drivers/hwmon/scmi-hwmon.c
> @@ -211,8 +211,6 @@ static int scmi_thermal_sensor_register(struct device *dev,
>   	tzd = devm_thermal_of_zone_register(dev, th_sensor->info->id, th_sensor,
>   					    &scmi_hwmon_thermal_ops);
>   	if (IS_ERR(tzd)) {
> -		devm_kfree(dev, th_sensor);
> -
>   		if (PTR_ERR(tzd) != -ENODEV)
>   			return PTR_ERR(tzd);
>