Re: [PATCH v5 01/33] thermal/of: Rework the thermal device tree initialization

Ido Schimmel
2022-10-18T10:29:16Z

On Fri, Aug 05, 2022 at 12:43:17AM +0200, Daniel Lezcano wrote:
> +struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
> + const struct thermal_zone_device_ops *ops)
> +{
> + struct thermal_zone_device *tz;
> + struct thermal_trip *trips;
> + struct thermal_zone_params *tzp;
> + struct thermal_zone_device_ops *of_ops;
> + struct device_node *np;
> + int delay, pdelay;
> + int ntrips, mask;
> + int ret;
> +
> + of_ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
> + if (!of_ops)
> + return ERR_PTR(-ENOMEM);
> +
> + np = of_thermal_zone_find(sensor, id);
> + if (IS_ERR(np)) {
> + pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id);
> + return ERR_CAST(np);
> + }
> +
> + trips = thermal_of_trips_init(np, &ntrips);
> + if (IS_ERR(trips)) {
> + pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id);
> + return ERR_CAST(trips);
> + }
> +
> + ret = thermal_of_monitor_init(np, &delay, &pdelay);
> + if (ret) {
> + pr_err("Failed to initialize monitoring delays from %pOFn\n", np);
> + goto out_kfree_trips;
> + }
> +
> + tzp = thermal_of_parameters_init(np);
> + if (IS_ERR(tzp)) {
> + ret = PTR_ERR(tzp);
> + pr_err("Failed to initialize parameter from %pOFn: %d\n", np, ret);
> + goto out_kfree_trips;
> + }
> +
> + of_ops->get_trip_type = of_ops->get_trip_type ? : of_thermal_get_trip_type;
> + of_ops->get_trip_temp = of_ops->get_trip_temp ? : of_thermal_get_trip_temp;
> + of_ops->get_trip_hyst = of_ops->get_trip_hyst ? : of_thermal_get_trip_hyst;
> + of_ops->set_trip_hyst = of_ops->set_trip_hyst ? : of_thermal_set_trip_hyst;
> + of_ops->get_crit_temp = of_ops->get_crit_temp ? : of_thermal_get_crit_temp;
> + of_ops->bind = thermal_of_bind;
> + of_ops->unbind = thermal_of_unbind;
> +
> + mask = GENMASK_ULL((ntrips) - 1, 0);
> +
> + tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips,
> + mask, data, of_ops, tzp,
> + pdelay, delay);
> + if (IS_ERR(tz)) {
> + ret = PTR_ERR(tz);
> + pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret);
> + goto out_kfree_tzp;
> + }
> +
> + ret = thermal_zone_device_enable(tz);
> + if (ret) {
> + pr_err("Failed to enabled thermal zone '%s', id=%d: %d\n",
> + tz->type, tz->id, ret);
> + thermal_of_zone_unregister(tz);
> + return ERR_PTR(ret);
> + }
> +
> + return tz;
> +
> +out_kfree_tzp:
> + kfree(tzp);
> +out_kfree_trips:
> + kfree(trips);
> +
> + return ERR_PTR(ret);
> +}
> +EXPORT_SYMBOL_GPL(thermal_of_zone_register);

Daniel, I started seeing these memory leaks [1] since commit
613ed3f67609 ("hwmon: pm_bus: core: Switch to new of thermal API").
Seems to be fixed by the following patch [2].

Do you already have a patch for this issue or should I submit it?

Thanks

