Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

thermal: core: Move threshold out of struct thermal_trip

The threshold field in struct thermal_trip is only used internally by
the thermal core and it is better to prevent drivers from misusing it.
It also takes some space unnecessarily in the trip tables passed by
drivers to the core during thermal zone registration.

For this reason, introduce struct thermal_trip_desc as a wrapper around
struct thermal_trip, move the threshold field directly into it and make
the thermal core store struct thermal_trip_desc objects in the internal
thermal zone trip tables. Adjust all of the code using trip tables in
the thermal core accordingly.

No intentional functional impact.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Lukasz Luba <lukasz.luba@arm.com>

+78 -52
+5 -2
drivers/thermal/gov_fair_share.c
··· 17 17 18 18 static int get_trip_level(struct thermal_zone_device *tz) 19 19 { 20 - const struct thermal_trip *trip, *level_trip = NULL; 20 + const struct thermal_trip *level_trip = NULL; 21 + const struct thermal_trip_desc *td; 21 22 int trip_level = -1; 22 23 23 - for_each_trip(tz, trip) { 24 + for_each_trip_desc(tz, td) { 25 + const struct thermal_trip *trip = &td->trip; 26 + 24 27 if (trip->temperature >= tz->temperature) 25 28 continue; 26 29
+4 -2
drivers/thermal/gov_power_allocator.c
··· 496 496 const struct thermal_trip *first_passive = NULL; 497 497 const struct thermal_trip *last_passive = NULL; 498 498 const struct thermal_trip *last_active = NULL; 499 - const struct thermal_trip *trip; 499 + const struct thermal_trip_desc *td; 500 500 501 - for_each_trip(tz, trip) { 501 + for_each_trip_desc(tz, td) { 502 + const struct thermal_trip *trip = &td->trip; 503 + 502 504 switch (trip->type) { 503 505 case THERMAL_TRIP_PASSIVE: 504 506 if (!first_passive) {
+27 -19
drivers/thermal/thermal_core.c
··· 361 361 } 362 362 363 363 static void handle_thermal_trip(struct thermal_zone_device *tz, 364 - struct thermal_trip *trip) 364 + struct thermal_trip_desc *td) 365 365 { 366 + const struct thermal_trip *trip = &td->trip; 367 + 366 368 if (trip->temperature == THERMAL_TEMP_INVALID) 367 369 return; 368 370 369 371 if (tz->last_temperature == THERMAL_TEMP_INVALID) { 370 372 /* Initialization. */ 371 - trip->threshold = trip->temperature; 372 - if (tz->temperature >= trip->threshold) 373 - trip->threshold -= trip->hysteresis; 374 - } else if (tz->last_temperature < trip->threshold) { 373 + td->threshold = trip->temperature; 374 + if (tz->temperature >= td->threshold) 375 + td->threshold -= trip->hysteresis; 376 + } else if (tz->last_temperature < td->threshold) { 375 377 /* 376 378 * The trip threshold is equal to the trip temperature, unless 377 379 * the latter has changed in the meantime. In either case, ··· 384 382 if (tz->temperature >= trip->temperature) { 385 383 thermal_notify_tz_trip_up(tz, trip); 386 384 thermal_debug_tz_trip_up(tz, trip); 387 - trip->threshold = trip->temperature - trip->hysteresis; 385 + td->threshold = trip->temperature - trip->hysteresis; 388 386 } else { 389 - trip->threshold = trip->temperature; 387 + td->threshold = trip->temperature; 390 388 } 391 389 } else { 392 390 /* ··· 402 400 if (tz->temperature < trip->temperature - trip->hysteresis) { 403 401 thermal_notify_tz_trip_down(tz, trip); 404 402 thermal_debug_tz_trip_down(tz, trip); 405 - trip->threshold = trip->temperature; 403 + td->threshold = trip->temperature; 406 404 } else { 407 - trip->threshold = trip->temperature - trip->hysteresis; 405 + td->threshold = trip->temperature - trip->hysteresis; 408 406 } 409 407 } 410 408 ··· 460 458 void __thermal_zone_device_update(struct thermal_zone_device *tz, 461 459 enum thermal_notify_event event) 462 460 { 463 - struct thermal_trip *trip; 461 + struct thermal_trip_desc *td; 464 462 465 463 if (tz->suspended) 466 464 return; ··· 474 472 475 473 tz->notify_event = event; 476 474 477 - for_each_trip(tz, trip) 478 - handle_thermal_trip(tz, trip); 475 + for_each_trip_desc(tz, td) 476 + handle_thermal_trip(tz, td); 479 477 480 478 monitor_thermal_zone(tz); 481 479 } ··· 768 766 if (trip_index < 0 || trip_index >= tz->num_trips) 769 767 return -EINVAL; 770 768 771 - return thermal_bind_cdev_to_trip(tz, &tz->trips[trip_index], cdev, 769 + return thermal_bind_cdev_to_trip(tz, &tz->trips[trip_index].trip, cdev, 772 770 upper, lower, weight); 773 771 } 774 772 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device); ··· 827 825 if (trip_index < 0 || trip_index >= tz->num_trips) 828 826 return -EINVAL; 829 827 830 - return thermal_unbind_cdev_from_trip(tz, &tz->trips[trip_index], cdev); 828 + return thermal_unbind_cdev_from_trip(tz, &tz->trips[trip_index].trip, cdev); 831 829 } 832 830 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device); 833 831 ··· 1223 1221 1224 1222 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp) 1225 1223 { 1226 - int i, ret = -EINVAL; 1224 + const struct thermal_trip_desc *td; 1225 + int ret = -EINVAL; 1227 1226 1228 1227 if (tz->ops.get_crit_temp) 1229 1228 return tz->ops.get_crit_temp(tz, temp); 1230 1229 1231 1230 mutex_lock(&tz->lock); 1232 1231 1233 - for (i = 0; i < tz->num_trips; i++) { 1234 - if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) { 1235 - *temp = tz->trips[i].temperature; 1232 + for_each_trip_desc(tz, td) { 1233 + const struct thermal_trip *trip = &td->trip; 1234 + 1235 + if (trip->type == THERMAL_TRIP_CRITICAL) { 1236 + *temp = trip->temperature; 1236 1237 ret = 0; 1237 1238 break; 1238 1239 } ··· 1279 1274 const struct thermal_zone_params *tzp, 1280 1275 int passive_delay, int polling_delay) 1281 1276 { 1277 + const struct thermal_trip *trip = trips; 1282 1278 struct thermal_zone_device *tz; 1279 + struct thermal_trip_desc *td; 1283 1280 int id; 1284 1281 int result; 1285 1282 struct thermal_governor *governor; ··· 1346 1339 tz->device.class = thermal_class; 1347 1340 tz->devdata = devdata; 1348 1341 tz->num_trips = num_trips; 1349 - memcpy(tz->trips, trips, num_trips * sizeof(*trips)); 1342 + for_each_trip_desc(tz, td) 1343 + td->trip = *trip++; 1350 1344 1351 1345 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay); 1352 1346 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
+5 -2
drivers/thermal/thermal_core.h
··· 120 120 enum thermal_notify_event reason); 121 121 122 122 /* Helpers */ 123 - #define for_each_trip(__tz, __trip) \ 124 - for (__trip = __tz->trips; __trip - __tz->trips < __tz->num_trips; __trip++) 123 + #define for_each_trip_desc(__tz, __td) \ 124 + for (__td = __tz->trips; __td - __tz->trips < __tz->num_trips; __td++) 125 + 126 + #define trip_to_trip_desc(__trip) \ 127 + container_of(__trip, struct thermal_trip_desc, trip) 125 128 126 129 void __thermal_zone_set_trips(struct thermal_zone_device *tz); 127 130 int thermal_zone_trip_id(const struct thermal_zone_device *tz,
+4 -2
drivers/thermal/thermal_debugfs.c
··· 744 744 static int tze_seq_show(struct seq_file *s, void *v) 745 745 { 746 746 struct thermal_zone_device *tz = s->private; 747 - struct thermal_trip *trip; 747 + struct thermal_trip_desc *td; 748 748 struct tz_episode *tze; 749 749 const char *type; 750 750 int trip_id; ··· 757 757 758 758 seq_printf(s, "| trip | type | temp(°mC) | hyst(°mC) | duration | avg(°mC) | min(°mC) | max(°mC) |\n"); 759 759 760 - for_each_trip(tz, trip) { 760 + for_each_trip_desc(tz, td) { 761 + const struct thermal_trip *trip = &td->trip; 762 + 761 763 /* 762 764 * There is no possible mitigation happening at the 763 765 * critical trip point, so the stats will be always
+5 -3
drivers/thermal/thermal_helpers.c
··· 50 50 mutex_lock(&tz->lock); 51 51 mutex_lock(&cdev->lock); 52 52 53 - trip = &tz->trips[trip_index]; 53 + trip = &tz->trips[trip_index].trip; 54 54 55 55 list_for_each_entry(pos, &tz->thermal_instances, tz_node) { 56 56 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { ··· 82 82 */ 83 83 int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) 84 84 { 85 - const struct thermal_trip *trip; 85 + const struct thermal_trip_desc *td; 86 86 int crit_temp = INT_MAX; 87 87 int ret = -EINVAL; 88 88 ··· 91 91 ret = tz->ops.get_temp(tz, temp); 92 92 93 93 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) { 94 - for_each_trip(tz, trip) { 94 + for_each_trip_desc(tz, td) { 95 + const struct thermal_trip *trip = &td->trip; 96 + 95 97 if (trip->type == THERMAL_TRIP_CRITICAL) { 96 98 crit_temp = trip->temperature; 97 99 break;
+4 -2
drivers/thermal/thermal_netlink.c
··· 445 445 static int thermal_genl_cmd_tz_get_trip(struct param *p) 446 446 { 447 447 struct sk_buff *msg = p->msg; 448 - const struct thermal_trip *trip; 448 + const struct thermal_trip_desc *td; 449 449 struct thermal_zone_device *tz; 450 450 struct nlattr *start_trip; 451 451 int id; ··· 465 465 466 466 mutex_lock(&tz->lock); 467 467 468 - for_each_trip(tz, trip) { 468 + for_each_trip_desc(tz, td) { 469 + const struct thermal_trip *trip = &td->trip; 470 + 469 471 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, 470 472 thermal_zone_trip_id(tz, trip)) || 471 473 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, trip->type) ||
+10 -10
drivers/thermal/thermal_sysfs.c
··· 88 88 if (sscanf(attr->attr.name, "trip_point_%d_type", &trip_id) != 1) 89 89 return -EINVAL; 90 90 91 - switch (tz->trips[trip_id].type) { 91 + switch (tz->trips[trip_id].trip.type) { 92 92 case THERMAL_TRIP_CRITICAL: 93 93 return sprintf(buf, "critical\n"); 94 94 case THERMAL_TRIP_HOT: ··· 120 120 121 121 mutex_lock(&tz->lock); 122 122 123 - trip = &tz->trips[trip_id]; 123 + trip = &tz->trips[trip_id].trip; 124 124 125 125 if (temp != trip->temperature) { 126 126 if (tz->ops.set_trip_temp) { ··· 150 150 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1) 151 151 return -EINVAL; 152 152 153 - return sprintf(buf, "%d\n", tz->trips[trip_id].temperature); 153 + return sprintf(buf, "%d\n", tz->trips[trip_id].trip.temperature); 154 154 } 155 155 156 156 static ssize_t ··· 171 171 172 172 mutex_lock(&tz->lock); 173 173 174 - trip = &tz->trips[trip_id]; 174 + trip = &tz->trips[trip_id].trip; 175 175 176 176 if (hyst != trip->hysteresis) { 177 177 trip->hysteresis = hyst; ··· 194 194 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1) 195 195 return -EINVAL; 196 196 197 - return sprintf(buf, "%d\n", tz->trips[trip_id].hysteresis); 197 + return sprintf(buf, "%d\n", tz->trips[trip_id].trip.hysteresis); 198 198 } 199 199 200 200 static ssize_t ··· 393 393 */ 394 394 static int create_trip_attrs(struct thermal_zone_device *tz) 395 395 { 396 - const struct thermal_trip *trip; 396 + const struct thermal_trip_desc *td; 397 397 struct attribute **attrs; 398 398 399 399 /* This function works only for zones with at least one trip */ ··· 429 429 return -ENOMEM; 430 430 } 431 431 432 - for_each_trip(tz, trip) { 433 - int indx = thermal_zone_trip_id(tz, trip); 432 + for_each_trip_desc(tz, td) { 433 + int indx = thermal_zone_trip_id(tz, &td->trip); 434 434 435 435 /* create trip type attribute */ 436 436 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH, ··· 452 452 tz->trip_temp_attrs[indx].name; 453 453 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO; 454 454 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show; 455 - if (trip->flags & THERMAL_TRIP_FLAG_RW_TEMP) { 455 + if (td->trip.flags & THERMAL_TRIP_FLAG_RW_TEMP) { 456 456 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR; 457 457 tz->trip_temp_attrs[indx].attr.store = 458 458 trip_point_temp_store; ··· 467 467 tz->trip_hyst_attrs[indx].name; 468 468 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO; 469 469 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show; 470 - if (trip->flags & THERMAL_TRIP_FLAG_RW_HYST) { 470 + if (td->trip.flags & THERMAL_TRIP_FLAG_RW_HYST) { 471 471 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR; 472 472 tz->trip_hyst_attrs[indx].attr.store = 473 473 trip_point_hyst_store;
+8 -7
drivers/thermal/thermal_trip.c
··· 13 13 int (*cb)(struct thermal_trip *, void *), 14 14 void *data) 15 15 { 16 - struct thermal_trip *trip; 16 + struct thermal_trip_desc *td; 17 17 int ret; 18 18 19 - for_each_trip(tz, trip) { 20 - ret = cb(trip, data); 19 + for_each_trip_desc(tz, td) { 20 + ret = cb(&td->trip, data); 21 21 if (ret) 22 22 return ret; 23 23 } ··· 63 63 */ 64 64 void __thermal_zone_set_trips(struct thermal_zone_device *tz) 65 65 { 66 - const struct thermal_trip *trip; 66 + const struct thermal_trip_desc *td; 67 67 int low = -INT_MAX, high = INT_MAX; 68 68 int ret; 69 69 ··· 72 72 if (!tz->ops.set_trips) 73 73 return; 74 74 75 - for_each_trip(tz, trip) { 75 + for_each_trip_desc(tz, td) { 76 + const struct thermal_trip *trip = &td->trip; 76 77 int trip_low; 77 78 78 79 trip_low = trip->temperature - trip->hysteresis; ··· 111 110 if (!tz || trip_id < 0 || trip_id >= tz->num_trips || !trip) 112 111 return -EINVAL; 113 112 114 - *trip = tz->trips[trip_id]; 113 + *trip = tz->trips[trip_id].trip; 115 114 return 0; 116 115 } 117 116 EXPORT_SYMBOL_GPL(__thermal_zone_get_trip); ··· 136 135 * Assume the trip to be located within the bounds of the thermal 137 136 * zone's trips[] table. 138 137 */ 139 - return trip - tz->trips; 138 + return trip_to_trip_desc(trip) - tz->trips; 140 139 } 141 140 void thermal_zone_trip_updated(struct thermal_zone_device *tz, 142 141 const struct thermal_trip *trip)
+6 -3
include/linux/thermal.h
··· 61 61 * struct thermal_trip - representation of a point in temperature domain 62 62 * @temperature: temperature value in miliCelsius 63 63 * @hysteresis: relative hysteresis in miliCelsius 64 - * @threshold: trip crossing notification threshold miliCelsius 65 64 * @type: trip point type 66 65 * @priv: pointer to driver data associated with this trip 67 66 * @flags: flags representing binary properties of the trip ··· 68 69 struct thermal_trip { 69 70 int temperature; 70 71 int hysteresis; 71 - int threshold; 72 72 enum thermal_trip_type type; 73 73 u8 flags; 74 74 void *priv; 75 + }; 76 + 77 + struct thermal_trip_desc { 78 + struct thermal_trip trip; 79 + int threshold; 75 80 }; 76 81 77 82 #define THERMAL_TRIP_FLAG_RW_TEMP BIT(0) ··· 206 203 #ifdef CONFIG_THERMAL_DEBUGFS 207 204 struct thermal_debugfs *debugfs; 208 205 #endif 209 - struct thermal_trip trips[] __counted_by(num_trips); 206 + struct thermal_trip_desc trips[] __counted_by(num_trips); 210 207 }; 211 208 212 209 /**