thermal: core: Updates related to thermal instances, trip points handling and locking
Subsystems (2)
PATCH 4/8 thermal: core: Initialize thermal zones before registerimg them
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Since user space can start interacting with a new thermal zone as soon as device_register() called by thermal_zone_device_register_with_trips() returns, it is better to initialize the thermal zone before calling device_register() on it.
--- drivers/thermal/thermal_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/thermal/thermal_core.c +1 -1
@@ -1469,6 +1469,7 @@ thermal_zone_device_register_with_trips(thermal_zone_destroy_device_groups (tz); goto remove_id; } +thermal_zone_device_init (tz); result =device_register (&tz->device);if (result) goto release_device;
@@ -1507,7 +1508,6 @@ thermal_zone_device_register_with_trips(mutex_unlock (&thermal_list_lock); -thermal_zone_device_init (tz);/* Update the new thermal zone and mark it as already updated. */ if (atomic_cmpxchg (&tz->need_update,1 ,0 ))thermal_zone_device_update (tz,THERMAL_EVENT_UNSPECIFIED );
PATCH 3/8 thermal: core: Build sorted lists instead of sorting them later
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Large numbers of trip points are not expected to be crossed in one go, so 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 provided in ascending temperature order during thermal zone registration. so they are naturally sorted anyway and building a sorted list out of them is quite straightforward.
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.
--- drivers/thermal/thermal_core.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-)
drivers/thermal/thermal_core.c +18 -15
@@ -15,7 +15,6 @@#include <linux/slab.h> #include <linux/kdev_t.h> #include <linux/idr.h> -#include <linux/list_sort.h> #include <linux/thermal.h> #include <linux/reboot.h> #include <linux/string.h>
@@ -421,6 +420,21 @@ static void handle_critical_trips(struct tz->ops.hot (tz); } +static void add_trip_to_sorted_list (struct thermal_trip_desc *td, +struct list_head *list) +{ +struct thermal_trip_desc *entry; + +/* Assume that the new entry is likely to be the last one. */ +list_for_each_entry_reverse (entry, list, notify_list_node) { +if (entry->notify_temp <= td->notify_temp) { +list_add (&td->notify_list_node, &entry->notify_list_node); +return ; + } + } +list_add (&td->notify_list_node, list); +} + static void handle_thermal_trip(struct thermal_zone_device *tz, struct thermal_trip_desc *td, struct list_head *way_up_list,
@@ -450,8 +464,8 @@ static void handle_thermal_trip(struct t* In that case, the trip temperature becomes the new threshold. */ if (tz->temperature < trip->temperature - trip->hysteresis) { -list_add (&td->notify_list_node, way_down_list); td->notify_temp = trip->temperature - trip->hysteresis; +add_trip_to_sorted_list (td, way_down_list);if (trip->type ==THERMAL_TRIP_PASSIVE ) { tz->passive--;
@@ -466,8 +480,9 @@ static void handle_thermal_trip(struct t* if the zone temperature exceeds the trip one. The new * threshold is then set to the low temperature of the trip. */ -list_add_tail (&td->notify_list_node, way_up_list); td->notify_temp = trip->temperature; +add_trip_to_sorted_list (td, way_up_list); + td->threshold -= trip->hysteresis;if (trip->type ==THERMAL_TRIP_PASSIVE )
@@ -531,16 +546,6 @@ static void thermal_trip_crossed(structthermal_governor_trip_crossed (governor, tz, trip, crossed_up); } -static int thermal_trip_notify_cmp (void *not_used,const struct list_head *a, -const struct list_head *b) -{ - struct thermal_trip_desc *tda = container_of(a, struct thermal_trip_desc, - notify_list_node); - struct thermal_trip_desc *tdb = container_of(b, struct thermal_trip_desc, - notify_list_node); -return tda->notify_temp - tdb->notify_temp; -} -void __thermal_zone_device_update (struct thermal_zone_device *tz,enum thermal_notify_event event) {
@@ -591,11 +596,9 @@ void __thermal_zone_device_update(structthermal_zone_set_trips (tz, low, high); -list_sort (NULL , &way_up_list, thermal_trip_notify_cmp);list_for_each_entry (td, &way_up_list, notify_list_node)thermal_trip_crossed (tz, &td->trip, governor, true); -list_sort (NULL , &way_down_list, thermal_trip_notify_cmp);list_for_each_entry_reverse (td, &way_down_list, notify_list_node)thermal_trip_crossed (tz, &td->trip, governor, false);
PATCH 2/8 thermal: core: Pass trip descriptors to trip bind/unbind functions
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Subject: [PATCH v3]
The code is somewhat cleaner if struct thermal_trip_desc pointers are passed to thermal_bind_cdev_to_trip(), thermal_unbind_cdev_from_trip(), and print_bind_err_msg() instead of struct thermal_trip pointers, so modify it accordingly.
No intentional functional impact.
--- drivers/thermal/thermal_core.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)
drivers/thermal/thermal_core.c +12 -15
@@ -754,9 +754,9 @@ struct thermal_zone_device *thermal_zone/** * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone * @tz: pointer to struct thermal_zone_device -* @trip: trip point the cooling devices is associated with in this zone. +* @td: descriptor of the trip point to bind @cdev to * @cdev: pointer to struct thermal_cooling_device -* @cool_spec: cooling specification for @trip and @cdev +* @cool_spec: cooling specification for the trip point and @cdev * * This interface function bind a thermal cooling device to the certain trip * point of a thermal zone device.
@@ -765,11 +765,10 @@ struct thermal_zone_device *thermal_zone* Return: 0 on success, the proper error value otherwise. */ static int thermal_bind_cdev_to_trip (struct thermal_zone_device *tz, -struct thermal_trip *trip, +struct thermal_trip_desc *td,struct thermal_cooling_device *cdev,struct cooling_spec *cool_spec) { -struct thermal_trip_desc *td =trip_to_trip_desc (trip);struct thermal_instance *dev, *instance;bool upper_no_limit;int result;
@@ -793,7 +792,7 @@ static int thermal_bind_cdev_to_trip(strreturn -ENOMEM ; dev->cdev = cdev; - dev->trip = trip; + dev->trip = &td->trip; dev->upper = cool_spec->upper; dev->upper_no_limit = upper_no_limit; dev->lower = cool_spec->lower;
@@ -865,7 +864,7 @@ free_mem:/** * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone. * @tz: pointer to a struct thermal_zone_device. -* @trip: trip point the cooling devices is associated with in this zone. +* @td: descriptor of the trip point to unbind @cdev from * @cdev: pointer to a struct thermal_cooling_device. * * This interface function unbind a thermal cooling device from the certain
@@ -873,10 +872,9 @@ free_mem:* This function is usually called in the thermal zone device .unbind callback. */ static void thermal_unbind_cdev_from_trip (struct thermal_zone_device *tz, -struct thermal_trip *trip, +struct thermal_trip_desc *td,struct thermal_cooling_device *cdev) { -struct thermal_trip_desc *td =trip_to_trip_desc (trip);struct thermal_instance *pos, *next;mutex_lock (&cdev->lock);
@@ -928,11 +926,11 @@ static struct class *thermal_class;static inline void print_bind_err_msg (struct thermal_zone_device *tz, -const struct thermal_trip *trip, +const struct thermal_trip_desc *td,struct thermal_cooling_device *cdev,int ret) {dev_err (&tz->device,"binding cdev %s to trip %d failed: %d\n" , - cdev->type,thermal_zone_trip_id (tz, trip), ret); + cdev->type,thermal_zone_trip_id (tz, &td->trip), ret); } static void thermal_zone_cdev_bind(struct thermal_zone_device *tz,
@@ -946,7 +944,6 @@ static void thermal_zone_cdev_bind(strucmutex_lock (&tz->lock);for_each_trip_desc (tz, td) { -struct thermal_trip *trip = &td->trip;struct cooling_spec c = { .upper =THERMAL_NO_LIMIT , .lower =THERMAL_NO_LIMIT ,
@@ -954,12 +951,12 @@ static void thermal_zone_cdev_bind(struc };int ret; -if (!tz->ops.should_bind (tz, trip, cdev, &c)) +if (!tz->ops.should_bind (tz, &td->trip, cdev, &c))continue ; - ret =thermal_bind_cdev_to_trip (tz, trip, cdev, &c); + ret =thermal_bind_cdev_to_trip (tz, td, cdev, &c);if (ret) -print_bind_err_msg (tz, trip, cdev, ret); +print_bind_err_msg (tz, td, cdev, ret); }mutex_unlock (&tz->lock);
@@ -1270,7 +1267,7 @@ static void thermal_zone_cdev_unbind(strmutex_lock (&tz->lock);for_each_trip_desc (tz, td) -thermal_unbind_cdev_from_trip (tz, &td->trip, cdev); +thermal_unbind_cdev_from_trip (tz, td, cdev);mutex_unlock (&tz->lock); }
PATCH 1/8 thermal: core: Move lists of thermal instances to trip descriptors
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
In almost all places where a thermal zone's list of thermal instances is walked, there is a check to match a specific trip point and it is walked in vain whenever there are no cooling devices associated with the given trip.
To address this, store the lists of thermal instances in trip point descriptors instead of storing them in thermal zones and adjust all code using those lists accordingly.
No intentional functional impact.
--- drivers/thermal/gov_bang_bang.c | 11 ++++----- drivers/thermal/gov_fair_share.c | 16 ++++--------- drivers/thermal/gov_power_allocator.c | 41 ++++++++++++++-------------------- drivers/thermal/gov_step_wise.c | 16 ++++++------- drivers/thermal/thermal_core.c | 33 +++++++++++++++------------ drivers/thermal/thermal_core.h | 5 +--- drivers/thermal/thermal_helpers.c | 5 ++-- 7 files changed, 60 insertions(+), 67 deletions(-)
drivers/thermal/thermal_core.c +19 -14
@@ -488,7 +488,7 @@ static void thermal_zone_device_check(ststatic void thermal_zone_device_init (struct thermal_zone_device *tz) { -struct thermal_instance *pos; +struct thermal_trip_desc *td;INIT_DELAYED_WORK (&tz->poll_queue, thermal_zone_device_check);
@@ -496,8 +496,12 @@ static void thermal_zone_device_init(str tz->passive =0 ; tz->prev_low_trip = -INT_MAX ; tz->prev_high_trip =INT_MAX ; -list_for_each_entry (pos, &tz->thermal_instances, tz_node) - pos->initialized = false; +for_each_trip_desc (tz, td) { +struct thermal_instance *instance; + +list_for_each_entry (instance, &td->thermal_instances, trip_node) + instance->initialized = false; + } } static void thermal_governor_trip_crossed(struct thermal_governor *governor,
@@ -761,12 +765,12 @@ struct thermal_zone_device *thermal_zone* Return: 0 on success, the proper error value otherwise. */ static int thermal_bind_cdev_to_trip (struct thermal_zone_device *tz, -const struct thermal_trip *trip, +struct thermal_trip *trip,struct thermal_cooling_device *cdev,struct cooling_spec *cool_spec) { -struct thermal_instance *dev; -struct thermal_instance *pos; +struct thermal_trip_desc *td =trip_to_trip_desc (trip); +struct thermal_instance *dev, *instance;bool upper_no_limit;int result;
@@ -829,13 +833,13 @@ static int thermal_bind_cdev_to_trip(str goto remove_trip_file;mutex_lock (&cdev->lock); -list_for_each_entry (pos, &tz->thermal_instances, tz_node) -if (pos->trip == trip && pos->cdev == cdev) { +list_for_each_entry (instance, &td->thermal_instances, trip_node) +if (instance->cdev == cdev) { result = -EEXIST ;break ; }if (!result) { -list_add_tail (&dev->tz_node, &tz->thermal_instances); +list_add_tail (&dev->trip_node, &td->thermal_instances);list_add_tail (&dev->cdev_node, &cdev->thermal_instances);atomic_set (&tz->need_update,1 );
@@ -869,15 +873,16 @@ free_mem:* This function is usually called in the thermal zone device .unbind callback. */ static void thermal_unbind_cdev_from_trip (struct thermal_zone_device *tz, -const struct thermal_trip *trip, +struct thermal_trip *trip,struct thermal_cooling_device *cdev) { +struct thermal_trip_desc *td =trip_to_trip_desc (trip);struct thermal_instance *pos, *next;mutex_lock (&cdev->lock); -list_for_each_entry_safe (pos, next, &tz->thermal_instances, tz_node) { -if (pos->trip == trip && pos->cdev == cdev) { -list_del (&pos->tz_node); +list_for_each_entry_safe (pos, next, &td->thermal_instances, trip_node) { +if (pos->cdev == cdev) { +list_del (&pos->trip_node);list_del (&pos->cdev_node);thermal_governor_update_tz (tz,THERMAL_TZ_UNBIND_CDEV );
@@ -1414,7 +1419,6 @@ thermal_zone_device_register_with_trips( } } -INIT_LIST_HEAD (&tz->thermal_instances);INIT_LIST_HEAD (&tz->node);ida_init (&tz->ida);mutex_init (&tz->lock);
@@ -1438,6 +1442,7 @@ thermal_zone_device_register_with_trips( tz->num_trips = num_trips;for_each_trip_desc (tz, td) { td->trip = *trip++; +INIT_LIST_HEAD (&td->thermal_instances);/* * Mark all thresholds as invalid to start with even though * this only matters for the trips that start as invalid and
drivers/thermal/thermal_core.h +2 -3
@@ -30,6 +30,7 @@ struct thermal_trip_desc {struct thermal_trip trip;struct thermal_trip_attrs trip_attrs;struct list_head notify_list_node; +struct list_head thermal_instances;int notify_temp;int threshold; };
@@ -93,7 +94,6 @@ struct thermal_governor {* @tzp: thermal zone parameters * @governor: pointer to the governor for this thermal zone * @governor_data: private pointer for governor data -* @thermal_instances: list of &struct thermal_instance of this thermal zone * @ida: &struct ida to generate unique id for this zone's cooling * devices * @lock: lock to protect thermal_instances list
@@ -128,7 +128,6 @@ struct thermal_zone_device {struct thermal_zone_params *tzp;struct thermal_governor *governor;void *governor_data; -struct list_head thermal_instances;struct ida ida;struct mutex lock;struct list_head node;
@@ -223,7 +222,7 @@ struct thermal_instance {struct device_attribute attr;char weight_attr_name[THERMAL_NAME_LENGTH ];struct device_attribute weight_attr; -struct list_head tz_node;/* node in tz->thermal_instances */ +struct list_head trip_node;/* node in trip->thermal_instances */ struct list_head cdev_node;/* node in cdev->thermal_instances */ unsigned int weight;/* The weight of the cooling device */ bool upper_no_limit;
drivers/thermal/gov_bang_bang.c +5 -6
@@ -67,6 +67,7 @@ static void bang_bang_control(struct the const struct thermal_trip *trip, bool crossed_up) { +const struct thermal_trip_desc *td =trip_to_trip_desc (trip);struct thermal_instance *instance;lockdep_assert_held (&tz->lock);
@@ -75,10 +76,8 @@ static void bang_bang_control(struct thethermal_zone_trip_id (tz, trip), trip->temperature, tz->temperature, trip->hysteresis); -list_for_each_entry (instance, &tz->thermal_instances, tz_node) { -if (instance->trip == trip) -bang_bang_set_instance_target (instance, crossed_up); - } +list_for_each_entry (instance, &td->thermal_instances, trip_node) +bang_bang_set_instance_target (instance, crossed_up); }static void bang_bang_manage (struct thermal_zone_device *tz)
@@ -104,8 +103,8 @@ static void bang_bang_manage(struct ther* to the thermal zone temperature and the trip point threshold. */ turn_on = tz->temperature >= td->threshold; -list_for_each_entry (instance, &tz->thermal_instances, tz_node) { -if (!instance->initialized && instance->trip == trip) +list_for_each_entry (instance, &td->thermal_instances, trip_node) { +if (!instance->initialized)bang_bang_set_instance_target (instance, turn_on); } }
drivers/thermal/gov_fair_share.c +5 -11
@@ -44,7 +44,7 @@ static int get_trip_level(struct thermal/** * fair_share_throttle - throttles devices associated with the given zone * @tz: thermal_zone_device -* @trip: trip point +* @td: trip point descriptor * @trip_level: number of trips crossed by the zone temperature * * Throttling Logic: This uses three parameters to calculate the new
@@ -61,29 +61,23 @@ static int get_trip_level(struct thermal* new_state of cooling device = P3 * P2 * P1 */ static void fair_share_throttle (struct thermal_zone_device *tz, -const struct thermal_trip *trip, +const struct thermal_trip_desc *td,int trip_level) {struct thermal_instance *instance;int total_weight =0 ;int nr_instances =0 ; -list_for_each_entry (instance, &tz->thermal_instances, tz_node) { -if (instance->trip != trip) -continue ; - +list_for_each_entry (instance, &td->thermal_instances, trip_node) { total_weight += instance->weight; nr_instances++; } -list_for_each_entry (instance, &tz->thermal_instances, tz_node) { +list_for_each_entry (instance, &td->thermal_instances, trip_node) {struct thermal_cooling_device *cdev = instance->cdev;u64 dividend;u32 divisor; -if (instance->trip != trip) -continue ; - dividend = trip_level; dividend *= cdev->max_state; divisor = tz->num_trips;
@@ -116,7 +110,7 @@ static void fair_share_manage(struct the trip->type == THERMAL_TRIP_HOT)continue ; -fair_share_throttle (tz, trip, trip_level); +fair_share_throttle (tz, td, trip_level); } }
drivers/thermal/gov_power_allocator.c +18 -23
117 lines not on this page · raw message
drivers/thermal/gov_step_wise.c +8 -8
@@ -66,9 +66,10 @@ static unsigned long get_target_state(st }static void thermal_zone_trip_update (struct thermal_zone_device *tz, -const struct thermal_trip *trip, +const struct thermal_trip_desc *td,int trip_threshold) { +const struct thermal_trip *trip = &td->trip;enum thermal_trend trend =get_tz_trend (tz, trip);int trip_id =thermal_zone_trip_id (tz, trip);struct thermal_instance *instance;
@@ -82,12 +83,9 @@ static void thermal_zone_trip_update(strdev_dbg (&tz->device,"Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n" , trip_id, trip->type, trip_threshold, trend, throttle); -list_for_each_entry (instance, &tz->thermal_instances, tz_node) { +list_for_each_entry (instance, &td->thermal_instances, trip_node) {int old_target; -if (instance->trip != trip) -continue ; - old_target = instance->target; instance->target =get_target_state (instance, trend, throttle);
@@ -127,11 +125,13 @@ static void step_wise_manage(struct ther trip->type == THERMAL_TRIP_HOT)continue ; -thermal_zone_trip_update (tz, trip, td->threshold); +thermal_zone_trip_update (tz, td, td->threshold); } -list_for_each_entry (instance, &tz->thermal_instances, tz_node) -thermal_cdev_update (instance->cdev); +for_each_trip_desc (tz, td) { +list_for_each_entry (instance, &td->thermal_instances, trip_node) +thermal_cdev_update (instance->cdev); + } }static struct thermal_governor thermal_gov_step_wise = {
drivers/thermal/thermal_helpers.c +3 -2
@@ -43,10 +43,11 @@ static bool thermal_instance_present(strstruct thermal_cooling_device *cdev, const struct thermal_trip *trip) { + const struct thermal_trip_desc *td = trip_to_trip_desc(trip);struct thermal_instance *ti; -list_for_each_entry (ti, &tz->thermal_instances, tz_node) { -if (ti->trip == trip && ti->cdev == cdev) +list_for_each_entry (ti, &td->thermal_instances, trip_node) { +if (ti->cdev == cdev)return true; }
cover thermal: core: Updates related to thermal instances, trip points handling and locking
Hi Everyone,
This is a collection of thermal core updates for the 6.13 cycle that have been present in my bleeding-edge branch for some time (they are also present in my thermal-core-experimental branch). They are based on linux-pm.git/thermal.
Some of them touch the same pieces of code as
https://lore.kernel.org/linux-pm/[email protected]/
but these are complementary changes rather than real conflicts so adjusting them to apply along with the above should not be a problem.
The first two patches move the lists of thermal instances from thermal zone device objects to trip point descriptors, which allows some code to become more straightforward, and clean up code on top of that.
The next five patches rearrange the handling of trip points while updating thermal zone temperature to use sorted lists of trip points, which allows the total amount of computations related to this to be reduced, at least on average.
The last patch simplifies the thermal zone locking with the help of a guard.
Please refer to the individual patch changelogs for details.
Thanks!
PATCH 7/8 thermal: core: Use trip lists for trip crossing detection
From: Rafael J. Wysocki <[email protected]>
Modify the thermal core to use three lists of trip points:
trips_above containing trips with thresholds strictly above the current
thermal zone temperature,
trips_below containing trips with thresholds at or below the current
zone temperature,
trips_invalid containing trips with temperature equal to
THERMAL_ZONE_INVALID,
where the first two lists are always sorted.
For each trip in trips_above, there is no mitigation under way and the trip threshold is equal to its temperature. In turn, for each trip in trips_below, there is mitigation under way and the trip threshold is equal to its low temperature. The trips in trips_invalid, of course, need not be taken into consideration.
The idea is to make __thermal_zone_device_update() walk trips_above and trips_below instead of walking the entire table of trip points in the thermal zone. Usually, it will only need to walk a few entries in one of the lists and check one entry in the other one, depending on the direction of the zone temperature changes, because crossing many trips by the zone temperature in one go between two consecutive temperature checks should be unlikely (if this happens, the thermal zone temperature is checked too rarely or not at the right time).
Moreover, because the lists are sorted, the low and high values needed by thermal_zone_set_trips() can be determined in a straightforward way by looking at one end of each list.
Of course, additional work is needed in some places in order to maintain the ordering of the lists, but it happens in situations that should be rare, like updating a trip point temperature or hysteresis, at the thermal zone initialization time or during system resume.
Going forward, the trips_below and trips_above lists can be used by thermal governors to reduce overhead.
--- drivers/thermal/thermal_core.c | 150 ++++++++++++++++++++--------------------- drivers/thermal/thermal_core.h | 7 + drivers/thermal/thermal_trip.c | 69 +++++++++++++----- 3 files changed, 132 insertions(+), 94 deletions(-)
drivers/thermal/thermal_core.c +75 -75
@@ -420,64 +420,6 @@ static void handle_critical_trips(struct tz->ops.hot (tz); } -static void handle_thermal_trip (struct thermal_zone_device *tz, -struct thermal_trip_desc *td, -struct list_head *way_up_list, -struct list_head *way_down_list) -{ -const struct thermal_trip *trip = &td->trip; -int old_threshold; - -if (trip->temperature ==THERMAL_TEMP_INVALID ) -return ; - -/* -* If the trip temperature or hysteresis has been updated recently, -* the threshold needs to be computed again using the new values. -* However, its initial value still reflects the old ones and that -* is what needs to be compared with the previous zone temperature -* to decide which action to take. -*/ - old_threshold = td->threshold; - td->threshold = trip->temperature; - -if (tz->last_temperature >= old_threshold && - tz->last_temperature !=THERMAL_TEMP_INIT ) { -/* -* Mitigation is under way, so it needs to stop if the zone -* temperature falls below the low temperature of the trip. -* In that case, the trip temperature becomes the new threshold. -*/ -if (tz->temperature < trip->temperature - trip->hysteresis) { - td->notify_temp = trip->temperature - trip->hysteresis; -thermal_trip_move_to_sorted_list (td, way_down_list); - -if (trip->type ==THERMAL_TRIP_PASSIVE ) { - tz->passive--; -WARN_ON (tz->passive <0 ); - } - }else { - td->threshold -= trip->hysteresis; - } - }else if (tz->temperature >= trip->temperature) { -/* -* There is no mitigation under way, so it needs to be started -* if the zone temperature exceeds the trip one. The new -* threshold is then set to the low temperature of the trip. -*/ - td->notify_temp = trip->temperature; -thermal_trip_move_to_sorted_list (td, way_up_list); - - td->threshold -= trip->hysteresis; - -if (trip->type ==THERMAL_TRIP_PASSIVE ) - tz->passive++; -else if (trip->type ==THERMAL_TRIP_CRITICAL || - trip->type ==THERMAL_TRIP_HOT ) -handle_critical_trips (tz, trip); - } -} -static void thermal_zone_device_check (struct work_struct *work) { struct thermal_zone_device *tz = container_of(work, struct
@@ -486,9 +428,16 @@ static void thermal_zone_device_check(stthermal_zone_device_update (tz,THERMAL_EVENT_UNSPECIFIED ); } +static void move_trip_to_trips_above (struct thermal_trip_desc *td, +struct thermal_zone_device *tz) +{ + td->threshold = td->trip.temperature; +thermal_trip_move_to_sorted_list (td, &tz->trips_above); +} +static void thermal_zone_device_init (struct thermal_zone_device *tz) { -struct thermal_trip_desc *td; +struct thermal_trip_desc *td, *next;INIT_DELAYED_WORK (&tz->poll_queue, thermal_zone_device_check);
@@ -502,6 +451,16 @@ static void thermal_zone_device_init(strlist_for_each_entry (instance, &td->thermal_instances, trip_node) instance->initialized = false; } +list_for_each_entry_safe (td, next, &tz->trips_invalid, list_node) { +if (td->trip.temperature !=THERMAL_TEMP_INVALID ) +move_trip_to_trips_above (td, tz); + } +list_for_each_entry_safe (td, next, &tz->trips_below, list_node) { +if (td->trip.temperature ==THERMAL_TEMP_INVALID ) +list_move (&td->list_node, &tz->trips_invalid); +else +move_trip_to_trips_above (td, tz); + } } static void thermal_governor_trip_crossed(struct thermal_governor *governor,
@@ -569,27 +528,71 @@ void __thermal_zone_device_update(struct tz->notify_event = event; -for_each_trip_desc (tz, td) { -handle_thermal_trip (tz, td, &way_up_list, &way_down_list); +/* Check trips that were previously above the zone temperature. */ +list_for_each_entry_safe (td, next, &tz->trips_above, list_node) { +struct thermal_trip *trip = &td->trip; -if (td->threshold <= tz->temperature && td->threshold > low) - low = td->threshold; +if (td->threshold > tz->temperature) +break ; + +/* +* There is no mitigation under way, so it needs to be started. +* Move the trip to the "crossed on the way up" list (sorted by +* the old threshold) and set the new threshold to the low +* temperature of the trip. +*/ +thermal_trip_move_to_sorted_list (td, &way_up_list); + td->threshold = trip->temperature - trip->hysteresis; -if (td->threshold >= tz->temperature && td->threshold < high) - high = td->threshold; +if (trip->type ==THERMAL_TRIP_PASSIVE ) + tz->passive++; +else if (trip->type ==THERMAL_TRIP_CRITICAL || + trip->type ==THERMAL_TRIP_HOT ) +handle_critical_trips (tz, trip); } +/* Check trips that were previously below or at the zone temperature. */ +list_for_each_entry_safe_reverse (td, next, &tz->trips_below, list_node) { +struct thermal_trip *trip = &td->trip; -thermal_zone_set_trips (tz, low, high); +if (td->threshold <= tz->temperature) +break ; + +/* +* Mitigation is under way, so it needs to stop. Move the trip +* to the "crossed on the way down" list (sorted by the old +* threshold) and set the new threshold to the trip temperature. +*/ +thermal_trip_move_to_sorted_list (td, &way_down_list); + td->threshold = trip->temperature; + +if (trip->type ==THERMAL_TRIP_PASSIVE ) { + tz->passive--; +WARN_ON (tz->passive <0 ); + } + }list_for_each_entry_safe (td, next, &way_up_list, list_node) {thermal_trip_crossed (tz, &td->trip, governor, true); -list_del_init (&td->list_node); +thermal_trip_move_to_sorted_list (td, &tz->trips_below); }list_for_each_entry_safe_reverse (td, next, &way_down_list, list_node) {thermal_trip_crossed (tz, &td->trip, governor, false); -list_del_init (&td->list_node); +thermal_trip_move_to_sorted_list (td, &tz->trips_above); + } + +if (!list_empty (&tz->trips_below)) { + td = list_last_entry(&tz->trips_below, struct thermal_trip_desc, + list_node); +/* Avoid checks that don't correspond to trip crossing. */ + low = td->threshold -1 ; + } +if (!list_empty (&tz->trips_above)) { + td = list_first_entry(&tz->trips_above, struct thermal_trip_desc, + list_node); + high = td->threshold; } +thermal_zone_set_trips (tz, low, high);if (governor->manage) governor->manage (tz);
@@ -1409,6 +1412,9 @@ thermal_zone_device_register_with_trips( }INIT_LIST_HEAD (&tz->node); +INIT_LIST_HEAD (&tz->trips_above); +INIT_LIST_HEAD (&tz->trips_below); +INIT_LIST_HEAD (&tz->trips_invalid);ida_init (&tz->ida);mutex_init (&tz->lock);init_completion (&tz->removal);
@@ -1432,13 +1438,7 @@ thermal_zone_device_register_with_trips(for_each_trip_desc (tz, td) { td->trip = *trip++;INIT_LIST_HEAD (&td->thermal_instances); -INIT_LIST_HEAD (&td->list_node); -/* -* Mark all thresholds as invalid to start with even though -* this only matters for the trips that start as invalid and -* become valid later. -*/ - td->threshold =INT_MAX ; +list_add (&td->list_node, &tz->trips_invalid); } tz->polling_delay_jiffies =msecs_to_jiffies (polling_delay);
drivers/thermal/thermal_core.h +6 -1
@@ -31,7 +31,6 @@ struct thermal_trip_desc {struct thermal_trip_attrs trip_attrs;struct list_head list_node;struct list_head thermal_instances; -int notify_temp;int threshold; };
@@ -69,6 +68,9 @@ struct thermal_governor {* @device: &struct device for this thermal zone * @removal: removal completion * @resume: resume completion +* @trips_above: trips above the current zone temperature +* @trips_below: trips equal to or below the current zone temperature +* @trips_invalid: trips with invalid temperature * @mode: current mode of this thermal zone * @devdata: private pointer for device private data * @num_trips: number of trip points the thermal zone supports
@@ -111,6 +113,9 @@ struct thermal_zone_device {struct completion removal;struct completion resume;struct attribute_group trips_attribute_group; +struct list_head trips_above; +struct list_head trips_below; +struct list_head trips_invalid;enum thermal_device_mode mode;void *devdata;int num_trips;
drivers/thermal/thermal_trip.c +51 -18
97 lines not on this page · raw message
PATCH 8/8 thermal: core: Add and use thermal zone guard
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Add and use a special guard for thermal zones.
This allows quite a few error code paths to be simplified among other things and brings in a noticeable code size reduction for a good measure.
No intentional functional impact.
--- drivers/thermal/thermal_core.c | 67 +++++++++++++------------------ drivers/thermal/thermal_core.h | 4 + drivers/thermal/thermal_helpers.c | 17 ++----- drivers/thermal/thermal_sysfs.c | 81 ++++++++++++++++---------------------- drivers/thermal/thermal_trip.c | 8 --- 5 files changed, 76 insertions(+), 101 deletions(-)
drivers/thermal/thermal_core.h +4 -0
@@ -9,6 +9,7 @@#ifndef __THERMAL_CORE_H__#define __THERMAL_CORE_H__ +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/thermal.h>
@@ -146,6 +147,9 @@ struct thermal_zone_device { struct thermal_trip_desc trips[] __counted_by(num_trips); }; +DEFINE_GUARD(thermal_zone, struct thermal_zone_device *, mutex_lock(&_T->lock), +mutex_unlock (&_T->lock)) + /* Initial thermal zone temperature. */#define THERMAL_TEMP_INIT INT_MIN
drivers/thermal/thermal_sysfs.c +35 -46
162 lines not on this page · raw message
drivers/thermal/thermal_trip.c +2 -6
@@ -45,13 +45,9 @@ int thermal_zone_for_each_trip(struct thint (*cb)(struct thermal_trip *,void *), void *data) { - int ret; +guard (thermal_zone )(tz ); -mutex_lock (&tz->lock); - ret =for_each_thermal_trip (tz, cb, data); -mutex_unlock (&tz->lock); - -return ret; +return for_each_thermal_trip (tz, cb, data); }EXPORT_SYMBOL_GPL (thermal_zone_for_each_trip);
drivers/thermal/thermal_helpers.c +6 -11
@@ -60,13 +60,13 @@ bool thermal_trip_is_bound_to_cdev(struc {bool ret; -mutex_lock (&tz->lock); +guard (thermal_zone)(tz); +mutex_lock (&cdev->lock); ret =thermal_instance_present (tz, cdev, trip);mutex_unlock (&cdev->lock); -mutex_unlock (&tz->lock);return ret; }
@@ -138,19 +138,14 @@ int thermal_zone_get_temp(struct thermalif (IS_ERR_OR_NULL (tz))return -EINVAL ; -mutex_lock (&tz->lock); +guard (thermal_zone)(tz); -if (!tz->ops.get_temp) { - ret = -EINVAL ; - goto unlock; - } +if (!tz->ops.get_temp) +return -EINVAL ; ret =__thermal_zone_get_temp (tz, temp);if (!ret && *temp <=THERMAL_TEMP_INVALID ) - ret = -ENODATA ; - -unlock: -mutex_unlock (&tz->lock); +return -ENODATA ;return ret; }
drivers/thermal/thermal_core.c +29 -38
166 lines not on this page · raw message
PATCH 6/8 thermal: core: Add function for moving trips to sorted lists
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Subsequently, trips will be moved between sorted lists in multiple places, so replace add_trip_to_sorted_list() with an analogous function that will move a given trip to a given sorted list.
Call the new function thermal_trip_move_to_sorted_list() and put it into the trips-related part of the code.
To allow list_move() used in the new function to work, initialize the list_node fields of trip descriptors so they are always valid.
No intentional functional impact.
--- drivers/thermal/thermal_core.c | 30 ++++++++++-------------------- drivers/thermal/thermal_core.h | 2 ++ drivers/thermal/thermal_trip.c | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 20 deletions(-)
drivers/thermal/thermal_core.c +10 -20
@@ -420,21 +420,6 @@ static void handle_critical_trips(struct tz->ops.hot (tz); } -static void add_trip_to_sorted_list (struct thermal_trip_desc *td, -struct list_head *list) -{ -struct thermal_trip_desc *entry; - -/* Assume that the new entry is likely to be the last one. */ -list_for_each_entry_reverse (entry, list, list_node) { -if (entry->notify_temp <= td->notify_temp) { -list_add (&td->list_node, &entry->list_node); -return ; - } - } -list_add (&td->list_node, list); -} - static void handle_thermal_trip(struct thermal_zone_device *tz, struct thermal_trip_desc *td, struct list_head *way_up_list,
@@ -465,7 +450,7 @@ static void handle_thermal_trip(struct t*/ if (tz->temperature < trip->temperature - trip->hysteresis) { td->notify_temp = trip->temperature - trip->hysteresis; -add_trip_to_sorted_list (td, way_down_list); +thermal_trip_move_to_sorted_list (td, way_down_list);if (trip->type ==THERMAL_TRIP_PASSIVE ) { tz->passive--;
@@ -481,7 +466,7 @@ static void handle_thermal_trip(struct t* threshold is then set to the low temperature of the trip. */ td->notify_temp = trip->temperature; -add_trip_to_sorted_list (td, way_up_list); +thermal_trip_move_to_sorted_list (td, way_up_list); td->threshold -= trip->hysteresis;
@@ -550,7 +535,7 @@ void __thermal_zone_device_update(struct enum thermal_notify_event event) {struct thermal_governor *governor =thermal_get_tz_governor (tz); -struct thermal_trip_desc *td; +struct thermal_trip_desc *td, *next;LIST_HEAD (way_down_list);LIST_HEAD (way_up_list);int low = -INT_MAX , high =INT_MAX ;
@@ -596,11 +581,15 @@ void __thermal_zone_device_update(structthermal_zone_set_trips (tz, low, high); -list_for_each_entry (td, &way_up_list, list_node) +list_for_each_entry_safe (td, next, &way_up_list, list_node) {thermal_trip_crossed (tz, &td->trip, governor, true); +list_del_init (&td->list_node); + } -list_for_each_entry_reverse (td, &way_down_list, list_node) +list_for_each_entry_safe_reverse (td, next, &way_down_list, list_node) {thermal_trip_crossed (tz, &td->trip, governor, false); +list_del_init (&td->list_node); + }if (governor->manage) governor->manage (tz);
@@ -1443,6 +1432,7 @@ thermal_zone_device_register_with_trips(for_each_trip_desc (tz, td) { td->trip = *trip++;INIT_LIST_HEAD (&td->thermal_instances); +INIT_LIST_HEAD (&td->list_node);/* * Mark all thresholds as invalid to start with even though * this only matters for the trips that start as invalid and
drivers/thermal/thermal_core.h +2 -0
@@ -256,6 +256,8 @@ const char *thermal_trip_type_name(enumvoid thermal_zone_set_trips (struct thermal_zone_device *tz,int low,int high);int thermal_zone_trip_id (const struct thermal_zone_device *tz,const struct thermal_trip *trip); +void thermal_trip_move_to_sorted_list (struct thermal_trip_desc *td, +struct list_head *list);int __thermal_zone_get_temp (struct thermal_zone_device *tz,int *temp);void thermal_zone_trip_down (struct thermal_zone_device *tz,const struct thermal_trip *trip);
drivers/thermal/thermal_trip.c +15 -0
@@ -93,6 +93,21 @@ int thermal_zone_trip_id(const struct threturn trip_to_trip_desc (trip) - tz->trips; } +void thermal_trip_move_to_sorted_list (struct thermal_trip_desc *td, +struct list_head *list) +{ +struct thermal_trip_desc *entry; + +/* Assume that the new entry is likely to be the last one. */ +list_for_each_entry_reverse (entry, list, list_node) { +if (entry->notify_temp <= td->notify_temp) { +list_move (&td->list_node, &entry->list_node); +return ; + } + } +list_move (&td->list_node, list); +} +void thermal_zone_set_trip_hyst (struct thermal_zone_device *tz,struct thermal_trip *trip,int hyst) {
PATCH 5/8 thermal: core: Rename trip list node in struct thermal_trip_desc
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Since the list node field in struct thermal_trip_desc is going to be used for purposes other than trip crossing notification, rename it to list_node.
No intentional functional impact.
--- drivers/thermal/thermal_core.c | 10 +++++----- drivers/thermal/thermal_core.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/thermal/thermal_core.c +5 -5
@@ -426,13 +426,13 @@ static void add_trip_to_sorted_list(strustruct thermal_trip_desc *entry;/* Assume that the new entry is likely to be the last one. */ -list_for_each_entry_reverse (entry, list, notify_list_node) { +list_for_each_entry_reverse (entry, list, list_node) {if (entry->notify_temp <= td->notify_temp) { -list_add (&td->notify_list_node, &entry->notify_list_node); +list_add (&td->list_node, &entry->list_node);return ; } } -list_add (&td->notify_list_node, list); +list_add (&td->list_node, list); } static void handle_thermal_trip(struct thermal_zone_device *tz,
@@ -596,10 +596,10 @@ void __thermal_zone_device_update(structthermal_zone_set_trips (tz, low, high); -list_for_each_entry (td, &way_up_list, notify_list_node) +list_for_each_entry (td, &way_up_list, list_node)thermal_trip_crossed (tz, &td->trip, governor, true); -list_for_each_entry_reverse (td, &way_down_list, notify_list_node) +list_for_each_entry_reverse (td, &way_down_list, list_node)thermal_trip_crossed (tz, &td->trip, governor, false);if (governor->manage)
drivers/thermal/thermal_core.h +1 -1
@@ -29,7 +29,7 @@ struct thermal_trip_attrs {struct thermal_trip_desc {struct thermal_trip trip;struct thermal_trip_attrs trip_attrs; -struct list_head notify_list_node; +struct list_head list_node;struct list_head thermal_instances;int notify_temp;int threshold;