[1]
unreferenced object 0xffff8ee846198c80 (size 128):
comm "swapper/0", pid 1, jiffies 4294699704 (age 70.076s)
hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
d0 3f 6e 8c ff ff ff ff 00 00 00 00 00 00 00 00 .?n.............
backtrace:
[<00000000d136f562>] __kmalloc_node_track_caller+0x42/0x120
[<0000000063f31678>] kmemdup+0x1d/0x40
[<00000000e6d24096>] thermal_of_zone_register+0x49/0x520
[<000000005e78c755>] devm_thermal_of_zone_register+0x54/0x90
[<00000000ee6b209e>] pmbus_add_sensor+0x1b4/0x1d0
[<00000000896105e3>] pmbus_add_sensor_attrs_one+0x123/0x440
[<0000000049e990a6>] pmbus_add_sensor_attrs+0xfe/0x1d0
[<00000000466b5440>] pmbus_do_probe+0x66b/0x14e0
[<0000000084d42285>] i2c_device_probe+0x13b/0x2f0
[<0000000029e2ae74>] really_probe+0xce/0x2c0
[<00000000692df15c>] driver_probe_device+0x19/0xd0
[<00000000547d9cce>] __device_attach_driver+0x6f/0x100
[<0000000020abd24b>] bus_for_each_drv+0x76/0xc0
[<00000000665d9563>] __device_attach+0xfc/0x180
[<000000008ddd4d6a>] bus_probe_device+0x82/0xa0
[<000000009e61132b>] device_add+0x3fe/0x920
unreferenced object 0xffff8ee846199200 (size 128):
comm "swapper/0", pid 1, jiffies 4294699710 (age 70.070s)
hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
d0 3f 6e 8c ff ff ff ff 00 00 00 00 00 00 00 00 .?n.............
backtrace:
[<00000000d136f562>] __kmalloc_node_track_caller+0x42/0x120
[<0000000063f31678>] kmemdup+0x1d/0x40
[<00000000e6d24096>] thermal_of_zone_register+0x49/0x520
[<000000005e78c755>] devm_thermal_of_zone_register+0x54/0x90
[<00000000ee6b209e>] pmbus_add_sensor+0x1b4/0x1d0
[<00000000896105e3>] pmbus_add_sensor_attrs_one+0x123/0x440
[<0000000049e990a6>] pmbus_add_sensor_attrs+0xfe/0x1d0
[<00000000466b5440>] pmbus_do_probe+0x66b/0x14e0
[<0000000084d42285>] i2c_device_probe+0x13b/0x2f0
[<0000000029e2ae74>] really_probe+0xce/0x2c0
[<00000000692df15c>] driver_probe_device+0x19/0xd0
[<00000000547d9cce>] __device_attach_driver+0x6f/0x100
[<0000000020abd24b>] bus_for_each_drv+0x76/0xc0
[<00000000665d9563>] __device_attach+0xfc/0x180
[<000000008ddd4d6a>] bus_probe_device+0x82/0xa0
[<000000009e61132b>] device_add+0x3fe/0x920
unreferenced object 0xffff8ee846199780 (size 128):
comm "swapper/0", pid 1, jiffies 4294699716 (age 70.064s)
hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
d0 3f 6e 8c ff ff ff ff 00 00 00 00 00 00 00 00 .?n.............
backtrace:
[<00000000d136f562>] __kmalloc_node_track_caller+0x42/0x120
[<0000000063f31678>] kmemdup+0x1d/0x40
[<00000000e6d24096>] thermal_of_zone_register+0x49/0x520
[<000000005e78c755>] devm_thermal_of_zone_register+0x54/0x90
[<00000000ee6b209e>] pmbus_add_sensor+0x1b4/0x1d0
[<00000000896105e3>] pmbus_add_sensor_attrs_one+0x123/0x440
[<0000000049e990a6>] pmbus_add_sensor_attrs+0xfe/0x1d0
[<00000000466b5440>] pmbus_do_probe+0x66b/0x14e0
[<0000000084d42285>] i2c_device_probe+0x13b/0x2f0
[<0000000029e2ae74>] really_probe+0xce/0x2c0
[<00000000692df15c>] driver_probe_device+0x19/0xd0
[<00000000547d9cce>] __device_attach_driver+0x6f/0x100
[<0000000020abd24b>] bus_for_each_drv+0x76/0xc0
[<00000000665d9563>] __device_attach+0xfc/0x180
[<000000008ddd4d6a>] bus_probe_device+0x82/0xa0
[<000000009e61132b>] device_add+0x3fe/0x920

[2]
diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
index d4b6335ace15..43d55c29a29a 100644
--- a/drivers/thermal/thermal_of.c
+++ b/drivers/thermal/thermal_of.c
@@ -604,13 +604,15 @@ struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor,
if (IS_ERR(np)) {
if (PTR_ERR(np) != -ENODEV)
pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id);
- return ERR_CAST(np);
+ ret = PTR_ERR(np);
+ goto out_kfree_of_ops;
}

trips = thermal_of_trips_init(np, &ntrips);
if (IS_ERR(trips)) {
pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id);
- return ERR_CAST(trips);
+ ret = PTR_ERR(np);
+ goto out_kfree_of_ops;
}

ret = thermal_of_monitor_init(np, &delay, &pdelay);
@@ -659,6 +661,8 @@ struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor,
kfree(tzp);
out_kfree_trips:
kfree(trips);
+out_kfree_of_ops:
+ kfree(of_ops);

return ERR_PTR(ret);
}