Skip to content

Commit 698a1eb

Browse files
committed
thermal: core: Store zone ops in struct thermal_zone_device
The current code requires thermal zone creators to pass pointers to writable ops structures to thermal_zone_device_register_with_trips() which needs to modify the target struct thermal_zone_device_ops object if the "critical" operation in it is NULL. Moreover, the callers of thermal_zone_device_register_with_trips() are required to hold on to the struct thermal_zone_device_ops object passed to it until the given thermal zone is unregistered. Both of these requirements are quite inconvenient, so modify struct thermal_zone_device to contain struct thermal_zone_device_ops as field and make thermal_zone_device_register_with_trips() copy the contents of the struct thermal_zone_device_ops passed to it via a pointer (which can be const now) to that field. Also adjust the code using thermal zone ops accordingly and modify thermal_of_zone_register() to use a local ops variable during thermal zone registration so ops do not need to be freed in thermal_of_zone_unregister() any more. Signed-off-by: Rafael J. Wysocki <[email protected]> Reviewed-by: Stanislaw Gruszka <[email protected]> Reviewed-by: Daniel Lezcano <[email protected]>
1 parent fcbf878 commit 698a1eb

File tree

7 files changed

+48
-59
lines changed

7 files changed

+48
-59
lines changed

drivers/thermal/thermal_core.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,9 @@ static void handle_critical_trips(struct thermal_zone_device *tz,
356356
trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
357357

358358
if (trip->type == THERMAL_TRIP_CRITICAL)
359-
tz->ops->critical(tz);
360-
else if (tz->ops->hot)
361-
tz->ops->hot(tz);
359+
tz->ops.critical(tz);
360+
else if (tz->ops.hot)
361+
tz->ops.hot(tz);
362362
}
363363

364364
static void handle_thermal_trip(struct thermal_zone_device *tz,
@@ -493,8 +493,8 @@ static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
493493
return ret;
494494
}
495495

496-
if (tz->ops->change_mode)
497-
ret = tz->ops->change_mode(tz, mode);
496+
if (tz->ops.change_mode)
497+
ret = tz->ops.change_mode(tz, mode);
498498

499499
if (!ret)
500500
tz->mode = mode;
@@ -867,8 +867,8 @@ static void bind_cdev(struct thermal_cooling_device *cdev)
867867
struct thermal_zone_device *pos = NULL;
868868

