Skip to content

Commit c12629f

Browse files
committed
thermal: core: Build sorted lists instead of sorting them later
Since it is not expected that multiple trip points will be crossed in one go very often (if this happens, there are too many trip points in the given thermal zone or they are checked too rarely), quite likely it is more efficient to build a sorted list of crossed trip points than to put them on an unsorted list and sort it later. Moreover, trip points are often sorted in ascending temperature order during thermal zone registration, so building a sorted list out of them is quite straightforward and relatively inexpensive. Accordingly, make handle_thermal_trip() maintain list ordering when adding trip points to the lists and get rid of separate list sorting in __thermal_zone_device_update(). No intentional functional impact. Signed-off-by: Rafael J. Wysocki <[email protected]> Reviewed-by: Lukasz Luba <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 7569406 commit c12629f

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

drivers/thermal/thermal_core.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <linux/slab.h>
1616
#include <linux/kdev_t.h>
1717
#include <linux/idr.h>
18-
#include <linux/list_sort.h>
1918
#include <linux/thermal.h>
2019
#include <linux/reboot.h>
2120
#include <linux/string.h>
@@ -409,6 +408,21 @@ static void handle_critical_trips(struct thermal_zone_device *tz,
409408
tz->ops.hot(tz);
410409
}
411410

411+
static void add_trip_to_sorted_list(struct thermal_trip_desc *td,
412+
struct list_head *list)
413+
{
414+
struct thermal_trip_desc *entry;
415+
416+
/* Assume that the new entry is likely to be the last one. */
417+
list_for_each_entry_reverse(entry, list, notify_list_node) {
418+
if (entry->notify_temp <= td->notify_temp) {
419+
list_add(&td->notify_list_node, &entry->notify_list_node);
420+
return;
421+
}
422+
}
423+
list_add(&td->notify_list_node, list);
424+
}
425+
412426
static void handle_thermal_trip(struct thermal_zone_device *tz,
413427
struct thermal_trip_desc *td,
414428
struct list_head *way_up_list,
@@ -438,8 +452,8 @@ static void handle_thermal_trip(struct thermal_zone_device *tz,
438452
* In that case, the trip temperature becomes the new threshold.
439453
*/
440454
if (tz->temperature < trip->temperature - trip->hysteresis) {
441-
list_add(&td->notify_list_node, way_down_list);
442455
td->notify_temp = trip->temperature - trip->hysteresis;
456+
add_trip_to_sorted_list(td, way_down_list);
443457

444458
if (trip->type == THERMAL_TRIP_PASSIVE) {
445459
tz->passive--;
@@ -454,8 +468,9 @@ static void handle_thermal_trip(struct thermal_zone_device *tz,
454468
* if the zone temperature exceeds the trip one. The new
455469
* threshold is then set to the low temperature of the trip.
456470
*/
457-
list_add_tail(&td->notify_list_node, way_up_list);
458471
td->notify_temp = trip->temperature;
472+
add_trip_to_sorted_list(td, way_up_list);
473+
459474
td->threshold -= trip->hysteresis;
460475

461476
if (trip->type == THERMAL_TRIP_PASSIVE)
@@ -519,16 +534,6 @@ static void thermal_trip_crossed(struct thermal_zone_device *tz,
519534
thermal_governor_trip_crossed(governor, tz, trip, crossed_up);
520535
}
521536

522-
static int thermal_trip_notify_cmp(void *not_used, const struct list_head *a,
523-
const struct list_head *b)
524-
{
525-
struct thermal_trip_desc *tda = container_of(a, struct thermal_trip_desc,
526-
notify_list_node);
527-
struct thermal_trip_desc *tdb = container_of(b, struct thermal_trip_desc,
528-
notify_list_node);
529-
return tda->notify_temp - tdb->notify_temp;
530-
}
531-
532537
void __thermal_zone_device_update(struct thermal_zone_device *tz,
533538
enum thermal_notify_event event)
534539
{
@@ -581,11 +586,9 @@ void __thermal_zone_device_update(struct thermal_zone_device *tz,
581586

582587
thermal_zone_set_trips(tz, low, high);
583588

584-
list_sort(NULL, &way_up_list, thermal_trip_notify_cmp);
585589
list_for_each_entry(td, &way_up_list, notify_list_node)
586590
thermal_trip_crossed(tz, &td->trip, governor, true);
587591

588-
list_sort(NULL, &way_down_list, thermal_trip_notify_cmp);
589592
list_for_each_entry_reverse(td, &way_down_list, notify_list_node)
590593
thermal_trip_crossed(tz, &td->trip, governor, false);
591594

0 commit comments

Comments
 (0)