Skip to content

Commit ef1d87e

Browse files
Keerthyjzhang-rui
authored andcommitted
thermal: core: Add a back up thermal shutdown mechanism
orderly_poweroff is triggered when a graceful shutdown of system is desired. This may be used in many critical states of the kernel such as when subsystems detects conditions such as critical temperature conditions. However, in certain conditions in system boot up sequences like those in the middle of driver probes being initiated, userspace will be unable to power off the system in a clean manner and leaves the system in a critical state. In cases like these, the /sbin/poweroff will return success (having forked off to attempt powering off the system. However, the system overall will fail to completely poweroff (since other modules will be probed) and the system is still functional with no userspace (since that would have shut itself off). However, there is no clean way of detecting such failure of userspace powering off the system. In such scenarios, it is necessary for a backup workqueue to be able to force a shutdown of the system when orderly shutdown is not successful after a configurable time period. Reported-by: Nishanth Menon <nm@ti.com> Signed-off-by: Keerthy <j-keerthy@ti.com> Acked-by: Eduardo Valentin <edubezval@gmail.com> Signed-off-by: Zhang Rui <rui.zhang@intel.com>
1 parent e441fd6 commit ef1d87e

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

Documentation/thermal/sysfs-api.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,24 @@ platform data is provided, this uses the step_wise throttling policy.
582582
This function serves as an arbitrator to set the state of a cooling
583583
device. It sets the cooling device to the deepest cooling state if
584584
possible.
585+
586+
6. thermal_emergency_poweroff:
587+
588+
On an event of critical trip temperature crossing. Thermal framework
589+
allows the system to shutdown gracefully by calling orderly_poweroff().
590+
In the event of a failure of orderly_poweroff() to shut down the system
591+
we are in danger of keeping the system alive at undesirably high
592+
temperatures. To mitigate this high risk scenario we program a work
593+
queue to fire after a pre-determined number of seconds to start
594+
an emergency shutdown of the device using the kernel_power_off()
595+
function. In case kernel_power_off() fails then finally
596+
emergency_restart() is called in the worst case.
597+
598+
The delay should be carefully profiled so as to give adequate time for
599+
orderly_poweroff(). In case of failure of an orderly_poweroff() the
600+
emergency poweroff kicks in after the delay has elapsed and shuts down
601+
the system.
602+
603+
If set to 0 emergency poweroff will not be supported. So a carefully
604+
profiled non-zero positive value is a must for emergerncy poweroff to be
605+
triggered.

drivers/thermal/Kconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ menuconfig THERMAL
1515

1616
if THERMAL
1717

18+
config THERMAL_EMERGENCY_POWEROFF_DELAY_MS
19+
int "Emergency poweroff delay in milli-seconds"
20+
depends on THERMAL
21+
default 0
22+
help
23+
Thermal subsystem will issue a graceful shutdown when
24+
critical temperatures are reached using orderly_poweroff(). In
25+
case of failure of an orderly_poweroff(), the thermal emergency
26+
poweroff kicks in after a delay has elapsed and shuts down the system.
27+
This config is number of milliseconds to delay before emergency
28+
poweroff kicks in. Similarly to the critical trip point,
29+
the delay should be carefully profiled so as to give adequate
30+
time for orderly_poweroff() to finish on regular execution.
31+
If set to 0 emergency poweroff will not be supported.
32+
33+
In doubt, leave as 0.
34+
1835
config THERMAL_HWMON
1936
bool
2037
prompt "Expose thermal sensors as hwmon device"

drivers/thermal/thermal_core.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,54 @@ static void handle_non_critical_trips(struct thermal_zone_device *tz,
324324
def_governor->throttle(tz, trip);
325325
}
326326

327+
/**
328+
* thermal_emergency_poweroff_func - emergency poweroff work after a known delay
329+
* @work: work_struct associated with the emergency poweroff function
330+
*
331+
* This function is called in very critical situations to force
332+
* a kernel poweroff after a configurable timeout value.
333+
*/
334+
static void thermal_emergency_poweroff_func(struct work_struct *work)
335+
{
336+
/*
337+
* We have reached here after the emergency thermal shutdown
338+
* Waiting period has expired. This means orderly_poweroff has
339+
* not been able to shut off the system for some reason.
340+
* Try to shut down the system immediately using kernel_power_off
341+
* if populated
342+
*/
343+
WARN(1, "Attempting kernel_power_off: Temperature too high\n");
344+
kernel_power_off();
345+
346+
/*
347+
* Worst of the worst case trigger emergency restart
348+
*/
349+
WARN(1, "Attempting emergency_restart: Temperature too high\n");
350+
emergency_restart();
351+
}
352+
353+
static DECLARE_DELAYED_WORK(thermal_emergency_poweroff_work,
354+
thermal_emergency_poweroff_func);
355+
356+
/**
357+
* thermal_emergency_poweroff - Trigger an emergency system poweroff
358+
*
359+
* This may be called from any critical situation to trigger a system shutdown
360+
* after a known period of time. By default this is not scheduled.
361+
*/
362+
void thermal_emergency_poweroff(void)
363+
{
364+
int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
365+
/*
366+
* poweroff_delay_ms must be a carefully profiled positive value.
367+
* Its a must for thermal_emergency_poweroff_work to be scheduled
368+
*/
369+
if (poweroff_delay_ms <= 0)
370+
return;
371+
schedule_delayed_work(&thermal_emergency_poweroff_work,
372+
msecs_to_jiffies(poweroff_delay_ms));
373+
}
374+
327375
static void handle_critical_trips(struct thermal_zone_device *tz,
328376
int trip, enum thermal_trip_type trip_type)
329377
{
@@ -346,6 +394,11 @@ static void handle_critical_trips(struct thermal_zone_device *tz,
346394
tz->temperature / 1000);
347395
mutex_lock(&poweroff_lock);
348396
if (!power_off_triggered) {
397+
/*
398+
* Queue a backup emergency shutdown in the event of
399+
* orderly_poweroff failure
400+
*/
401+
thermal_emergency_poweroff();
349402
orderly_poweroff(true);
350403
power_off_triggered = true;
351404
}

0 commit comments

Comments
 (0)