869869
list_for_each_entry(pos, &thermal_tz_list, node) {
870-
if (pos->ops->bind) {
871-
ret = pos->ops->bind(pos, cdev);
870+
if (pos->ops.bind) {
871+
ret = pos->ops.bind(pos, cdev);
872872
if (ret)
873873
print_bind_err_msg(pos, cdev, ret);
874874
}
@@ -1184,8 +1184,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
11841184

11851185
/* Unbind all thermal zones associated with 'this' cdev */
11861186
list_for_each_entry(tz, &thermal_tz_list, node) {
1187-
if (tz->ops->unbind)
1188-
tz->ops->unbind(tz, cdev);
1187+
if (tz->ops.unbind)
1188+
tz->ops.unbind(tz, cdev);
11891189
}
11901190

11911191
mutex_unlock(&thermal_list_lock);
@@ -1199,13 +1199,13 @@ static void bind_tz(struct thermal_zone_device *tz)
11991199
int ret;
12001200
struct thermal_cooling_device *pos = NULL;
12011201

1202-
if (!tz->ops->bind)
1202+
if (!tz->ops.bind)
12031203
return;
12041204

12051205
mutex_lock(&thermal_list_lock);
12061206

12071207
list_for_each_entry(pos, &thermal_cdev_list, node) {
1208-
ret = tz->ops->bind(tz, pos);
1208+
ret = tz->ops.bind(tz, pos);
12091209
if (ret)
12101210
print_bind_err_msg(tz, pos, ret);
12111211
}
@@ -1224,8 +1224,8 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
12241224
{
12251225
int i, ret = -EINVAL;
12261226

1227-
if (tz->ops->get_crit_temp)
1228-
return tz->ops->get_crit_temp(tz, temp);
1227+
if (tz->ops.get_crit_temp)
1228+
return tz->ops.get_crit_temp(tz, temp);
12291229

12301230
mutex_lock(&tz->lock);
12311231

@@ -1273,7 +1273,7 @@ thermal_zone_device_register_with_trips(const char *type,
12731273
const struct thermal_trip *trips,
12741274
int num_trips, int mask,
12751275
void *devdata,
1276-
struct thermal_zone_device_ops *ops,
1276+
const struct thermal_zone_device_ops *ops,
12771277
const struct thermal_zone_params *tzp,
12781278
int passive_delay, int polling_delay)
12791279
{
@@ -1348,10 +1348,10 @@ thermal_zone_device_register_with_trips(const char *type,
13481348
tz->id = id;
13491349
strscpy(tz->type, type, sizeof(tz->type));
13501350

1351-
if (!ops->critical)
1352-
ops->critical = thermal_zone_device_critical;
1351+
tz->ops = *ops;
1352+
if (!tz->ops.critical)
1353+
tz->ops.critical = thermal_zone_device_critical;
13531354

1354-
tz->ops = ops;
13551355
tz->device.class = thermal_class;
13561356
tz->devdata = devdata;
13571357
memcpy(tz->trips, trips, num_trips * sizeof(*trips));
@@ -1437,7 +1437,7 @@ EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
14371437
struct thermal_zone_device *thermal_tripless_zone_device_register(
14381438
const char *type,
14391439
void *devdata,
1440-
struct thermal_zone_device_ops *ops,
1440+
const struct thermal_zone_device_ops *ops,
14411441
const struct thermal_zone_params *tzp)
14421442
{
14431443
return thermal_zone_device_register_with_trips(type, NULL, 0, 0, devdata,
@@ -1499,8 +1499,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
14991499

15001500
/* Unbind all cdevs associated with 'this' thermal zone */
15011501
list_for_each_entry(cdev, &thermal_cdev_list, node)
1502-
if (tz->ops->unbind)
1503-
tz->ops->unbind(tz, cdev);
1502+
if (tz->ops.unbind)
1503+
tz->ops.unbind(tz, cdev);
15041504

15051505
mutex_unlock(&thermal_list_lock);
15061506

drivers/thermal/thermal_helpers.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip
2626
{
2727
enum thermal_trend trend;
2828

29-
if (tz->emul_temperature || !tz->ops->get_trend ||
30-
tz->ops->get_trend(tz, trip, &trend)) {
29+
if (tz->emul_temperature || !tz->ops.get_trend ||
30+
tz->ops.get_trend(tz, trip, &trend)) {
3131
if (tz->temperature > tz->last_temperature)
3232
trend = THERMAL_TREND_RAISING;
3333
else if (tz->temperature < tz->last_temperature)
@@ -75,7 +75,7 @@ EXPORT_SYMBOL(get_thermal_instance);
7575
* temperature and fill @temp.
7676
*
7777
* Both tz and tz->ops must be valid pointers when calling this function,
78-
* and the tz->ops->get_temp callback must be provided.
78+
* and the tz->ops.get_temp callback must be provided.
7979
* The function must be called under tz->lock.
8080
*
8181
* Return: On success returns 0, an error code otherwise
@@ -88,7 +88,7 @@ int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
8888

8989
lockdep_assert_held(&tz->lock);
9090

91-
ret = tz->ops->get_temp(tz, temp);
91+
ret = tz->ops.get_temp(tz, temp);
9292

9393
if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
9494
for_each_trip(tz, trip) {
@@ -132,7 +132,7 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
132132

133133
mutex_lock(&tz->lock);
134134

135-
if (!tz->ops->get_temp) {
135+
if (!tz->ops.get_temp) {
136136
ret = -EINVAL;
137137
goto unlock;
138138
}

drivers/thermal/thermal_hwmon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
8080

8181
mutex_lock(&tz->lock);
8282

83-
ret = tz->ops->get_crit_temp(tz, &temperature);
83+
ret = tz->ops.get_crit_temp(tz, &temperature);
8484

8585
mutex_unlock(&tz->lock);
8686

@@ -132,7 +132,7 @@ thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
132132
static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
133133
{
134134
int temp;
135-
return tz->ops->get_crit_temp && !tz->ops->get_crit_temp(tz, &temp);
135+
return tz->ops.get_crit_temp && !tz->ops.get_crit_temp(tz, &temp);
136136
}
137137

138138
int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)

drivers/thermal/thermal_of.c

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,8 @@ 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_zone_device_ops *ops = tz->ops;
442-
443441
thermal_zone_device_disable(tz);
444442
thermal_zone_device_unregister(tz);
445-
kfree(ops);
446443
}
447444

448445
/**
@@ -468,33 +465,27 @@ static void thermal_of_zone_unregister(struct thermal_zone_device *tz)
468465
static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
469466
const struct thermal_zone_device_ops *ops)
470467
{
468+
struct thermal_zone_device_ops of_ops = *ops;
471469
struct thermal_zone_device *tz;
472470
struct thermal_trip *trips;
473471
struct thermal_zone_params tzp = {};
474-
struct thermal_zone_device_ops *of_ops;
475472
struct device_node *np;
476473
const char *action;
477474
int delay, pdelay;
478475
int ntrips, mask;
479476
int ret;
480477

481-
of_ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
482-
if (!of_ops)
483-
return ERR_PTR(-ENOMEM);
484-
485478
np = of_thermal_zone_find(sensor, id);
486479
if (IS_ERR(np)) {
487480
if (PTR_ERR(np) != -ENODEV)
488481
pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id);
489-
ret = PTR_ERR(np);
490-
goto out_kfree_of_ops;
482+
return ERR_CAST(np);
491483
}
492484

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

500491
ret = thermal_of_monitor_init(np, &delay, &pdelay);
@@ -505,18 +496,18 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
505496

506497
thermal_of_parameters_init(np, &tzp);
507498

508-
of_ops->bind = thermal_of_bind;
509-
of_ops->unbind = thermal_of_unbind;
499+
of_ops.bind = thermal_of_bind;
500+
of_ops.unbind = thermal_of_unbind;
510501

511502
mask = GENMASK_ULL((ntrips) - 1, 0);
512503

513504
ret = of_property_read_string(np, "critical-action", &action);
514505
if (!ret)
515-
if (!of_ops->critical && !strcasecmp(action, "reboot"))
516-
of_ops->critical = thermal_zone_device_critical_reboot;
506+
if (!of_ops.critical && !strcasecmp(action, "reboot"))
507+
of_ops.critical = thermal_zone_device_critical_reboot;
517508

518509
tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips,
519-
mask, data, of_ops, &tzp,
510+
mask, data, &of_ops, &tzp,
520511
pdelay, delay);
521512
if (IS_ERR(tz)) {
522513
ret = PTR_ERR(tz);
@@ -538,8 +529,6 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
538529

539530
out_kfree_trips:
540531
kfree(trips);
541-
out_kfree_of_ops:
542-
kfree(of_ops);
543532

544533
return ERR_PTR(ret);
545534
}

drivers/thermal/thermal_sysfs.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
123123
trip = &tz->trips[trip_id];
124124

125125
if (temp != trip->temperature) {
126-
if (tz->ops->set_trip_temp) {
127-
ret = tz->ops->set_trip_temp(tz, trip_id, temp);
126+
if (tz->ops.set_trip_temp) {
127+
ret = tz->ops.set_trip_temp(tz, trip_id, temp);
128128
if (ret)
129129
goto unlock;
130130
}
@@ -174,8 +174,8 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
174174
trip = &tz->trips[trip_id];
175175

176176
if (hyst != trip->hysteresis) {
177-
if (tz->ops->set_trip_hyst) {
178-
ret = tz->ops->set_trip_hyst(tz, trip_id, hyst);
177+
if (tz->ops.set_trip_hyst) {
178+
ret = tz->ops.set_trip_hyst(tz, trip_id, hyst);
179179
if (ret)
180180
goto unlock;
181181
}
@@ -250,10 +250,10 @@ emul_temp_store(struct device *dev, struct device_attribute *attr,
250250

251251
mutex_lock(&tz->lock);
252252

253-
if (!tz->ops->set_emul_temp)
253+
if (!tz->ops.set_emul_temp)
254254
tz->emul_temperature = temperature;
255255
else
256-
ret = tz->ops->set_emul_temp(tz, temperature);
256+
ret = tz->ops.set_emul_temp(tz, temperature);
257257

258258
if (!ret)
259259
__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
@@ -474,7 +474,7 @@ static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
474474
tz->trip_hyst_attrs[indx].name;
475475
tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
476476
tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
477-
if (tz->ops->set_trip_hyst) {
477+
if (tz->ops.set_trip_hyst) {
478478
tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
479479
tz->trip_hyst_attrs[indx].attr.store =
480480
trip_point_hyst_store;

drivers/thermal/thermal_trip.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz)
7070

7171
lockdep_assert_held(&tz->lock);
7272

73-
if (!tz->ops->set_trips)
73+
if (!tz->ops.set_trips)
7474
return;
7575

7676
for_each_trip(tz, trip) {
@@ -114,7 +114,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz)
114114
* Set a temperature window. When this window is left the driver
115115
* must inform the thermal core via thermal_zone_device_update.
116116
*/
117-
ret = tz->ops->set_trips(tz, low, high);
117+
ret = tz->ops.set_trips(tz, low, high);
118118
if (ret)
119119
dev_err(&tz->device, "Failed to set trips: %d\n", ret);
120120
}

include/linux/thermal.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ struct thermal_zone_device {
182182
int prev_low_trip;
183183
int prev_high_trip;
184184
atomic_t need_update;
185-
struct thermal_zone_device_ops *ops;
185+
struct thermal_zone_device_ops ops;
186186
struct thermal_zone_params *tzp;
187187
struct thermal_governor *governor;
188188
void *governor_data;
@@ -318,14 +318,14 @@ struct thermal_zone_device *thermal_zone_device_register_with_trips(
318318
const struct thermal_trip *trips,
319319
int num_trips, int mask,
320320
void *devdata,
321-
struct thermal_zone_device_ops *ops,
321+
const struct thermal_zone_device_ops *ops,
322322
const struct thermal_zone_params *tzp,
323323
int passive_delay, int polling_delay);
324324

325325
struct thermal_zone_device *thermal_tripless_zone_device_register(
326326
const char *type,
327327
void *devdata,
328-
struct thermal_zone_device_ops *ops,
328+
const struct thermal_zone_device_ops *ops,
329329
const struct thermal_zone_params *tzp);
330330

331331
void thermal_zone_device_unregister(struct thermal_zone_device *tz);
@@ -378,7 +378,7 @@ static inline struct thermal_zone_device *thermal_zone_device_register_with_trip
378378
const struct thermal_trip *trips,
379379
int num_trips, int mask,
380380
void *devdata,
381-
struct thermal_zone_device_ops *ops,
381+
const struct thermal_zone_device_ops *ops,
382382
const struct thermal_zone_params *tzp,
383383
int passive_delay, int polling_delay)
384384
{ return ERR_PTR(-ENODEV); }

0 commit comments

Comments
 (0)