1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/kernel/reboot.c
5 * Copyright (C) 2013 Linus Torvalds
8 #define pr_fmt(fmt) "reboot: " fmt
10 #include <linux/atomic.h>
11 #include <linux/ctype.h>
12 #include <linux/export.h>
13 #include <linux/kexec.h>
14 #include <linux/kmod.h>
15 #include <linux/kmsg_dump.h>
16 #include <linux/reboot.h>
17 #include <linux/suspend.h>
18 #include <linux/syscalls.h>
19 #include <linux/syscore_ops.h>
20 #include <linux/uaccess.h>
23 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
28 EXPORT_SYMBOL(cad_pid
);
30 #if defined(CONFIG_ARM)
31 #define DEFAULT_REBOOT_MODE = REBOOT_HARD
33 #define DEFAULT_REBOOT_MODE
35 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE
;
36 EXPORT_SYMBOL_GPL(reboot_mode
);
37 enum reboot_mode panic_reboot_mode
= REBOOT_UNDEFINED
;
40 * This variable is used privately to keep track of whether or not
41 * reboot_type is still set to its default value (i.e., reboot= hasn't
42 * been set on the command line). This is needed so that we can
43 * suppress DMI scanning for reboot quirks. Without it, it's
44 * impossible to override a faulty reboot quirk without recompiling.
46 int reboot_default
= 1;
48 enum reboot_type reboot_type
= BOOT_ACPI
;
51 struct sys_off_handler
{
52 struct notifier_block nb
;
53 int (*sys_off_cb
)(struct sys_off_data
*data
);
55 enum sys_off_mode mode
;
61 * Temporary stub that prevents linkage failure while we're in process
62 * of removing all uses of legacy pm_power_off() around the kernel.
64 void __weak (*pm_power_off
)(void);
67 * emergency_restart - reboot the system
69 * Without shutting down any hardware or taking any locks
70 * reboot the system. This is called when we know we are in
71 * trouble so this is our best effort to reboot. This is
72 * safe to call in interrupt context.
74 void emergency_restart(void)
76 kmsg_dump(KMSG_DUMP_EMERG
);
77 machine_emergency_restart();
79 EXPORT_SYMBOL_GPL(emergency_restart
);
81 void kernel_restart_prepare(char *cmd
)
83 blocking_notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, cmd
);
84 system_state
= SYSTEM_RESTART
;
85 try_block_console_kthreads(10000);
86 usermodehelper_disable();
91 * register_reboot_notifier - Register function to be called at reboot time
92 * @nb: Info about notifier function to be called
94 * Registers a function with the list of functions
95 * to be called at reboot time.
97 * Currently always returns zero, as blocking_notifier_chain_register()
98 * always returns zero.
100 int register_reboot_notifier(struct notifier_block
*nb
)
102 return blocking_notifier_chain_register(&reboot_notifier_list
, nb
);
104 EXPORT_SYMBOL(register_reboot_notifier
);
107 * unregister_reboot_notifier - Unregister previously registered reboot notifier
108 * @nb: Hook to be unregistered
110 * Unregisters a previously registered reboot
113 * Returns zero on success, or %-ENOENT on failure.
115 int unregister_reboot_notifier(struct notifier_block
*nb
)
117 return blocking_notifier_chain_unregister(&reboot_notifier_list
, nb
);
119 EXPORT_SYMBOL(unregister_reboot_notifier
);
121 static void devm_unregister_reboot_notifier(struct device
*dev
, void *res
)
123 WARN_ON(unregister_reboot_notifier(*(struct notifier_block
**)res
));
126 int devm_register_reboot_notifier(struct device
*dev
, struct notifier_block
*nb
)
128 struct notifier_block
**rcnb
;
131 rcnb
= devres_alloc(devm_unregister_reboot_notifier
,
132 sizeof(*rcnb
), GFP_KERNEL
);
136 ret
= register_reboot_notifier(nb
);
139 devres_add(dev
, rcnb
);
146 EXPORT_SYMBOL(devm_register_reboot_notifier
);
149 * Notifier list for kernel code which wants to be called
150 * to restart the system.
152 static ATOMIC_NOTIFIER_HEAD(restart_handler_list
);
155 * register_restart_handler - Register function to be called to reset
157 * @nb: Info about handler function to be called
158 * @nb->priority: Handler priority. Handlers should follow the
159 * following guidelines for setting priorities.
160 * 0: Restart handler of last resort,
161 * with limited restart capabilities
162 * 128: Default restart handler; use if no other
163 * restart handler is expected to be available,
164 * and/or if restart functionality is
165 * sufficient to restart the entire system
166 * 255: Highest priority restart handler, will
167 * preempt all other restart handlers
169 * Registers a function with code to be called to restart the
172 * Registered functions will be called from machine_restart as last
173 * step of the restart sequence (if the architecture specific
174 * machine_restart function calls do_kernel_restart - see below
176 * Registered functions are expected to restart the system immediately.
177 * If more than one function is registered, the restart handler priority
178 * selects which function will be called first.
180 * Restart handlers are expected to be registered from non-architecture
181 * code, typically from drivers. A typical use case would be a system
182 * where restart functionality is provided through a watchdog. Multiple
183 * restart handlers may exist; for example, one restart handler might
184 * restart the entire system, while another only restarts the CPU.
185 * In such cases, the restart handler which only restarts part of the
186 * hardware is expected to register with low priority to ensure that
187 * it only runs if no other means to restart the system is available.
189 * Currently always returns zero, as atomic_notifier_chain_register()
190 * always returns zero.
192 int register_restart_handler(struct notifier_block
*nb
)
194 return atomic_notifier_chain_register(&restart_handler_list
, nb
);
196 EXPORT_SYMBOL(register_restart_handler
);
199 * unregister_restart_handler - Unregister previously registered
201 * @nb: Hook to be unregistered
203 * Unregisters a previously registered restart handler function.
205 * Returns zero on success, or %-ENOENT on failure.
207 int unregister_restart_handler(struct notifier_block
*nb
)
209 return atomic_notifier_chain_unregister(&restart_handler_list
, nb
);
211 EXPORT_SYMBOL(unregister_restart_handler
);
214 * do_kernel_restart - Execute kernel restart handler call chain
216 * Calls functions registered with register_restart_handler.
218 * Expected to be called from machine_restart as last step of the restart
221 * Restarts the system immediately if a restart handler function has been
222 * registered. Otherwise does nothing.
224 void do_kernel_restart(char *cmd
)
226 atomic_notifier_call_chain(&restart_handler_list
, reboot_mode
, cmd
);
229 void migrate_to_reboot_cpu(void)
231 /* The boot cpu is always logical cpu 0 */
232 int cpu
= reboot_cpu
;
234 cpu_hotplug_disable();
236 /* Make certain the cpu I'm about to reboot on is online */
237 if (!cpu_online(cpu
))
238 cpu
= cpumask_first(cpu_online_mask
);
240 /* Prevent races with other tasks migrating this task */
241 current
->flags
|= PF_NO_SETAFFINITY
;
243 /* Make certain I only run on the appropriate processor */
244 set_cpus_allowed_ptr(current
, cpumask_of(cpu
));
248 * kernel_restart - reboot the system
249 * @cmd: pointer to buffer containing command to execute for restart
252 * Shutdown everything and perform a clean reboot.
253 * This is not safe to call in interrupt context.
255 void kernel_restart(char *cmd
)
257 kernel_restart_prepare(cmd
);
258 migrate_to_reboot_cpu();
261 pr_emerg("Restarting system\n");
263 pr_emerg("Restarting system with command '%s'\n", cmd
);
264 kmsg_dump(KMSG_DUMP_SHUTDOWN
);
265 machine_restart(cmd
);
267 EXPORT_SYMBOL_GPL(kernel_restart
);
269 static void kernel_shutdown_prepare(enum system_states state
)
271 blocking_notifier_call_chain(&reboot_notifier_list
,
272 (state
== SYSTEM_HALT
) ? SYS_HALT
: SYS_POWER_OFF
, NULL
);
273 system_state
= state
;
274 try_block_console_kthreads(10000);
275 usermodehelper_disable();
279 * kernel_halt - halt the system
281 * Shutdown everything and perform a clean system halt.
283 void kernel_halt(void)
285 kernel_shutdown_prepare(SYSTEM_HALT
);
286 migrate_to_reboot_cpu();
288 pr_emerg("System halted\n");
289 kmsg_dump(KMSG_DUMP_SHUTDOWN
);
292 EXPORT_SYMBOL_GPL(kernel_halt
);
295 * Notifier list for kernel code which wants to be called
296 * to prepare system for power off.
298 static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list
);
301 * Notifier list for kernel code which wants to be called
302 * to power off system.
304 static ATOMIC_NOTIFIER_HEAD(power_off_handler_list
);
306 static int sys_off_notify(struct notifier_block
*nb
,
307 unsigned long mode
, void *cmd
)
309 struct sys_off_handler
*handler
;
310 struct sys_off_data data
= {};
312 handler
= container_of(nb
, struct sys_off_handler
, nb
);
313 data
.cb_data
= handler
->cb_data
;
317 return handler
->sys_off_cb(&data
);
320 static struct sys_off_handler platform_sys_off_handler
;
322 static struct sys_off_handler
*alloc_sys_off_handler(int priority
)
324 struct sys_off_handler
*handler
;
328 * Platforms like m68k can't allocate sys_off handler dynamically
329 * at the early boot time because memory allocator isn't available yet.
331 if (priority
== SYS_OFF_PRIO_PLATFORM
) {
332 handler
= &platform_sys_off_handler
;
333 if (handler
->cb_data
)
334 return ERR_PTR(-EBUSY
);
336 if (system_state
> SYSTEM_RUNNING
)
341 handler
= kzalloc(sizeof(*handler
), flags
);
343 return ERR_PTR(-ENOMEM
);
349 static void free_sys_off_handler(struct sys_off_handler
*handler
)
351 if (handler
== &platform_sys_off_handler
)
352 memset(handler
, 0, sizeof(*handler
));
358 * register_sys_off_handler - Register sys-off handler
359 * @mode: Sys-off mode
360 * @priority: Handler priority
361 * @callback: Callback function
362 * @cb_data: Callback argument
364 * Registers system power-off or restart handler that will be invoked
365 * at the step corresponding to the given sys-off mode. Handler's callback
366 * should return NOTIFY_DONE to permit execution of the next handler in
367 * the call chain or NOTIFY_STOP to break the chain (in error case for
370 * Multiple handlers can be registered at the default priority level.
372 * Only one handler can be registered at the non-default priority level,
373 * otherwise ERR_PTR(-EBUSY) is returned.
375 * Returns a new instance of struct sys_off_handler on success, or
376 * an ERR_PTR()-encoded error code otherwise.
378 struct sys_off_handler
*
379 register_sys_off_handler(enum sys_off_mode mode
,
381 int (*callback
)(struct sys_off_data
*data
),
384 struct sys_off_handler
*handler
;
387 handler
= alloc_sys_off_handler(priority
);
392 case SYS_OFF_MODE_POWER_OFF_PREPARE
:
393 handler
->list
= &power_off_prep_handler_list
;
394 handler
->blocking
= true;
397 case SYS_OFF_MODE_POWER_OFF
:
398 handler
->list
= &power_off_handler_list
;
401 case SYS_OFF_MODE_RESTART
:
402 handler
->list
= &restart_handler_list
;
406 free_sys_off_handler(handler
);
407 return ERR_PTR(-EINVAL
);
410 handler
->nb
.notifier_call
= sys_off_notify
;
411 handler
->nb
.priority
= priority
;
412 handler
->sys_off_cb
= callback
;
413 handler
->cb_data
= cb_data
;
414 handler
->mode
= mode
;
416 if (handler
->blocking
) {
417 if (priority
== SYS_OFF_PRIO_DEFAULT
)
418 err
= blocking_notifier_chain_register(handler
->list
,
421 err
= blocking_notifier_chain_register_unique_prio(handler
->list
,
424 if (priority
== SYS_OFF_PRIO_DEFAULT
)
425 err
= atomic_notifier_chain_register(handler
->list
,
428 err
= atomic_notifier_chain_register_unique_prio(handler
->list
,
433 free_sys_off_handler(handler
);
439 EXPORT_SYMBOL_GPL(register_sys_off_handler
);
442 * unregister_sys_off_handler - Unregister sys-off handler
443 * @handler: Sys-off handler
445 * Unregisters given sys-off handler.
447 void unregister_sys_off_handler(struct sys_off_handler
*handler
)
451 if (IS_ERR_OR_NULL(handler
))
454 if (handler
->blocking
)
455 err
= blocking_notifier_chain_unregister(handler
->list
,
458 err
= atomic_notifier_chain_unregister(handler
->list
,
461 /* sanity check, shall never happen */
464 free_sys_off_handler(handler
);
466 EXPORT_SYMBOL_GPL(unregister_sys_off_handler
);
468 static void devm_unregister_sys_off_handler(void *data
)
470 struct sys_off_handler
*handler
= data
;
472 unregister_sys_off_handler(handler
);
476 * devm_register_sys_off_handler - Register sys-off handler
477 * @dev: Device that registers handler
478 * @mode: Sys-off mode
479 * @priority: Handler priority
480 * @callback: Callback function
481 * @cb_data: Callback argument
483 * Registers resource-managed sys-off handler.
485 * Returns zero on success, or error code on failure.
487 int devm_register_sys_off_handler(struct device
*dev
,
488 enum sys_off_mode mode
,
490 int (*callback
)(struct sys_off_data
*data
),
493 struct sys_off_handler
*handler
;
495 handler
= register_sys_off_handler(mode
, priority
, callback
, cb_data
);
497 return PTR_ERR(handler
);
499 return devm_add_action_or_reset(dev
, devm_unregister_sys_off_handler
,
502 EXPORT_SYMBOL_GPL(devm_register_sys_off_handler
);
505 * devm_register_power_off_handler - Register power-off handler
506 * @dev: Device that registers callback
507 * @callback: Callback function
508 * @cb_data: Callback's argument
510 * Registers resource-managed sys-off handler with a default priority
511 * and using power-off mode.
513 * Returns zero on success, or error code on failure.
515 int devm_register_power_off_handler(struct device
*dev
,
516 int (*callback
)(struct sys_off_data
*data
),
519 return devm_register_sys_off_handler(dev
,
520 SYS_OFF_MODE_POWER_OFF
,
521 SYS_OFF_PRIO_DEFAULT
,
524 EXPORT_SYMBOL_GPL(devm_register_power_off_handler
);
527 * devm_register_restart_handler - Register restart handler
528 * @dev: Device that registers callback
529 * @callback: Callback function
530 * @cb_data: Callback's argument
532 * Registers resource-managed sys-off handler with a default priority
533 * and using restart mode.
535 * Returns zero on success, or error code on failure.
537 int devm_register_restart_handler(struct device
*dev
,
538 int (*callback
)(struct sys_off_data
*data
),
541 return devm_register_sys_off_handler(dev
,
542 SYS_OFF_MODE_RESTART
,
543 SYS_OFF_PRIO_DEFAULT
,
546 EXPORT_SYMBOL_GPL(devm_register_restart_handler
);
548 static struct sys_off_handler
*platform_power_off_handler
;
550 static int platform_power_off_notify(struct sys_off_data
*data
)
552 void (*platform_power_power_off_cb
)(void) = data
->cb_data
;
554 platform_power_power_off_cb();
560 * register_platform_power_off - Register platform-level power-off callback
561 * @power_off: Power-off callback
563 * Registers power-off callback that will be called as last step
564 * of the power-off sequence. This callback is expected to be invoked
565 * for the last resort. Only one platform power-off callback is allowed
566 * to be registered at a time.
568 * Returns zero on success, or error code on failure.
570 int register_platform_power_off(void (*power_off
)(void))
572 struct sys_off_handler
*handler
;
574 handler
= register_sys_off_handler(SYS_OFF_MODE_POWER_OFF
,
575 SYS_OFF_PRIO_PLATFORM
,
576 platform_power_off_notify
,
579 return PTR_ERR(handler
);
581 platform_power_off_handler
= handler
;
585 EXPORT_SYMBOL_GPL(register_platform_power_off
);
588 * unregister_platform_power_off - Unregister platform-level power-off callback
589 * @power_off: Power-off callback
591 * Unregisters previously registered platform power-off callback.
593 void unregister_platform_power_off(void (*power_off
)(void))
595 if (platform_power_off_handler
&&
596 platform_power_off_handler
->cb_data
== power_off
) {
597 unregister_sys_off_handler(platform_power_off_handler
);
598 platform_power_off_handler
= NULL
;
601 EXPORT_SYMBOL_GPL(unregister_platform_power_off
);
603 static int legacy_pm_power_off(struct sys_off_data
*data
)
611 static void do_kernel_power_off_prepare(void)
613 blocking_notifier_call_chain(&power_off_prep_handler_list
, 0, NULL
);
617 * do_kernel_power_off - Execute kernel power-off handler call chain
619 * Expected to be called as last step of the power-off sequence.
621 * Powers off the system immediately if a power-off handler function has
622 * been registered. Otherwise does nothing.
624 void do_kernel_power_off(void)
626 struct sys_off_handler
*sys_off
= NULL
;
629 * Register sys-off handlers for legacy PM callback. This allows
630 * legacy PM callbacks temporary co-exist with the new sys-off API.
632 * TODO: Remove legacy handlers once all legacy PM users will be
633 * switched to the sys-off based APIs.
636 sys_off
= register_sys_off_handler(SYS_OFF_MODE_POWER_OFF
,
637 SYS_OFF_PRIO_DEFAULT
,
638 legacy_pm_power_off
, NULL
);
640 atomic_notifier_call_chain(&power_off_handler_list
, 0, NULL
);
642 unregister_sys_off_handler(sys_off
);
646 * kernel_can_power_off - check whether system can be powered off
648 * Returns true if power-off handler is registered and system can be
649 * powered off, false otherwise.
651 bool kernel_can_power_off(void)
653 return !atomic_notifier_call_chain_is_empty(&power_off_handler_list
) ||
656 EXPORT_SYMBOL_GPL(kernel_can_power_off
);
659 * kernel_power_off - power_off the system
661 * Shutdown everything and perform a clean system power_off.
663 void kernel_power_off(void)
665 kernel_shutdown_prepare(SYSTEM_POWER_OFF
);
666 do_kernel_power_off_prepare();
667 migrate_to_reboot_cpu();
669 pr_emerg("Power down\n");
670 kmsg_dump(KMSG_DUMP_SHUTDOWN
);
673 EXPORT_SYMBOL_GPL(kernel_power_off
);
675 DEFINE_MUTEX(system_transition_mutex
);
678 * Reboot system call: for obvious reasons only root may call it,
679 * and even root needs to set up some magic numbers in the registers
680 * so that some mistake won't make this reboot the whole machine.
681 * You can also set the meaning of the ctrl-alt-del-key here.
683 * reboot doesn't sync: do that yourself before calling this.
685 SYSCALL_DEFINE4(reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
688 struct pid_namespace
*pid_ns
= task_active_pid_ns(current
);
692 /* We only trust the superuser with rebooting the system. */
693 if (!ns_capable(pid_ns
->user_ns
, CAP_SYS_BOOT
))
696 /* For safety, we require "magic" arguments. */
697 if (magic1
!= LINUX_REBOOT_MAGIC1
||
698 (magic2
!= LINUX_REBOOT_MAGIC2
&&
699 magic2
!= LINUX_REBOOT_MAGIC2A
&&
700 magic2
!= LINUX_REBOOT_MAGIC2B
&&
701 magic2
!= LINUX_REBOOT_MAGIC2C
))
705 * If pid namespaces are enabled and the current task is in a child
706 * pid_namespace, the command is handled by reboot_pid_ns() which will
709 ret
= reboot_pid_ns(pid_ns
, cmd
);
713 /* Instead of trying to make the power_off code look like
714 * halt when pm_power_off is not set do it the easy way.
716 if ((cmd
== LINUX_REBOOT_CMD_POWER_OFF
) && !kernel_can_power_off())
717 cmd
= LINUX_REBOOT_CMD_HALT
;
719 mutex_lock(&system_transition_mutex
);
721 case LINUX_REBOOT_CMD_RESTART
:
722 kernel_restart(NULL
);
725 case LINUX_REBOOT_CMD_CAD_ON
:
729 case LINUX_REBOOT_CMD_CAD_OFF
:
733 case LINUX_REBOOT_CMD_HALT
:
737 case LINUX_REBOOT_CMD_POWER_OFF
:
742 case LINUX_REBOOT_CMD_RESTART2
:
743 ret
= strncpy_from_user(&buffer
[0], arg
, sizeof(buffer
) - 1);
748 buffer
[sizeof(buffer
) - 1] = '\0';
750 kernel_restart(buffer
);
753 #ifdef CONFIG_KEXEC_CORE
754 case LINUX_REBOOT_CMD_KEXEC
:
755 ret
= kernel_kexec();
759 #ifdef CONFIG_HIBERNATION
760 case LINUX_REBOOT_CMD_SW_SUSPEND
:
769 mutex_unlock(&system_transition_mutex
);
773 static void deferred_cad(struct work_struct
*dummy
)
775 kernel_restart(NULL
);
779 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
780 * As it's called within an interrupt, it may NOT sync: the only choice
781 * is whether to reboot at once, or just ignore the ctrl-alt-del.
783 void ctrl_alt_del(void)
785 static DECLARE_WORK(cad_work
, deferred_cad
);
788 schedule_work(&cad_work
);
790 kill_cad_pid(SIGINT
, 1);
793 #define POWEROFF_CMD_PATH_LEN 256
794 static char poweroff_cmd
[POWEROFF_CMD_PATH_LEN
] = "/sbin/poweroff";
795 static const char reboot_cmd
[] = "/sbin/reboot";
797 static int run_cmd(const char *cmd
)
800 static char *envp
[] = {
802 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
806 argv
= argv_split(GFP_KERNEL
, cmd
, NULL
);
808 ret
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
817 static int __orderly_reboot(void)
821 ret
= run_cmd(reboot_cmd
);
824 printk_prefer_direct_enter();
825 pr_warn("Failed to start orderly reboot: forcing the issue\n");
827 kernel_restart(NULL
);
828 printk_prefer_direct_exit();
834 static int __orderly_poweroff(bool force
)
838 ret
= run_cmd(poweroff_cmd
);
841 printk_prefer_direct_enter();
842 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
845 * I guess this should try to kick off some daemon to sync and
846 * poweroff asap. Or not even bother syncing if we're doing an
847 * emergency shutdown?
851 printk_prefer_direct_exit();
857 static bool poweroff_force
;
859 static void poweroff_work_func(struct work_struct
*work
)
861 __orderly_poweroff(poweroff_force
);
864 static DECLARE_WORK(poweroff_work
, poweroff_work_func
);
867 * orderly_poweroff - Trigger an orderly system poweroff
868 * @force: force poweroff if command execution fails
870 * This may be called from any context to trigger a system shutdown.
871 * If the orderly shutdown fails, it will force an immediate shutdown.
873 void orderly_poweroff(bool force
)
875 if (force
) /* do not override the pending "true" */
876 poweroff_force
= true;
877 schedule_work(&poweroff_work
);
879 EXPORT_SYMBOL_GPL(orderly_poweroff
);
881 static void reboot_work_func(struct work_struct
*work
)
886 static DECLARE_WORK(reboot_work
, reboot_work_func
);
889 * orderly_reboot - Trigger an orderly system reboot
891 * This may be called from any context to trigger a system reboot.
892 * If the orderly reboot fails, it will force an immediate reboot.
894 void orderly_reboot(void)
896 schedule_work(&reboot_work
);
898 EXPORT_SYMBOL_GPL(orderly_reboot
);
901 * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
902 * @work: work_struct associated with the emergency poweroff function
904 * This function is called in very critical situations to force
905 * a kernel poweroff after a configurable timeout value.
907 static void hw_failure_emergency_poweroff_func(struct work_struct
*work
)
909 printk_prefer_direct_enter();
912 * We have reached here after the emergency shutdown waiting period has
913 * expired. This means orderly_poweroff has not been able to shut off
914 * the system for some reason.
916 * Try to shut down the system immediately using kernel_power_off
919 pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
923 * Worst of the worst case trigger emergency restart
925 pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
928 printk_prefer_direct_exit();
931 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work
,
932 hw_failure_emergency_poweroff_func
);
935 * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
937 * This may be called from any critical situation to trigger a system shutdown
938 * after a given period of time. If time is negative this is not scheduled.
940 static void hw_failure_emergency_poweroff(int poweroff_delay_ms
)
942 if (poweroff_delay_ms
<= 0)
944 schedule_delayed_work(&hw_failure_emergency_poweroff_work
,
945 msecs_to_jiffies(poweroff_delay_ms
));
949 * hw_protection_shutdown - Trigger an emergency system poweroff
951 * @reason: Reason of emergency shutdown to be printed.
952 * @ms_until_forced: Time to wait for orderly shutdown before tiggering a
953 * forced shudown. Negative value disables the forced
956 * Initiate an emergency system shutdown in order to protect hardware from
957 * further damage. Usage examples include a thermal protection or a voltage or
958 * current regulator failures.
959 * NOTE: The request is ignored if protection shutdown is already pending even
960 * if the previous request has given a large timeout for forced shutdown.
961 * Can be called from any context.
963 void hw_protection_shutdown(const char *reason
, int ms_until_forced
)
965 static atomic_t allow_proceed
= ATOMIC_INIT(1);
967 printk_prefer_direct_enter();
969 pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason
);
971 /* Shutdown should be initiated only once. */
972 if (!atomic_dec_and_test(&allow_proceed
))
976 * Queue a backup emergency shutdown in the event of
977 * orderly_poweroff failure
979 hw_failure_emergency_poweroff(ms_until_forced
);
980 orderly_poweroff(true);
982 printk_prefer_direct_exit();
984 EXPORT_SYMBOL_GPL(hw_protection_shutdown
);
986 static int __init
reboot_setup(char *str
)
989 enum reboot_mode
*mode
;
992 * Having anything passed on the command line via
993 * reboot= will cause us to disable DMI checking
998 if (!strncmp(str
, "panic_", 6)) {
999 mode
= &panic_reboot_mode
;
1002 mode
= &reboot_mode
;
1007 *mode
= REBOOT_WARM
;
1011 *mode
= REBOOT_COLD
;
1015 *mode
= REBOOT_HARD
;
1020 * reboot_cpu is s[mp]#### with #### being the processor
1021 * to be used for rebooting. Skip 's' or 'smp' prefix.
1023 str
+= str
[1] == 'm' && str
[2] == 'p' ? 3 : 1;
1025 if (isdigit(str
[0])) {
1026 int cpu
= simple_strtoul(str
, NULL
, 0);
1028 if (cpu
>= num_possible_cpus()) {
1029 pr_err("Ignoring the CPU number in reboot= option. "
1030 "CPU %d exceeds possible cpu number %d\n",
1031 cpu
, num_possible_cpus());
1036 *mode
= REBOOT_SOFT
;
1040 *mode
= REBOOT_GPIO
;
1057 str
= strchr(str
, ',');
1065 __setup("reboot=", reboot_setup
);
1069 #define REBOOT_COLD_STR "cold"
1070 #define REBOOT_WARM_STR "warm"
1071 #define REBOOT_HARD_STR "hard"
1072 #define REBOOT_SOFT_STR "soft"
1073 #define REBOOT_GPIO_STR "gpio"
1074 #define REBOOT_UNDEFINED_STR "undefined"
1076 #define BOOT_TRIPLE_STR "triple"
1077 #define BOOT_KBD_STR "kbd"
1078 #define BOOT_BIOS_STR "bios"
1079 #define BOOT_ACPI_STR "acpi"
1080 #define BOOT_EFI_STR "efi"
1081 #define BOOT_PCI_STR "pci"
1083 static ssize_t
mode_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
1087 switch (reboot_mode
) {
1089 val
= REBOOT_COLD_STR
;
1092 val
= REBOOT_WARM_STR
;
1095 val
= REBOOT_HARD_STR
;
1098 val
= REBOOT_SOFT_STR
;
1101 val
= REBOOT_GPIO_STR
;
1104 val
= REBOOT_UNDEFINED_STR
;
1107 return sprintf(buf
, "%s\n", val
);
1109 static ssize_t
mode_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1110 const char *buf
, size_t count
)
1112 if (!capable(CAP_SYS_BOOT
))
1115 if (!strncmp(buf
, REBOOT_COLD_STR
, strlen(REBOOT_COLD_STR
)))
1116 reboot_mode
= REBOOT_COLD
;
1117 else if (!strncmp(buf
, REBOOT_WARM_STR
, strlen(REBOOT_WARM_STR
)))
1118 reboot_mode
= REBOOT_WARM
;
1119 else if (!strncmp(buf
, REBOOT_HARD_STR
, strlen(REBOOT_HARD_STR
)))
1120 reboot_mode
= REBOOT_HARD
;
1121 else if (!strncmp(buf
, REBOOT_SOFT_STR
, strlen(REBOOT_SOFT_STR
)))
1122 reboot_mode
= REBOOT_SOFT
;
1123 else if (!strncmp(buf
, REBOOT_GPIO_STR
, strlen(REBOOT_GPIO_STR
)))
1124 reboot_mode
= REBOOT_GPIO
;
1132 static struct kobj_attribute reboot_mode_attr
= __ATTR_RW(mode
);
1135 static ssize_t
force_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
1137 return sprintf(buf
, "%d\n", reboot_force
);
1139 static ssize_t
force_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1140 const char *buf
, size_t count
)
1144 if (!capable(CAP_SYS_BOOT
))
1147 if (kstrtobool(buf
, &res
))
1155 static struct kobj_attribute reboot_force_attr
= __ATTR_RW(force
);
1157 static ssize_t
type_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
1161 switch (reboot_type
) {
1163 val
= BOOT_TRIPLE_STR
;
1169 val
= BOOT_BIOS_STR
;
1172 val
= BOOT_ACPI_STR
;
1177 case BOOT_CF9_FORCE
:
1181 val
= REBOOT_UNDEFINED_STR
;
1184 return sprintf(buf
, "%s\n", val
);
1186 static ssize_t
type_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1187 const char *buf
, size_t count
)
1189 if (!capable(CAP_SYS_BOOT
))
1192 if (!strncmp(buf
, BOOT_TRIPLE_STR
, strlen(BOOT_TRIPLE_STR
)))
1193 reboot_type
= BOOT_TRIPLE
;
1194 else if (!strncmp(buf
, BOOT_KBD_STR
, strlen(BOOT_KBD_STR
)))
1195 reboot_type
= BOOT_KBD
;
1196 else if (!strncmp(buf
, BOOT_BIOS_STR
, strlen(BOOT_BIOS_STR
)))
1197 reboot_type
= BOOT_BIOS
;
1198 else if (!strncmp(buf
, BOOT_ACPI_STR
, strlen(BOOT_ACPI_STR
)))
1199 reboot_type
= BOOT_ACPI
;
1200 else if (!strncmp(buf
, BOOT_EFI_STR
, strlen(BOOT_EFI_STR
)))
1201 reboot_type
= BOOT_EFI
;
1202 else if (!strncmp(buf
, BOOT_PCI_STR
, strlen(BOOT_PCI_STR
)))
1203 reboot_type
= BOOT_CF9_FORCE
;
1211 static struct kobj_attribute reboot_type_attr
= __ATTR_RW(type
);
1215 static ssize_t
cpu_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
1217 return sprintf(buf
, "%d\n", reboot_cpu
);
1219 static ssize_t
cpu_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1220 const char *buf
, size_t count
)
1222 unsigned int cpunum
;
1225 if (!capable(CAP_SYS_BOOT
))
1228 rc
= kstrtouint(buf
, 0, &cpunum
);
1233 if (cpunum
>= num_possible_cpus())
1237 reboot_cpu
= cpunum
;
1241 static struct kobj_attribute reboot_cpu_attr
= __ATTR_RW(cpu
);
1244 static struct attribute
*reboot_attrs
[] = {
1245 &reboot_mode_attr
.attr
,
1247 &reboot_force_attr
.attr
,
1248 &reboot_type_attr
.attr
,
1251 &reboot_cpu_attr
.attr
,
1256 #ifdef CONFIG_SYSCTL
1257 static struct ctl_table kern_reboot_table
[] = {
1259 .procname
= "poweroff_cmd",
1260 .data
= &poweroff_cmd
,
1261 .maxlen
= POWEROFF_CMD_PATH_LEN
,
1263 .proc_handler
= proc_dostring
,
1266 .procname
= "ctrl-alt-del",
1268 .maxlen
= sizeof(int),
1270 .proc_handler
= proc_dointvec
,
1275 static void __init
kernel_reboot_sysctls_init(void)
1277 register_sysctl_init("kernel", kern_reboot_table
);
1280 #define kernel_reboot_sysctls_init() do { } while (0)
1281 #endif /* CONFIG_SYSCTL */
1283 static const struct attribute_group reboot_attr_group
= {
1284 .attrs
= reboot_attrs
,
1287 static int __init
reboot_ksysfs_init(void)
1289 struct kobject
*reboot_kobj
;
1292 reboot_kobj
= kobject_create_and_add("reboot", kernel_kobj
);
1296 ret
= sysfs_create_group(reboot_kobj
, &reboot_attr_group
);
1298 kobject_put(reboot_kobj
);
1302 kernel_reboot_sysctls_init();
1306 late_initcall(reboot_ksysfs_init
);