Skip to content

Commit 9b0a627

Browse files
committed
thermal: core: Store zone trips table in struct thermal_zone_device
The current code expects thermal zone creators to pass a pointer to a writable trips table to thermal_zone_device_register_with_trips() and that trips table is then used by the thermal core going forward. Consequently, the callers of thermal_zone_device_register_with_trips() are required to hold on to the trips table passed to it until the given thermal zone is unregistered, at which point the trips table can be freed, but at the same time they are not expected to access that table directly. This is both error prone and confusing. To address it, turn the trips table pointer in struct thermal_zone_device into a flex array (counted by its num_trips field), allocate it during thermal zone device allocation and copy the contents of the trips table supplied by the zone creator (which can be const now) into it, which will allow the callers of thermal_zone_device_register_with_trips() to drop their trip tables right after the zone registration. This requires the imx thermal driver to be adjusted to store the new temperature in its internal trips table in imx_set_trip_temp(), because it will be separate from the core's trips table now and it has to be explicitly kept in sync with the latter. Signed-off-by: Rafael J. Wysocki <[email protected]> Reviewed-by: Stanislaw Gruszka <[email protected]> Reviewed-by: Daniel Lezcano <[email protected]>
1 parent 2c8459a commit 9b0a627

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

drivers/thermal/imx_thermal.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip_id,
354354
return -EINVAL;
355355

356356
imx_set_alarm_temp(data, temp);
357+
trips[IMX_TRIP_PASSIVE].temperature = temp;
357358

358359
pm_runtime_put(data->dev);
359360

drivers/thermal/thermal_core.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,9 +1227,6 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
12271227
if (tz->ops->get_crit_temp)
12281228
return tz->ops->get_crit_temp(tz, temp);
12291229

1230-
if (!tz->trips)
1231-
return -EINVAL;
1232-
12331230
mutex_lock(&tz->lock);
12341231

12351232
for (i = 0; i < tz->num_trips; i++) {
@@ -1272,10 +1269,13 @@ EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
12721269
* IS_ERR*() helpers.
12731270
*/
12741271
struct thermal_zone_device *
1275-
thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask,
1276-
void *devdata, struct thermal_zone_device_ops *ops,
1277-
const struct thermal_zone_params *tzp, int passive_delay,
1278-
int polling_delay)
1272+
thermal_zone_device_register_with_trips(const char *type,
1273+
const struct thermal_trip *trips,
1274+
int num_trips, int mask,
1275+
void *devdata,
1276+
struct thermal_zone_device_ops *ops,
1277+
const struct thermal_zone_params *tzp,
1278+
int passive_delay, int polling_delay)
12791279
{
12801280
struct thermal_zone_device *tz;
12811281
int id;
@@ -1322,7 +1322,7 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
13221322
if (!thermal_class)
13231323
return ERR_PTR(-ENODEV);
13241324

1325-
tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1325+
tz = kzalloc(struct_size(tz, trips, num_trips), GFP_KERNEL);
13261326
if (!tz)
13271327
return ERR_PTR(-ENOMEM);
13281328

@@ -1354,7 +1354,7 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
13541354
tz->ops = ops;
13551355
tz->device.class = thermal_class;
13561356
tz->devdata = devdata;
1357-
tz->trips = trips;
1357+
memcpy(tz->trips, trips, num_trips * sizeof(*trips));
13581358
tz->num_trips = num_trips;
13591359

13601360
thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);

drivers/thermal/thermal_of.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,10 @@ static int thermal_of_unbind(struct thermal_zone_device *tz,
438438
*/
439439
static void thermal_of_zone_unregister(struct thermal_zone_device *tz)
440440
{
441-
struct thermal_trip *trips = tz->trips;
442441
struct thermal_zone_device_ops *ops = tz->ops;
443442

444443
thermal_zone_device_disable(tz);
445444
thermal_zone_device_unregister(tz);
446-
kfree(trips);
447445
kfree(ops);
448446
}
449447

@@ -526,6 +524,8 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
526524
goto out_kfree_trips;
527525
}
528526

527+
kfree(trips);
528+
529529
ret = thermal_zone_device_enable(tz);
530530
if (ret) {
531531
pr_err("Failed to enabled thermal zone '%s', id=%d: %d\n",

drivers/thermal/thermal_trip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz)
122122
int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
123123
struct thermal_trip *trip)
124124
{
125-
if (!tz || !tz->trips || trip_id < 0 || trip_id >= tz->num_trips || !trip)
125+
if (!tz || trip_id < 0 || trip_id >= tz->num_trips || !trip)
126126
return -EINVAL;
127127

128128
*trip = tz->trips[trip_id];

include/linux/thermal.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ struct thermal_cooling_device {
130130
* @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
131131
* @mode: current mode of this thermal zone
132132
* @devdata: private pointer for device private data
133-
* @trips: an array of struct thermal_trip
134133
* @num_trips: number of trip points the thermal zone supports
135134
* @passive_delay_jiffies: number of jiffies to wait between polls when
136135
* performing passive cooling.
@@ -160,6 +159,7 @@ struct thermal_cooling_device {
160159
* @poll_queue: delayed work for polling
161160
* @notify_event: Last notification event
162161
* @suspended: thermal zone suspend indicator
162+
* @trips: array of struct thermal_trip objects
163163
*/
164164
struct thermal_zone_device {
165165
int id;
@@ -172,7 +172,6 @@ struct thermal_zone_device {
172172
struct thermal_attr *trip_hyst_attrs;
173173
enum thermal_device_mode mode;
174174
void *devdata;
175-
struct thermal_trip *trips;
176175
int num_trips;
177176
unsigned long passive_delay_jiffies;
178177
unsigned long polling_delay_jiffies;
@@ -193,10 +192,11 @@ struct thermal_zone_device {
193192
struct list_head node;
194193
struct delayed_work poll_queue;
195194
enum thermal_notify_event notify_event;
195+
bool suspended;
196196
#ifdef CONFIG_THERMAL_DEBUGFS
197197
struct thermal_debugfs *debugfs;
198198
#endif
199-
bool suspended;
199+
struct thermal_trip trips[] __counted_by(num_trips);
200200
};
201201

202202
/**
@@ -315,7 +315,7 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
315315
#ifdef CONFIG_THERMAL
316316
struct thermal_zone_device *thermal_zone_device_register_with_trips(
317317
const char *type,
318-
struct thermal_trip *trips,
318+
const struct thermal_trip *trips,
319319
int num_trips, int mask,
320320
void *devdata,
321321
struct thermal_zone_device_ops *ops,
@@ -375,7 +375,7 @@ void thermal_zone_device_critical(struct thermal_zone_device *tz);
375375
#else
376376
static inline struct thermal_zone_device *thermal_zone_device_register_with_trips(
377377
const char *type,
378-
struct thermal_trip *trips,
378+
const struct thermal_trip *trips,
379379
int num_trips, int mask,
380380
void *devdata,
381381
struct thermal_zone_device_ops *ops,

0 commit comments

Comments
 (0)