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
;
62 * Temporary stub that prevents linkage failure while we're in process
63 * of removing all uses of legacy pm_power_off() around the kernel.
65 void __weak (*pm_power_off
)(void);
68 * emergency_restart - reboot the system
70 * Without shutting down any hardware or taking any locks
71 * reboot the system. This is called when we know we are in
72 * trouble so this is our best effort to reboot. This is
73 * safe to call in interrupt context.
75 void emergency_restart(void)
77 kmsg_dump(KMSG_DUMP_EMERG
);
78 system_state
= SYSTEM_RESTART
;
79 machine_emergency_restart();
81 EXPORT_SYMBOL_GPL(emergency_restart
);
83 void kernel_restart_prepare(char *cmd
)
85 blocking_notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, cmd
);
86 system_state
= SYSTEM_RESTART
;
87 usermodehelper_disable();
92 * register_reboot_notifier - Register function to be called at reboot time
93 * @nb: Info about notifier function to be called
95 * Registers a function with the list of functions
96 * to be called at reboot time.
98 * Currently always returns zero, as blocking_notifier_chain_register()
99 * always returns zero.
101 int register_reboot_notifier(struct notifier_block
*nb
)
103 return blocking_notifier_chain_register(&reboot_notifier_list
, nb
);
105 EXPORT_SYMBOL(register_reboot_notifier
);
108 * unregister_reboot_notifier - Unregister previously registered reboot notifier
109 * @nb: Hook to be unregistered
111 * Unregisters a previously registered reboot
114 * Returns zero on success, or %-ENOENT on failure.
116 int unregister_reboot_notifier(struct notifier_block
*nb
)
118 return blocking_notifier_chain_unregister(&reboot_notifier_list
, nb
);
120 EXPORT_SYMBOL(unregister_reboot_notifier
);
122 static void devm_unregister_reboot_notifier(struct device
*dev
, void *res
)
124 WARN_ON(unregister_reboot_notifier(*(struct notifier_block
**)res
));
127 int devm_register_reboot_notifier(struct device
*dev
, struct notifier_block
*nb
)
129 struct notifier_block
**rcnb
;
132 rcnb
= devres_alloc(devm_unregister_reboot_notifier
,
133 sizeof(*rcnb
), GFP_KERNEL
);
137 ret
= register_reboot_notifier(nb
);
140 devres_add(dev
, rcnb
);
147 EXPORT_SYMBOL(devm_register_reboot_notifier
);
150 * Notifier list for kernel code which wants to be called
151 * to restart the system.
153 static ATOMIC_NOTIFIER_HEAD(restart_handler_list
);
156 * register_restart_handler - Register function to be called to reset
158 * @nb: Info about handler function to be called
159 * @nb->priority: Handler priority. Handlers should follow the
160 * following guidelines for setting priorities.
161 * 0: Restart handler of last resort,
162 * with limited restart capabilities
163 * 128: Default restart handler; use if no other
164 * restart handler is expected to be available,
165 * and/or if restart functionality is
166 * sufficient to restart the entire system
167 * 255: Highest priority restart handler, will
168 * preempt all other restart handlers
170 * Registers a function with code to be called to restart the
173 * Registered functions will be called from machine_restart as last
174 * step of the restart sequence (if the architecture specific
175 * machine_restart function calls do_kernel_restart - see below
177 * Registered functions are expected to restart the system immediately.
178 * If more than one function is registered, the restart handler priority
179 * selects which function will be called first.
181 * Restart handlers are expected to be registered from non-architecture
182 * code, typically from drivers. A typical use case would be a system
183 * where restart functionality is provided through a watchdog. Multiple
184 * restart handlers may exist; for example, one restart handler might
185 * restart the entire system, while another only restarts the CPU.
186 * In such cases, the restart handler which only restarts part of the
187 * hardware is expected to register with low priority to ensure that
188 * it only runs if no other means to restart the system is available.
190 * Currently always returns zero, as atomic_notifier_chain_register()
191 * always returns zero.
193 int register_restart_handler(struct notifier_block
*nb
)
195 return atomic_notifier_chain_register(&restart_handler_list
, nb
);
197 EXPORT_SYMBOL(register_restart_handler
);
200 * unregister_restart_handler - Unregister previously registered
202 * @nb: Hook to be unregistered
204 * Unregisters a previously registered restart handler function.
206 * Returns zero on success, or %-ENOENT on failure.
208 int unregister_restart_handler(struct notifier_block
*nb
)
210 return atomic_notifier_chain_unregister(&restart_handler_list
, nb
);
212 EXPORT_SYMBOL(unregister_restart_handler
);
215 * do_kernel_restart - Execute kernel restart handler call chain
217 * Calls functions registered with register_restart_handler.
219 * Expected to be called from machine_restart as last step of the restart
222 * Restarts the system immediately if a restart handler function has been
223 * registered. Otherwise does nothing.
225 void do_kernel_restart(char *cmd
)
227 atomic_notifier_call_chain(&restart_handler_list
, reboot_mode
, cmd
);
230 void migrate_to_reboot_cpu(void)
232 /* The boot cpu is always logical cpu 0 */
233 int cpu
= reboot_cpu
;
235 cpu_hotplug_disable();
237 /* Make certain the cpu I'm about to reboot on is online */
238 if (!cpu_online(cpu
))
239 cpu
= cpumask_first(cpu_online_mask
);
241 /* Prevent races with other tasks migrating this task */
242 current
->flags
|= PF_NO_SETAFFINITY
;
244 /* Make certain I only run on the appropriate processor */
245 set_cpus_allowed_ptr(current
, cpumask_of(cpu
));
249 * Notifier list for kernel code which wants to be called
250 * to prepare system for restart.
252 static BLOCKING_NOTIFIER_HEAD(restart_prep_handler_list
);
254 static void do_kernel_restart_prepare(void)
256 blocking_notifier_call_chain(&restart_prep_handler_list
, 0, NULL
);
260 * kernel_restart - reboot the system
261 * @cmd: pointer to buffer containing command to execute for restart
264 * Shutdown everything and perform a clean reboot.
265 * This is not safe to call in interrupt context.
267 void kernel_restart(char *cmd
)
269 kernel_restart_prepare(cmd
);
270 do_kernel_restart_prepare();
271 migrate_to_reboot_cpu();
274 pr_emerg("Restarting system\n");
276 pr_emerg("Restarting system with command '%s'\n", cmd
);
277 kmsg_dump(KMSG_DUMP_SHUTDOWN
);
278 machine_restart(cmd
);
280 EXPORT_SYMBOL_GPL(kernel_restart
);
282 static void kernel_shutdown_prepare(enum system_states state
)
284 blocking_notifier_call_chain(&reboot_notifier_list
,
285 (state
== SYSTEM_HALT
) ? SYS_HALT
: SYS_POWER_OFF
, NULL
);
286 system_state
= state
;
287 usermodehelper_disable();
291 * kernel_halt - halt the system
293 * Shutdown everything and perform a clean system halt.
295 void kernel_halt(void)
297 kernel_shutdown_prepare(SYSTEM_HALT
);
298 migrate_to_reboot_cpu();
300 pr_emerg("System halted\n");
301 kmsg_dump(KMSG_DUMP_SHUTDOWN
);
304 EXPORT_SYMBOL_GPL(kernel_halt
);
307 * Notifier list for kernel code which wants to be called
308 * to prepare system for power off.
310 static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list
);
313 * Notifier list for kernel code which wants to be called
314 * to power off system.
316 static ATOMIC_NOTIFIER_HEAD(power_off_handler_list
);
318 static int sys_off_notify(struct notifier_block
*nb
,
319 unsigned long mode
, void *cmd
)
321 struct sys_off_handler
*handler
;
322 struct sys_off_data data
= {};
324 handler
= container_of(nb
, struct sys_off_handler
, nb
);
325 data
.cb_data
= handler
->cb_data
;
328 data
.dev
= handler
->dev
;
330 return handler
->sys_off_cb(&data
);
333 static struct sys_off_handler platform_sys_off_handler
;
335 static struct sys_off_handler
*alloc_sys_off_handler(int priority
)
337 struct sys_off_handler
*handler
;
341 * Platforms like m68k can't allocate sys_off handler dynamically
342 * at the early boot time because memory allocator isn't available yet.
344 if (priority
== SYS_OFF_PRIO_PLATFORM
) {
345 handler
= &platform_sys_off_handler
;
346 if (handler
->cb_data
)
347 return ERR_PTR(-EBUSY
);
349 if (system_state
> SYSTEM_RUNNING
)
354 handler
= kzalloc(sizeof(*handler
), flags
);
356 return ERR_PTR(-ENOMEM
);
362 static void free_sys_off_handler(struct sys_off_handler
*handler
)
364 if (handler
== &platform_sys_off_handler
)
365 memset(handler
, 0, sizeof(*handler
));
371 * register_sys_off_handler - Register sys-off handler
372 * @mode: Sys-off mode
373 * @priority: Handler priority
374 * @callback: Callback function
375 * @cb_data: Callback argument
377 * Registers system power-off or restart handler that will be invoked
378 * at the step corresponding to the given sys-off mode. Handler's callback
379 * should return NOTIFY_DONE to permit execution of the next handler in
380 * the call chain or NOTIFY_STOP to break the chain (in error case for
383 * Multiple handlers can be registered at the default priority level.
385 * Only one handler can be registered at the non-default priority level,
386 * otherwise ERR_PTR(-EBUSY) is returned.
388 * Returns a new instance of struct sys_off_handler on success, or
389 * an ERR_PTR()-encoded error code otherwise.
391 struct sys_off_handler
*
392 register_sys_off_handler(enum sys_off_mode mode
,
394 int (*callback
)(struct sys_off_data
*data
),
397 struct sys_off_handler
*handler
;
400 handler
= alloc_sys_off_handler(priority
);
405 case SYS_OFF_MODE_POWER_OFF_PREPARE
:
406 handler
->list
= &power_off_prep_handler_list
;
407 handler
->blocking
= true;
410 case SYS_OFF_MODE_POWER_OFF
:
411 handler
->list
= &power_off_handler_list
;
414 case SYS_OFF_MODE_RESTART_PREPARE
:
415 handler
->list
= &restart_prep_handler_list
;
416 handler
->blocking
= true;
419 case SYS_OFF_MODE_RESTART
:
420 handler
->list
= &restart_handler_list
;
424 free_sys_off_handler(handler
);
425 return ERR_PTR(-EINVAL
);
428 handler
->nb
.notifier_call
= sys_off_notify
;
429 handler
->nb
.priority
= priority
;
430 handler
->sys_off_cb
= callback
;
431 handler
->cb_data
= cb_data
;
432 handler
->mode
= mode
;
434 if (handler
->blocking
) {
435 if (priority
== SYS_OFF_PRIO_DEFAULT
)
436 err
= blocking_notifier_chain_register(handler
->list
,
439 err
= blocking_notifier_chain_register_unique_prio(handler
->list
,
442 if (priority
== SYS_OFF_PRIO_DEFAULT
)
443 err
= atomic_notifier_chain_register(handler
->list
,
446 err
= atomic_notifier_chain_register_unique_prio(handler
->list
,
451 free_sys_off_handler(handler
);
457 EXPORT_SYMBOL_GPL(register_sys_off_handler
);
460 * unregister_sys_off_handler - Unregister sys-off handler
461 * @handler: Sys-off handler
463 * Unregisters given sys-off handler.
465 void unregister_sys_off_handler(struct sys_off_handler
*handler
)
469 if (IS_ERR_OR_NULL(handler
))
472 if (handler
->blocking
)
473 err
= blocking_notifier_chain_unregister(handler
->list
,
476 err
= atomic_notifier_chain_unregister(handler
->list
,
479 /* sanity check, shall never happen */
482 free_sys_off_handler(handler
);
484 EXPORT_SYMBOL_GPL(unregister_sys_off_handler
);
486 static void devm_unregister_sys_off_handler(void *data
)
488 struct sys_off_handler
*handler
= data
;
490 unregister_sys_off_handler(handler
);
494 * devm_register_sys_off_handler - Register sys-off handler
495 * @dev: Device that registers handler
496 * @mode: Sys-off mode
497 * @priority: Handler priority
498 * @callback: Callback function
499 * @cb_data: Callback argument
501 * Registers resource-managed sys-off handler.
503 * Returns zero on success, or error code on failure.
505 int devm_register_sys_off_handler(struct device
*dev
,
506 enum sys_off_mode mode
,
508 int (*callback
)(struct sys_off_data
*data
),
511 struct sys_off_handler
*handler
;
513 handler
= register_sys_off_handler(mode
, priority
, callback
, cb_data
);
515 return PTR_ERR(handler
);
518 return devm_add_action_or_reset(dev
, devm_unregister_sys_off_handler
,
521 EXPORT_SYMBOL_GPL(devm_register_sys_off_handler
);
524 * devm_register_power_off_handler - Register power-off handler
525 * @dev: Device that registers callback
526 * @callback: Callback function
527 * @cb_data: Callback's argument
529 * Registers resource-managed sys-off handler with a default priority
530 * and using power-off mode.
532 * Returns zero on success, or error code on failure.
534 int devm_register_power_off_handler(struct device
*dev
,
535 int (*callback
)(struct sys_off_data
*data
),
538 return devm_register_sys_off_handler(dev
,
539 SYS_OFF_MODE_POWER_OFF
,
540 SYS_OFF_PRIO_DEFAULT
,
543 EXPORT_SYMBOL_GPL(devm_register_power_off_handler
);
546 * devm_register_restart_handler - Register restart handler
547 * @dev: Device that registers callback
548 * @callback: Callback function
549 * @cb_data: Callback's argument
551 * Registers resource-managed sys-off handler with a default priority
552 * and using restart mode.
554 * Returns zero on success, or error code on failure.
556 int devm_register_restart_handler(struct device
*dev
,
557 int (*callback
)(struct sys_off_data
*data
),
560 return devm_register_sys_off_handler(dev
,
561 SYS_OFF_MODE_RESTART
,
562 SYS_OFF_PRIO_DEFAULT
,
565 EXPORT_SYMBOL_GPL(devm_register_restart_handler
);
567 static struct sys_off_handler
*platform_power_off_handler
;
569 static int platform_power_off_notify(struct sys_off_data
*data
)
571 void (*platform_power_power_off_cb
)(void) = data
->cb_data
;
573 platform_power_power_off_cb();
579 * register_platform_power_off - Register platform-level power-off callback
580 * @power_off: Power-off callback
582 * Registers power-off callback that will be called as last step
583 * of the power-off sequence. This callback is expected to be invoked
584 * for the last resort. Only one platform power-off callback is allowed
585 * to be registered at a time.
587 * Returns zero on success, or error code on failure.
589 int register_platform_power_off(void (*power_off
)(void))
591 struct sys_off_handler
*handler
;
593 handler
= register_sys_off_handler(SYS_OFF_MODE_POWER_OFF
,
594 SYS_OFF_PRIO_PLATFORM
,
595 platform_power_off_notify
,
598 return PTR_ERR(handler
);
600 platform_power_off_handler
= handler
;
604 EXPORT_SYMBOL_GPL(register_platform_power_off
);
607 * unregister_platform_power_off - Unregister platform-level power-off callback
608 * @power_off: Power-off callback
610 * Unregisters previously registered platform power-off callback.
612 void unregister_platform_power_off(void (*power_off
)(void))
614 if (platform_power_off_handler
&&
615 platform_power_off_handler
->cb_data
== power_off
) {
616 unregister_sys_off_handler(platform_power_off_handler
);
617 platform_power_off_handler
= NULL
;
620 EXPORT_SYMBOL_GPL(unregister_platform_power_off
);
622 static int legacy_pm_power_off(struct sys_off_data
*data
)
630 static void do_kernel_power_off_prepare(void)
632 blocking_notifier_call_chain(&power_off_prep_handler_list
, 0, NULL
);
636 * do_kernel_power_off - Execute kernel power-off handler call chain
638 * Expected to be called as last step of the power-off sequence.
640 * Powers off the system immediately if a power-off handler function has
641 * been registered. Otherwise does nothing.
643 void do_kernel_power_off(void)
645 struct sys_off_handler
*sys_off
= NULL
;
648 * Register sys-off handlers for legacy PM callback. This allows
649 * legacy PM callbacks temporary co-exist with the new sys-off API.
651 * TODO: Remove legacy handlers once all legacy PM users will be
652 * switched to the sys-off based APIs.
655 sys_off
= register_sys_off_handler(SYS_OFF_MODE_POWER_OFF
,
656 SYS_OFF_PRIO_DEFAULT
,
657 legacy_pm_power_off
, NULL
);
659 atomic_notifier_call_chain(&power_off_handler_list
, 0, NULL
);
661 unregister_sys_off_handler(sys_off
);
665 * kernel_can_power_off - check whether system can be powered off
667 * Returns true if power-off handler is registered and system can be
668 * powered off, false otherwise.
670 bool kernel_can_power_off(void)
672 return !atomic_notifier_call_chain_is_empty(&power_off_handler_list
) ||
675 EXPORT_SYMBOL_GPL(kernel_can_power_off
);
678 * kernel_power_off - power_off the system
680 * Shutdown everything and perform a clean system power_off.
682 void kernel_power_off(void)
684 kernel_shutdown_prepare(SYSTEM_POWER_OFF
);
685 do_kernel_power_off_prepare();
686 migrate_to_reboot_cpu();
688 pr_emerg("Power down\n");
689 kmsg_dump(KMSG_DUMP_SHUTDOWN
);
692 EXPORT_SYMBOL_GPL(kernel_power_off
);
694 DEFINE_MUTEX(system_transition_mutex
);
697 * Reboot system call: for obvious reasons only root may call it,
698 * and even root needs to set up some magic numbers in the registers
699 * so that some mistake won't make this reboot the whole machine.
700 * You can also set the meaning of the ctrl-alt-del-key here.
702 * reboot doesn't sync: do that yourself before calling this.
704 SYSCALL_DEFINE4(reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
707 struct pid_namespace
*pid_ns
= task_active_pid_ns(current
);
711 /* We only trust the superuser with rebooting the system. */
712 if (!ns_capable(pid_ns
->user_ns
, CAP_SYS_BOOT
))
715 /* For safety, we require "magic" arguments. */
716 if (magic1
!= LINUX_REBOOT_MAGIC1
||
717 (magic2
!= LINUX_REBOOT_MAGIC2
&&
718 magic2
!= LINUX_REBOOT_MAGIC2A
&&
719 magic2
!= LINUX_REBOOT_MAGIC2B
&&
720 magic2
!= LINUX_REBOOT_MAGIC2C
))
724 * If pid namespaces are enabled and the current task is in a child
725 * pid_namespace, the command is handled by reboot_pid_ns() which will
728 ret
= reboot_pid_ns(pid_ns
, cmd
);
732 /* Instead of trying to make the power_off code look like
733 * halt when pm_power_off is not set do it the easy way.
735 if ((cmd
== LINUX_REBOOT_CMD_POWER_OFF
) && !kernel_can_power_off())
736 cmd
= LINUX_REBOOT_CMD_HALT
;
738 mutex_lock(&system_transition_mutex
);
740 case LINUX_REBOOT_CMD_RESTART
:
741 kernel_restart(NULL
);
744 case LINUX_REBOOT_CMD_CAD_ON
:
748 case LINUX_REBOOT_CMD_CAD_OFF
:
752 case LINUX_REBOOT_CMD_HALT
:
756 case LINUX_REBOOT_CMD_POWER_OFF
:
761 case LINUX_REBOOT_CMD_RESTART2
:
762 ret
= strncpy_from_user(&buffer
[0], arg
, sizeof(buffer
) - 1);
767 buffer
[sizeof(buffer
) - 1] = '\0';
769 kernel_restart(buffer
);
772 #ifdef CONFIG_KEXEC_CORE
773 case LINUX_REBOOT_CMD_KEXEC
:
774 ret
= kernel_kexec();
778 #ifdef CONFIG_HIBERNATION
779 case LINUX_REBOOT_CMD_SW_SUSPEND
:
788 mutex_unlock(&system_transition_mutex
);
792 static void deferred_cad(struct work_struct
*dummy
)
794 kernel_restart(NULL
);
798 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
799 * As it's called within an interrupt, it may NOT sync: the only choice
800 * is whether to reboot at once, or just ignore the ctrl-alt-del.
802 void ctrl_alt_del(void)
804 static DECLARE_WORK(cad_work
, deferred_cad
);
807 schedule_work(&cad_work
);
809 kill_cad_pid(SIGINT
, 1);
812 #define POWEROFF_CMD_PATH_LEN 256
813 static char poweroff_cmd
[POWEROFF_CMD_PATH_LEN
] = "/sbin/poweroff";
814 static const char reboot_cmd
[] = "/sbin/reboot";
816 static int run_cmd(const char *cmd
)
819 static char *envp
[] = {
821 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
825 argv
= argv_split(GFP_KERNEL
, cmd
, NULL
);
827 ret
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
836 static int __orderly_reboot(void)
840 ret
= run_cmd(reboot_cmd
);
843 pr_warn("Failed to start orderly reboot: forcing the issue\n");
845 kernel_restart(NULL
);
851 static int __orderly_poweroff(bool force
)
855 ret
= run_cmd(poweroff_cmd
);
858 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
861 * I guess this should try to kick off some daemon to sync and
862 * poweroff asap. Or not even bother syncing if we're doing an
863 * emergency shutdown?
872 static bool poweroff_force
;
874 static void poweroff_work_func(struct work_struct
*work
)
876 __orderly_poweroff(poweroff_force
);
879 static DECLARE_WORK(poweroff_work
, poweroff_work_func
);
882 * orderly_poweroff - Trigger an orderly system poweroff
883 * @force: force poweroff if command execution fails
885 * This may be called from any context to trigger a system shutdown.
886 * If the orderly shutdown fails, it will force an immediate shutdown.
888 void orderly_poweroff(bool force
)
890 if (force
) /* do not override the pending "true" */
891 poweroff_force
= true;
892 schedule_work(&poweroff_work
);
894 EXPORT_SYMBOL_GPL(orderly_poweroff
);
896 static void reboot_work_func(struct work_struct
*work
)
901 static DECLARE_WORK(reboot_work
, reboot_work_func
);
904 * orderly_reboot - Trigger an orderly system reboot
906 * This may be called from any context to trigger a system reboot.
907 * If the orderly reboot fails, it will force an immediate reboot.
909 void orderly_reboot(void)
911 schedule_work(&reboot_work
);
913 EXPORT_SYMBOL_GPL(orderly_reboot
);
916 * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
917 * @work: work_struct associated with the emergency poweroff function
919 * This function is called in very critical situations to force
920 * a kernel poweroff after a configurable timeout value.
922 static void hw_failure_emergency_poweroff_func(struct work_struct
*work
)
925 * We have reached here after the emergency shutdown waiting period has
926 * expired. This means orderly_poweroff has not been able to shut off
927 * the system for some reason.
929 * Try to shut down the system immediately using kernel_power_off
932 pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
936 * Worst of the worst case trigger emergency restart
938 pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
942 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work
,
943 hw_failure_emergency_poweroff_func
);
946 * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
948 * This may be called from any critical situation to trigger a system shutdown
949 * after a given period of time. If time is negative this is not scheduled.
951 static void hw_failure_emergency_poweroff(int poweroff_delay_ms
)
953 if (poweroff_delay_ms
<= 0)
955 schedule_delayed_work(&hw_failure_emergency_poweroff_work
,
956 msecs_to_jiffies(poweroff_delay_ms
));
960 * hw_protection_shutdown - Trigger an emergency system poweroff
962 * @reason: Reason of emergency shutdown to be printed.
963 * @ms_until_forced: Time to wait for orderly shutdown before tiggering a
964 * forced shudown. Negative value disables the forced
967 * Initiate an emergency system shutdown in order to protect hardware from
968 * further damage. Usage examples include a thermal protection or a voltage or
969 * current regulator failures.
970 * NOTE: The request is ignored if protection shutdown is already pending even
971 * if the previous request has given a large timeout for forced shutdown.
972 * Can be called from any context.
974 void hw_protection_shutdown(const char *reason
, int ms_until_forced
)
976 static atomic_t allow_proceed
= ATOMIC_INIT(1);
978 pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason
);
980 /* Shutdown should be initiated only once. */
981 if (!atomic_dec_and_test(&allow_proceed
))
985 * Queue a backup emergency shutdown in the event of
986 * orderly_poweroff failure
988 hw_failure_emergency_poweroff(ms_until_forced
);
989 orderly_poweroff(true);
991 EXPORT_SYMBOL_GPL(hw_protection_shutdown
);
993 static int __init
reboot_setup(char *str
)
996 enum reboot_mode
*mode
;
999 * Having anything passed on the command line via
1000 * reboot= will cause us to disable DMI checking
1005 if (!strncmp(str
, "panic_", 6)) {
1006 mode
= &panic_reboot_mode
;
1009 mode
= &reboot_mode
;
1014 *mode
= REBOOT_WARM
;
1018 *mode
= REBOOT_COLD
;
1022 *mode
= REBOOT_HARD
;
1027 * reboot_cpu is s[mp]#### with #### being the processor
1028 * to be used for rebooting. Skip 's' or 'smp' prefix.
1030 str
+= str
[1] == 'm' && str
[2] == 'p' ? 3 : 1;
1032 if (isdigit(str
[0])) {
1033 int cpu
= simple_strtoul(str
, NULL
, 0);
1035 if (cpu
>= num_possible_cpus()) {
1036 pr_err("Ignoring the CPU number in reboot= option. "
1037 "CPU %d exceeds possible cpu number %d\n",
1038 cpu
, num_possible_cpus());
1043 *mode
= REBOOT_SOFT
;
1047 *mode
= REBOOT_GPIO
;
1064 str
= strchr(str
, ',');
1072 __setup("reboot=", reboot_setup
);
1076 #define REBOOT_COLD_STR "cold"
1077 #define REBOOT_WARM_STR "warm"
1078 #define REBOOT_HARD_STR "hard"
1079 #define REBOOT_SOFT_STR "soft"
1080 #define REBOOT_GPIO_STR "gpio"
1081 #define REBOOT_UNDEFINED_STR "undefined"
1083 #define BOOT_TRIPLE_STR "triple"
1084 #define BOOT_KBD_STR "kbd"
1085 #define BOOT_BIOS_STR "bios"
1086 #define BOOT_ACPI_STR "acpi"
1087 #define BOOT_EFI_STR "efi"
1088 #define BOOT_PCI_STR "pci"
1090 static ssize_t
mode_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
1094 switch (reboot_mode
) {
1096 val
= REBOOT_COLD_STR
;
1099 val
= REBOOT_WARM_STR
;
1102 val
= REBOOT_HARD_STR
;
1105 val
= REBOOT_SOFT_STR
;
1108 val
= REBOOT_GPIO_STR
;
1111 val
= REBOOT_UNDEFINED_STR
;
1114 return sprintf(buf
, "%s\n", val
);
1116 static ssize_t
mode_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1117 const char *buf
, size_t count
)
1119 if (!capable(CAP_SYS_BOOT
))
1122 if (!strncmp(buf
, REBOOT_COLD_STR
, strlen(REBOOT_COLD_STR
)))
1123 reboot_mode
= REBOOT_COLD
;
1124 else if (!strncmp(buf
, REBOOT_WARM_STR
, strlen(REBOOT_WARM_STR
)))
1125 reboot_mode
= REBOOT_WARM
;
1126 else if (!strncmp(buf
, REBOOT_HARD_STR
, strlen(REBOOT_HARD_STR
)))
1127 reboot_mode
= REBOOT_HARD
;
1128 else if (!strncmp(buf
, REBOOT_SOFT_STR
, strlen(REBOOT_SOFT_STR
)))
1129 reboot_mode
= REBOOT_SOFT
;
1130 else if (!strncmp(buf
, REBOOT_GPIO_STR
, strlen(REBOOT_GPIO_STR
)))
1131 reboot_mode
= REBOOT_GPIO
;
1139 static struct kobj_attribute reboot_mode_attr
= __ATTR_RW(mode
);
1142 static ssize_t
force_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
1144 return sprintf(buf
, "%d\n", reboot_force
);
1146 static ssize_t
force_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1147 const char *buf
, size_t count
)
1151 if (!capable(CAP_SYS_BOOT
))
1154 if (kstrtobool(buf
, &res
))
1162 static struct kobj_attribute reboot_force_attr
= __ATTR_RW(force
);
1164 static ssize_t
type_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
1168 switch (reboot_type
) {
1170 val
= BOOT_TRIPLE_STR
;
1176 val
= BOOT_BIOS_STR
;
1179 val
= BOOT_ACPI_STR
;
1184 case BOOT_CF9_FORCE
:
1188 val
= REBOOT_UNDEFINED_STR
;
1191 return sprintf(buf
, "%s\n", val
);
1193 static ssize_t
type_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1194 const char *buf
, size_t count
)
1196 if (!capable(CAP_SYS_BOOT
))
1199 if (!strncmp(buf
, BOOT_TRIPLE_STR
, strlen(BOOT_TRIPLE_STR
)))
1200 reboot_type
= BOOT_TRIPLE
;
1201 else if (!strncmp(buf
, BOOT_KBD_STR
, strlen(BOOT_KBD_STR
)))
1202 reboot_type
= BOOT_KBD
;
1203 else if (!strncmp(buf
, BOOT_BIOS_STR
, strlen(BOOT_BIOS_STR
)))
1204 reboot_type
= BOOT_BIOS
;
1205 else if (!strncmp(buf
, BOOT_ACPI_STR
, strlen(BOOT_ACPI_STR
)))
1206 reboot_type
= BOOT_ACPI
;
1207 else if (!strncmp(buf
, BOOT_EFI_STR
, strlen(BOOT_EFI_STR
)))
1208 reboot_type
= BOOT_EFI
;
1209 else if (!strncmp(buf
, BOOT_PCI_STR
, strlen(BOOT_PCI_STR
)))
1210 reboot_type
= BOOT_CF9_FORCE
;
1218 static struct kobj_attribute reboot_type_attr
= __ATTR_RW(type
);
1222 static ssize_t
cpu_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
1224 return sprintf(buf
, "%d\n", reboot_cpu
);
1226 static ssize_t
cpu_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1227 const char *buf
, size_t count
)
1229 unsigned int cpunum
;
1232 if (!capable(CAP_SYS_BOOT
))
1235 rc
= kstrtouint(buf
, 0, &cpunum
);
1240 if (cpunum
>= num_possible_cpus())
1244 reboot_cpu
= cpunum
;
1248 static struct kobj_attribute reboot_cpu_attr
= __ATTR_RW(cpu
);
1251 static struct attribute
*reboot_attrs
[] = {
1252 &reboot_mode_attr
.attr
,
1254 &reboot_force_attr
.attr
,
1255 &reboot_type_attr
.attr
,
1258 &reboot_cpu_attr
.attr
,
1263 #ifdef CONFIG_SYSCTL
1264 static struct ctl_table kern_reboot_table
[] = {
1266 .procname
= "poweroff_cmd",
1267 .data
= &poweroff_cmd
,
1268 .maxlen
= POWEROFF_CMD_PATH_LEN
,
1270 .proc_handler
= proc_dostring
,
1273 .procname
= "ctrl-alt-del",
1275 .maxlen
= sizeof(int),
1277 .proc_handler
= proc_dointvec
,
1282 static void __init
kernel_reboot_sysctls_init(void)
1284 register_sysctl_init("kernel", kern_reboot_table
);
1287 #define kernel_reboot_sysctls_init() do { } while (0)
1288 #endif /* CONFIG_SYSCTL */
1290 static const struct attribute_group reboot_attr_group
= {
1291 .attrs
= reboot_attrs
,
1294 static int __init
reboot_ksysfs_init(void)
1296 struct kobject
*reboot_kobj
;
1299 reboot_kobj
= kobject_create_and_add("reboot", kernel_kobj
);
1303 ret
= sysfs_create_group(reboot_kobj
, &reboot_attr_group
);
1305 kobject_put(reboot_kobj
);
1309 kernel_reboot_sysctls_init();
1313 late_initcall(reboot_ksysfs_init
);