Skip to content

Commit 7454f2c

Browse files
committed
thermal: core: Sort trip point crossing notifications by temperature
If multiple trip points are crossed in one go and the trips table in the thermal zone device object is not sorted, the corresponding trip point crossing notifications sent to user space will not be ordered either. Moreover, if the trips table is sorted by trip temperature in ascending order, the trip crossing notifications on the way up will be sent in that order too, but the trip crossing notifications on the way down will be sent in the reverse order. This is generally confusing and it is better to make the kernel send the notifications in the order of growing (on the way up) or falling (on the way down) trip temperature. To achieve that, instead of sending a trip crossing notification and recording a trip crossing event in the statistics right away from handle_thermal_trip(), put the trip in question on a list that will be sorted by __thermal_zone_device_update() after processing all of the trips and before sending the notifications and recording trip crossing events. Link: https://lore.kernel.org/linux-pm/20240306085428.88011-1-daniel.lezcano@linaro.org/ Reported-by: Daniel Lezcano <daniel.lezcano@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Reviewed-by: Lukasz Luba <lukasz.luba@arm.com>
1 parent 9ad1804 commit 7454f2c

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

drivers/thermal/thermal_core.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/slab.h>
1616
#include <linux/kdev_t.h>
1717
#include <linux/idr.h>
18+
#include <linux/list_sort.h>
1819
#include <linux/thermal.h>
1920
#include <linux/reboot.h>
2021
#include <linux/string.h>
@@ -361,7 +362,9 @@ static void handle_critical_trips(struct thermal_zone_device *tz,
361362
}
362363

363364
static void handle_thermal_trip(struct thermal_zone_device *tz,
364-
struct thermal_trip_desc *td)
365+
struct thermal_trip_desc *td,
366+
struct list_head *way_up_list,
367+
struct list_head *way_down_list)
365368
{
366369
const struct thermal_trip *trip = &td->trip;
367370
int old_threshold;
@@ -387,8 +390,8 @@ static void handle_thermal_trip(struct thermal_zone_device *tz,
387390
* In that case, the trip temperature becomes the new threshold.
388391
*/
389392
if (tz->temperature < trip->temperature - trip->hysteresis) {
390-
thermal_notify_tz_trip_down(tz, trip);
391-
thermal_debug_tz_trip_down(tz, trip);
393+
list_add(&td->notify_list_node, way_down_list);
394+
td->notify_temp = trip->temperature - trip->hysteresis;
392395
} else {
393396
td->threshold -= trip->hysteresis;
394397
}
@@ -398,8 +401,8 @@ static void handle_thermal_trip(struct thermal_zone_device *tz,
398401
* if the zone temperature exceeds the trip one. The new
399402
* threshold is then set to the low temperature of the trip.
400403
*/
401-
thermal_notify_tz_trip_up(tz, trip);
402-
thermal_debug_tz_trip_up(tz, trip);
404+
list_add_tail(&td->notify_list_node, way_up_list);
405+
td->notify_temp = trip->temperature;
403406
td->threshold -= trip->hysteresis;
404407
}
405408

@@ -452,10 +455,24 @@ static void thermal_zone_device_init(struct thermal_zone_device *tz)
452455
pos->initialized = false;
453456
}
454457

458+
static int thermal_trip_notify_cmp(void *ascending, const struct list_head *a,
459+
const struct list_head *b)
460+
{
461+
struct thermal_trip_desc *tda = container_of(a, struct thermal_trip_desc,
462+
notify_list_node);
463+
struct thermal_trip_desc *tdb = container_of(b, struct thermal_trip_desc,
464+
notify_list_node);
465+
int ret = tdb->notify_temp - tda->notify_temp;
466+
467+
return ascending ? ret : -ret;
468+
}
469+
455470
void __thermal_zone_device_update(struct thermal_zone_device *tz,
456471
enum thermal_notify_event event)
457472
{
458473
struct thermal_trip_desc *td;
474+
LIST_HEAD(way_down_list);
475+
LIST_HEAD(way_up_list);
459476

460477
if (tz->suspended)
461478
return;
@@ -470,7 +487,19 @@ void __thermal_zone_device_update(struct thermal_zone_device *tz,
470487
tz->notify_event = event;
471488

472489
for_each_trip_desc(tz, td)
473-
handle_thermal_trip(tz, td);
490+
handle_thermal_trip(tz, td, &way_up_list, &way_down_list);
491+
492+
list_sort(&way_up_list, &way_up_list, thermal_trip_notify_cmp);
493+
list_for_each_entry(td, &way_up_list, notify_list_node) {
494+
thermal_notify_tz_trip_up(tz, &td->trip);
495+
thermal_debug_tz_trip_up(tz, &td->trip);
496+
}
497+
498+
list_sort(NULL, &way_down_list, thermal_trip_notify_cmp);
499+
list_for_each_entry(td, &way_down_list, notify_list_node) {
500+
thermal_notify_tz_trip_down(tz, &td->trip);
501+
thermal_debug_tz_trip_down(tz, &td->trip);
502+
}
474503

475504
monitor_thermal_zone(tz);
476505
}

drivers/thermal/thermal_core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
struct thermal_trip_desc {
1919
struct thermal_trip trip;
20+
struct list_head notify_list_node;
21+
int notify_temp;
2022
int threshold;
2123
};
2224

0 commit comments

Comments
 (0)