Commit 3d439b1a authored by Daniel Lezcano's avatar Daniel Lezcano

thermal/core: Alloc-copy-free the thermal zone parameters structure

The caller of the function thermal_zone_device_register_with_trips()
can pass a thermal_zone_params structure parameter.

This one is used by the thermal core code until the thermal zone is
destroyed. That forces the caller, so the driver, to keep the pointer
valid until it unregisters the thermal zone if we want to make the
thermal zone device structure private the core code.

As the thermal zone device structure would be private, the driver can
not access to thermal zone device structure to retrieve the tzp field
after it passed it to register the thermal zone.

So instead of forcing the users of the function to deal with the tzp
structure life cycle, make the usage easier by allocating our own
thermal zone params, copying the parameter content and by freeing at
unregister time. The user can then create the parameters on the stack,
pass it to the registering function and forget about it.
Signed-off-by: default avatarDaniel Lezcano <daniel.lezcano@linaro.org>
Link: https://lore.kernel.org/r/20230404075138.2914680-3-daniel.lezcano@linaro.org
parent ac614a9b
...@@ -1256,13 +1256,21 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t ...@@ -1256,13 +1256,21 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
if (!tz) if (!tz)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
if (tzp) {
tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
if (!tz->tzp) {
result = -ENOMEM;
goto free_tz;
}
}
INIT_LIST_HEAD(&tz->thermal_instances); INIT_LIST_HEAD(&tz->thermal_instances);
ida_init(&tz->ida); ida_init(&tz->ida);
mutex_init(&tz->lock); mutex_init(&tz->lock);
id = ida_alloc(&thermal_tz_ida, GFP_KERNEL); id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
if (id < 0) { if (id < 0) {
result = id; result = id;
goto free_tz; goto free_tzp;
} }
tz->id = id; tz->id = id;
...@@ -1272,7 +1280,6 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t ...@@ -1272,7 +1280,6 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
ops->critical = thermal_zone_device_critical; ops->critical = thermal_zone_device_critical;
tz->ops = ops; tz->ops = ops;
tz->tzp = tzp;
tz->device.class = thermal_class; tz->device.class = thermal_class;
tz->devdata = devdata; tz->devdata = devdata;
tz->trips = trips; tz->trips = trips;
...@@ -1354,6 +1361,8 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t ...@@ -1354,6 +1361,8 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
tz = NULL; tz = NULL;
remove_id: remove_id:
ida_free(&thermal_tz_ida, id); ida_free(&thermal_tz_ida, id);
free_tzp:
kfree(tz->tzp);
free_tz: free_tz:
kfree(tz); kfree(tz);
return ERR_PTR(result); return ERR_PTR(result);
...@@ -1434,6 +1443,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz) ...@@ -1434,6 +1443,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
device_del(&tz->device); device_del(&tz->device);
mutex_unlock(&tz->lock); mutex_unlock(&tz->lock);
kfree(tz->tzp);
put_device(&tz->device); put_device(&tz->device);
thermal_notify_tz_delete(tz_id); thermal_notify_tz_delete(tz_id);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment