Skip to content

Commit dfa19b1

Browse files
M-Vaittinenbroonie
authored andcommitted
reboot: Add hardware protection power-off
There can be few cases when we need to shut-down the system in order to protect the hardware. Currently this is done at least by the thermal core when temperature raises over certain limit. Some PMICs can also generate interrupts for example for over-current or over-voltage, voltage drops, short-circuit, ... etc. On some systems these are a sign of hardware failure and only thing to do is try to protect the rest of the hardware by shutting down the system. Add shut-down logic which can be used by all subsystems instead of implementing the shutdown in each subsystem. The logic is stolen from thermal_core with difference of using atomic_t instead of a mutex in order to allow calls directly from IRQ context and changing the WARN() to pr_emerg() as discussed here: https://lore.kernel.org/lkml/YJuPwAZroVZ%2Fw633@alley/ and here: https://lore.kernel.org/linux-iommu/20210331093104.383705-4-geert+renesas@glider.be/ Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com> Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org> Link: https://lore.kernel.org/r/e83ec1ca9408f90c857ea9dcdc57b14d9037b03f.1622628333.git.matti.vaittinen@fi.rohmeurope.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 673e851 commit dfa19b1

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎include/linux/reboot.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ extern char poweroff_cmd[POWEROFF_CMD_PATH_LEN];
7979

8080
extern void orderly_poweroff(bool force);
8181
extern void orderly_reboot(void);
82+
void hw_protection_shutdown(const char *reason, int ms_until_forced);
8283

8384
/*
8485
* Emergency restart, callable from an interrupt handler.

‎kernel/reboot.c‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#define pr_fmt(fmt) "reboot: " fmt
99

10+
#include <linux/atomic.h>
1011
#include <linux/ctype.h>
1112
#include <linux/export.h>
1213
#include <linux/kexec.h>
@@ -518,6 +519,84 @@ void orderly_reboot(void)
518519
}
519520
EXPORT_SYMBOL_GPL(orderly_reboot);
520521

522+
/**
523+
* hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
524+
* @work: work_struct associated with the emergency poweroff function
525+
*
526+
* This function is called in very critical situations to force
527+
* a kernel poweroff after a configurable timeout value.
528+
*/
529+
static void hw_failure_emergency_poweroff_func(struct work_struct *work)
530+
{
531+
/*
532+
* We have reached here after the emergency shutdown waiting period has
533+
* expired. This means orderly_poweroff has not been able to shut off
534+
* the system for some reason.
535+
*
536+
* Try to shut down the system immediately using kernel_power_off
537+
* if populated
538+
*/
539+
pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
540+
kernel_power_off();
541+
542+
/*
543+
* Worst of the worst case trigger emergency restart
544+
*/
545+
pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
546+
emergency_restart();
547+
}
548+
549+
static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
550+
hw_failure_emergency_poweroff_func);
551+
552+
/**
553+
* hw_failure_emergency_poweroff - Trigger an emergency system poweroff
554+
*
555+
* This may be called from any critical situation to trigger a system shutdown
556+
* after a given period of time. If time is negative this is not scheduled.
557+
*/
558+
static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
559+
{
560+
if (poweroff_delay_ms <= 0)
561+
return;
562+
schedule_delayed_work(&hw_failure_emergency_poweroff_work,
563+
msecs_to_jiffies(poweroff_delay_ms));
564+
}
565+
566+
/**
567+
* hw_protection_shutdown - Trigger an emergency system poweroff
568+
*
569+
* @reason: Reason of emergency shutdown to be printed.
570+
* @ms_until_forced: Time to wait for orderly shutdown before tiggering a
571+
* forced shudown. Negative value disables the forced
572+
* shutdown.
573+
*
574+
* Initiate an emergency system shutdown in order to protect hardware from
575+
* further damage. Usage examples include a thermal protection or a voltage or
576+
* current regulator failures.
577+
* NOTE: The request is ignored if protection shutdown is already pending even
578+
* if the previous request has given a large timeout for forced shutdown.
579+
* Can be called from any context.
580+
*/
581+
void hw_protection_shutdown(const char *reason, int ms_until_forced)
582+
{
583+
static atomic_t allow_proceed = ATOMIC_INIT(1);
584+
585+
pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
586+
587+
/* Shutdown should be initiated only once. */
588+
if (!atomic_dec_and_test(&allow_proceed))
589+
return;
590+
591+
/*
592+
* Queue a backup emergency shutdown in the event of
593+
* orderly_poweroff failure
594+
*/
595+
hw_failure_emergency_poweroff(ms_until_forced);
596+
orderly_poweroff(true);
597+
}
598+
EXPORT_SYMBOL_GPL(hw_protection_shutdown);
599+
521600
static int __init reboot_setup(char *str)
522601
{
523602
for (;;) {

0 commit comments

Comments
 (0)