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 system_state = SYSTEM_RESTART;
78 machine_emergency_restart();
80 EXPORT_SYMBOL_GPL(emergency_restart);
82 void kernel_restart_prepare(char *cmd)
84 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
85 system_state = SYSTEM_RESTART;
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 * Notifier list for kernel code which wants to be called
249 * to prepare system for restart.
251 static BLOCKING_NOTIFIER_HEAD(restart_prep_handler_list);
253 static void do_kernel_restart_prepare(void)
255 blocking_notifier_call_chain(&restart_prep_handler_list, 0, NULL);
259 * kernel_restart - reboot the system
260 * @cmd: pointer to buffer containing command to execute for restart
263 * Shutdown everything and perform a clean reboot.
264 * This is not safe to call in interrupt context.
266 void kernel_restart(char *cmd)
268 kernel_restart_prepare(cmd);
269 do_kernel_restart_prepare();
270 migrate_to_reboot_cpu();
273 pr_emerg("Restarting system\n");
275 pr_emerg("Restarting system with command '%s'\n", cmd);
276 kmsg_dump(KMSG_DUMP_SHUTDOWN);
277 machine_restart(cmd);
279 EXPORT_SYMBOL_GPL(kernel_restart);
281 static void kernel_shutdown_prepare(enum system_states state)
283 blocking_notifier_call_chain(&reboot_notifier_list,
284 (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
285 system_state = state;
286 usermodehelper_disable();
290 * kernel_halt - halt the system
292 * Shutdown everything and perform a clean system halt.
294 void kernel_halt(void)
296 kernel_shutdown_prepare(SYSTEM_HALT);
297 migrate_to_reboot_cpu();
299 pr_emerg("System halted\n");
300 kmsg_dump(KMSG_DUMP_SHUTDOWN);
303 EXPORT_SYMBOL_GPL(kernel_halt);
306 * Notifier list for kernel code which wants to be called
307 * to prepare system for power off.
309 static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
312 * Notifier list for kernel code which wants to be called
313 * to power off system.
315 static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
317 static int sys_off_notify(struct notifier_block *nb,
318 unsigned long mode, void *cmd)
320 struct sys_off_handler *handler;
321 struct sys_off_data data = {};
323 handler = container_of(nb, struct sys_off_handler, nb);
324 data.cb_data = handler->cb_data;
328 return handler->sys_off_cb(&data);
331 static struct sys_off_handler platform_sys_off_handler;
333 static struct sys_off_handler *alloc_sys_off_handler(int priority)
335 struct sys_off_handler *handler;
339 * Platforms like m68k can't allocate sys_off handler dynamically
340 * at the early boot time because memory allocator isn't available yet.
342 if (priority == SYS_OFF_PRIO_PLATFORM) {
343 handler = &platform_sys_off_handler;
344 if (handler->cb_data)
345 return ERR_PTR(-EBUSY);
347 if (system_state > SYSTEM_RUNNING)
352 handler = kzalloc(sizeof(*handler), flags);
354 return ERR_PTR(-ENOMEM);
360 static void free_sys_off_handler(struct sys_off_handler *handler)
362 if (handler == &platform_sys_off_handler)
363 memset(handler, 0, sizeof(*handler));
369 * register_sys_off_handler - Register sys-off handler
370 * @mode: Sys-off mode
371 * @priority: Handler priority
372 * @callback: Callback function
373 * @cb_data: Callback argument
375 * Registers system power-off or restart handler that will be invoked
376 * at the step corresponding to the given sys-off mode. Handler's callback
377 * should return NOTIFY_DONE to permit execution of the next handler in
378 * the call chain or NOTIFY_STOP to break the chain (in error case for
381 * Multiple handlers can be registered at the default priority level.
383 * Only one handler can be registered at the non-default priority level,
384 * otherwise ERR_PTR(-EBUSY) is returned.
386 * Returns a new instance of struct sys_off_handler on success, or
387 * an ERR_PTR()-encoded error code otherwise.
389 struct sys_off_handler *
390 register_sys_off_handler(enum sys_off_mode mode,
392 int (*callback)(struct sys_off_data *data),
395 struct sys_off_handler *handler;
398 handler = alloc_sys_off_handler(priority);
403 case SYS_OFF_MODE_POWER_OFF_PREPARE:
404 handler->list = &power_off_prep_handler_list;
405 handler->blocking = true;
408 case SYS_OFF_MODE_POWER_OFF:
409 handler->list = &power_off_handler_list;
412 case SYS_OFF_MODE_RESTART_PREPARE:
413 handler->list = &restart_prep_handler_list;
414 handler->blocking = true;
417 case SYS_OFF_MODE_RESTART:
418 handler->list = &restart_handler_list;
422 free_sys_off_handler(handler);
423 return ERR_PTR(-EINVAL);
426 handler->nb.notifier_call = sys_off_notify;
427 handler->nb.priority = priority;
428 handler->sys_off_cb = callback;
429 handler->cb_data = cb_data;
430 handler->mode = mode;
432 if (handler->blocking) {
433 if (priority == SYS_OFF_PRIO_DEFAULT)
434 err = blocking_notifier_chain_register(handler->list,
437 err = blocking_notifier_chain_register_unique_prio(handler->list,
440 if (priority == SYS_OFF_PRIO_DEFAULT)
441 err = atomic_notifier_chain_register(handler->list,
444 err = atomic_notifier_chain_register_unique_prio(handler->list,
449 free_sys_off_handler(handler);
455 EXPORT_SYMBOL_GPL(register_sys_off_handler);
458 * unregister_sys_off_handler - Unregister sys-off handler
459 * @handler: Sys-off handler
461 * Unregisters given sys-off handler.
463 void unregister_sys_off_handler(struct sys_off_handler *handler)
467 if (IS_ERR_OR_NULL(handler))
470 if (handler->blocking)
471 err = blocking_notifier_chain_unregister(handler->list,
474 err = atomic_notifier_chain_unregister(handler->list,
477 /* sanity check, shall never happen */
480 free_sys_off_handler(handler);
482 EXPORT_SYMBOL_GPL(unregister_sys_off_handler);
484 static void devm_unregister_sys_off_handler(void *data)
486 struct sys_off_handler *handler = data;
488 unregister_sys_off_handler(handler);
492 * devm_register_sys_off_handler - Register sys-off handler
493 * @dev: Device that registers handler
494 * @mode: Sys-off mode
495 * @priority: Handler priority
496 * @callback: Callback function
497 * @cb_data: Callback argument
499 * Registers resource-managed sys-off handler.
501 * Returns zero on success, or error code on failure.
503 int devm_register_sys_off_handler(struct device *dev,
504 enum sys_off_mode mode,
506 int (*callback)(struct sys_off_data *data),
509 struct sys_off_handler *handler;
511 handler = register_sys_off_handler(mode, priority, callback, cb_data);
513 return PTR_ERR(handler);
515 return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler,
518 EXPORT_SYMBOL_GPL(devm_register_sys_off_handler);
521 * devm_register_power_off_handler - Register power-off handler
522 * @dev: Device that registers callback
523 * @callback: Callback function
524 * @cb_data: Callback's argument
526 * Registers resource-managed sys-off handler with a default priority
527 * and using power-off mode.
529 * Returns zero on success, or error code on failure.
531 int devm_register_power_off_handler(struct device *dev,
532 int (*callback)(struct sys_off_data *data),
535 return devm_register_sys_off_handler(dev,
536 SYS_OFF_MODE_POWER_OFF,
537 SYS_OFF_PRIO_DEFAULT,
540 EXPORT_SYMBOL_GPL(devm_register_power_off_handler);
543 * devm_register_restart_handler - Register restart handler
544 * @dev: Device that registers callback
545 * @callback: Callback function
546 * @cb_data: Callback's argument
548 * Registers resource-managed sys-off handler with a default priority
549 * and using restart mode.
551 * Returns zero on success, or error code on failure.
553 int devm_register_restart_handler(struct device *dev,
554 int (*callback)(struct sys_off_data *data),
557 return devm_register_sys_off_handler(dev,
558 SYS_OFF_MODE_RESTART,
559 SYS_OFF_PRIO_DEFAULT,
562 EXPORT_SYMBOL_GPL(devm_register_restart_handler);
564 static struct sys_off_handler *platform_power_off_handler;
566 static int platform_power_off_notify(struct sys_off_data *data)
568 void (*platform_power_power_off_cb)(void) = data->cb_data;
570 platform_power_power_off_cb();
576 * register_platform_power_off - Register platform-level power-off callback
577 * @power_off: Power-off callback
579 * Registers power-off callback that will be called as last step
580 * of the power-off sequence. This callback is expected to be invoked
581 * for the last resort. Only one platform power-off callback is allowed
582 * to be registered at a time.
584 * Returns zero on success, or error code on failure.
586 int register_platform_power_off(void (*power_off)(void))
588 struct sys_off_handler *handler;
590 handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
591 SYS_OFF_PRIO_PLATFORM,
592 platform_power_off_notify,
595 return PTR_ERR(handler);
597 platform_power_off_handler = handler;
601 EXPORT_SYMBOL_GPL(register_platform_power_off);
604 * unregister_platform_power_off - Unregister platform-level power-off callback
605 * @power_off: Power-off callback
607 * Unregisters previously registered platform power-off callback.
609 void unregister_platform_power_off(void (*power_off)(void))
611 if (platform_power_off_handler &&
612 platform_power_off_handler->cb_data == power_off) {
613 unregister_sys_off_handler(platform_power_off_handler);
614 platform_power_off_handler = NULL;
617 EXPORT_SYMBOL_GPL(unregister_platform_power_off);
619 static int legacy_pm_power_off(struct sys_off_data *data)
627 static void do_kernel_power_off_prepare(void)
629 blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL);
633 * do_kernel_power_off - Execute kernel power-off handler call chain
635 * Expected to be called as last step of the power-off sequence.
637 * Powers off the system immediately if a power-off handler function has
638 * been registered. Otherwise does nothing.
640 void do_kernel_power_off(void)
642 struct sys_off_handler *sys_off = NULL;
645 * Register sys-off handlers for legacy PM callback. This allows
646 * legacy PM callbacks temporary co-exist with the new sys-off API.
648 * TODO: Remove legacy handlers once all legacy PM users will be
649 * switched to the sys-off based APIs.
652 sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
653 SYS_OFF_PRIO_DEFAULT,
654 legacy_pm_power_off, NULL);
656 atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
658 unregister_sys_off_handler(sys_off);
662 * kernel_can_power_off - check whether system can be powered off
664 * Returns true if power-off handler is registered and system can be
665 * powered off, false otherwise.
667 bool kernel_can_power_off(void)
669 return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) ||
672 EXPORT_SYMBOL_GPL(kernel_can_power_off);
675 * kernel_power_off - power_off the system
677 * Shutdown everything and perform a clean system power_off.
679 void kernel_power_off(void)
681 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
682 do_kernel_power_off_prepare();
683 migrate_to_reboot_cpu();
685 pr_emerg("Power down\n");
686 kmsg_dump(KMSG_DUMP_SHUTDOWN);
689 EXPORT_SYMBOL_GPL(kernel_power_off);
691 DEFINE_MUTEX(system_transition_mutex);
694 * Reboot system call: for obvious reasons only root may call it,
695 * and even root needs to set up some magic numbers in the registers
696 * so that some mistake won't make this reboot the whole machine.
697 * You can also set the meaning of the ctrl-alt-del-key here.
699 * reboot doesn't sync: do that yourself before calling this.
701 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
704 struct pid_namespace *pid_ns = task_active_pid_ns(current);
708 /* We only trust the superuser with rebooting the system. */
709 if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
712 /* For safety, we require "magic" arguments. */
713 if (magic1 != LINUX_REBOOT_MAGIC1 ||
714 (magic2 != LINUX_REBOOT_MAGIC2 &&
715 magic2 != LINUX_REBOOT_MAGIC2A &&
716 magic2 != LINUX_REBOOT_MAGIC2B &&
717 magic2 != LINUX_REBOOT_MAGIC2C))
721 * If pid namespaces are enabled and the current task is in a child
722 * pid_namespace, the command is handled by reboot_pid_ns() which will
725 ret = reboot_pid_ns(pid_ns, cmd);
729 /* Instead of trying to make the power_off code look like
730 * halt when pm_power_off is not set do it the easy way.
732 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off())
733 cmd = LINUX_REBOOT_CMD_HALT;
735 mutex_lock(&system_transition_mutex);
737 case LINUX_REBOOT_CMD_RESTART:
738 kernel_restart(NULL);
741 case LINUX_REBOOT_CMD_CAD_ON:
745 case LINUX_REBOOT_CMD_CAD_OFF:
749 case LINUX_REBOOT_CMD_HALT:
753 case LINUX_REBOOT_CMD_POWER_OFF:
758 case LINUX_REBOOT_CMD_RESTART2:
759 ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
764 buffer[sizeof(buffer) - 1] = '\0';
766 kernel_restart(buffer);
769 #ifdef CONFIG_KEXEC_CORE
770 case LINUX_REBOOT_CMD_KEXEC:
771 ret = kernel_kexec();
775 #ifdef CONFIG_HIBERNATION
776 case LINUX_REBOOT_CMD_SW_SUSPEND:
785 mutex_unlock(&system_transition_mutex);
789 static void deferred_cad(struct work_struct *dummy)
791 kernel_restart(NULL);
795 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
796 * As it's called within an interrupt, it may NOT sync: the only choice
797 * is whether to reboot at once, or just ignore the ctrl-alt-del.
799 void ctrl_alt_del(void)
801 static DECLARE_WORK(cad_work, deferred_cad);
804 schedule_work(&cad_work);
806 kill_cad_pid(SIGINT, 1);
809 #define POWEROFF_CMD_PATH_LEN 256
810 static char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
811 static const char reboot_cmd[] = "/sbin/reboot";
813 static int run_cmd(const char *cmd)
816 static char *envp[] = {
818 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
822 argv = argv_split(GFP_KERNEL, cmd, NULL);
824 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
833 static int __orderly_reboot(void)
837 ret = run_cmd(reboot_cmd);
840 pr_warn("Failed to start orderly reboot: forcing the issue\n");
842 kernel_restart(NULL);
848 static int __orderly_poweroff(bool force)
852 ret = run_cmd(poweroff_cmd);
855 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
858 * I guess this should try to kick off some daemon to sync and
859 * poweroff asap. Or not even bother syncing if we're doing an
860 * emergency shutdown?
869 static bool poweroff_force;
871 static void poweroff_work_func(struct work_struct *work)
873 __orderly_poweroff(poweroff_force);
876 static DECLARE_WORK(poweroff_work, poweroff_work_func);
879 * orderly_poweroff - Trigger an orderly system poweroff
880 * @force: force poweroff if command execution fails
882 * This may be called from any context to trigger a system shutdown.
883 * If the orderly shutdown fails, it will force an immediate shutdown.
885 void orderly_poweroff(bool force)
887 if (force) /* do not override the pending "true" */
888 poweroff_force = true;
889 schedule_work(&poweroff_work);
891 EXPORT_SYMBOL_GPL(orderly_poweroff);
893 static void reboot_work_func(struct work_struct *work)
898 static DECLARE_WORK(reboot_work, reboot_work_func);
901 * orderly_reboot - Trigger an orderly system reboot
903 * This may be called from any context to trigger a system reboot.
904 * If the orderly reboot fails, it will force an immediate reboot.
906 void orderly_reboot(void)
908 schedule_work(&reboot_work);
910 EXPORT_SYMBOL_GPL(orderly_reboot);
913 * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
914 * @work: work_struct associated with the emergency poweroff function
916 * This function is called in very critical situations to force
917 * a kernel poweroff after a configurable timeout value.
919 static void hw_failure_emergency_poweroff_func(struct work_struct *work)
922 * We have reached here after the emergency shutdown waiting period has
923 * expired. This means orderly_poweroff has not been able to shut off
924 * the system for some reason.
926 * Try to shut down the system immediately using kernel_power_off
929 pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
933 * Worst of the worst case trigger emergency restart
935 pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
939 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
940 hw_failure_emergency_poweroff_func);
943 * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
945 * This may be called from any critical situation to trigger a system shutdown
946 * after a given period of time. If time is negative this is not scheduled.
948 static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
950 if (poweroff_delay_ms <= 0)
952 schedule_delayed_work(&hw_failure_emergency_poweroff_work,
953 msecs_to_jiffies(poweroff_delay_ms));
957 * hw_protection_shutdown - Trigger an emergency system poweroff
959 * @reason: Reason of emergency shutdown to be printed.
960 * @ms_until_forced: Time to wait for orderly shutdown before tiggering a
961 * forced shudown. Negative value disables the forced
964 * Initiate an emergency system shutdown in order to protect hardware from
965 * further damage. Usage examples include a thermal protection or a voltage or
966 * current regulator failures.
967 * NOTE: The request is ignored if protection shutdown is already pending even
968 * if the previous request has given a large timeout for forced shutdown.
969 * Can be called from any context.
971 void hw_protection_shutdown(const char *reason, int ms_until_forced)
973 static atomic_t allow_proceed = ATOMIC_INIT(1);
975 pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
977 /* Shutdown should be initiated only once. */
978 if (!atomic_dec_and_test(&allow_proceed))
982 * Queue a backup emergency shutdown in the event of
983 * orderly_poweroff failure
985 hw_failure_emergency_poweroff(ms_until_forced);
986 orderly_poweroff(true);
988 EXPORT_SYMBOL_GPL(hw_protection_shutdown);
990 static int __init reboot_setup(char *str)
993 enum reboot_mode *mode;
996 * Having anything passed on the command line via
997 * reboot= will cause us to disable DMI checking
1002 if (!strncmp(str, "panic_", 6)) {
1003 mode = &panic_reboot_mode;
1006 mode = &reboot_mode;
1011 *mode = REBOOT_WARM;
1015 *mode = REBOOT_COLD;
1019 *mode = REBOOT_HARD;
1024 * reboot_cpu is s[mp]#### with #### being the processor
1025 * to be used for rebooting. Skip 's' or 'smp' prefix.
1027 str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
1029 if (isdigit(str[0])) {
1030 int cpu = simple_strtoul(str, NULL, 0);
1032 if (cpu >= num_possible_cpus()) {
1033 pr_err("Ignoring the CPU number in reboot= option. "
1034 "CPU %d exceeds possible cpu number %d\n",
1035 cpu, num_possible_cpus());
1040 *mode = REBOOT_SOFT;
1044 *mode = REBOOT_GPIO;
1061 str = strchr(str, ',');
1069 __setup("reboot=", reboot_setup);
1073 #define REBOOT_COLD_STR "cold"
1074 #define REBOOT_WARM_STR "warm"
1075 #define REBOOT_HARD_STR "hard"
1076 #define REBOOT_SOFT_STR "soft"
1077 #define REBOOT_GPIO_STR "gpio"
1078 #define REBOOT_UNDEFINED_STR "undefined"
1080 #define BOOT_TRIPLE_STR "triple"
1081 #define BOOT_KBD_STR "kbd"
1082 #define BOOT_BIOS_STR "bios"
1083 #define BOOT_ACPI_STR "acpi"
1084 #define BOOT_EFI_STR "efi"
1085 #define BOOT_PCI_STR "pci"
1087 static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1091 switch (reboot_mode) {
1093 val = REBOOT_COLD_STR;
1096 val = REBOOT_WARM_STR;
1099 val = REBOOT_HARD_STR;
1102 val = REBOOT_SOFT_STR;
1105 val = REBOOT_GPIO_STR;
1108 val = REBOOT_UNDEFINED_STR;
1111 return sprintf(buf, "%s\n", val);
1113 static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
1114 const char *buf, size_t count)
1116 if (!capable(CAP_SYS_BOOT))
1119 if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
1120 reboot_mode = REBOOT_COLD;
1121 else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
1122 reboot_mode = REBOOT_WARM;
1123 else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
1124 reboot_mode = REBOOT_HARD;
1125 else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
1126 reboot_mode = REBOOT_SOFT;
1127 else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
1128 reboot_mode = REBOOT_GPIO;
1136 static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
1139 static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1141 return sprintf(buf, "%d\n", reboot_force);
1143 static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
1144 const char *buf, size_t count)
1148 if (!capable(CAP_SYS_BOOT))
1151 if (kstrtobool(buf, &res))
1159 static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
1161 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1165 switch (reboot_type) {
1167 val = BOOT_TRIPLE_STR;
1173 val = BOOT_BIOS_STR;
1176 val = BOOT_ACPI_STR;
1181 case BOOT_CF9_FORCE:
1185 val = REBOOT_UNDEFINED_STR;
1188 return sprintf(buf, "%s\n", val);
1190 static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
1191 const char *buf, size_t count)
1193 if (!capable(CAP_SYS_BOOT))
1196 if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
1197 reboot_type = BOOT_TRIPLE;
1198 else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
1199 reboot_type = BOOT_KBD;
1200 else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
1201 reboot_type = BOOT_BIOS;
1202 else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
1203 reboot_type = BOOT_ACPI;
1204 else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
1205 reboot_type = BOOT_EFI;
1206 else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
1207 reboot_type = BOOT_CF9_FORCE;
1215 static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
1219 static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1221 return sprintf(buf, "%d\n", reboot_cpu);
1223 static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
1224 const char *buf, size_t count)
1226 unsigned int cpunum;
1229 if (!capable(CAP_SYS_BOOT))
1232 rc = kstrtouint(buf, 0, &cpunum);
1237 if (cpunum >= num_possible_cpus())
1241 reboot_cpu = cpunum;
1245 static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
1248 static struct attribute *reboot_attrs[] = {
1249 &reboot_mode_attr.attr,
1251 &reboot_force_attr.attr,
1252 &reboot_type_attr.attr,
1255 &reboot_cpu_attr.attr,
1260 #ifdef CONFIG_SYSCTL
1261 static struct ctl_table kern_reboot_table[] = {
1263 .procname = "poweroff_cmd",
1264 .data = &poweroff_cmd,
1265 .maxlen = POWEROFF_CMD_PATH_LEN,
1267 .proc_handler = proc_dostring,
1270 .procname = "ctrl-alt-del",
1272 .maxlen = sizeof(int),
1274 .proc_handler = proc_dointvec,
1279 static void __init kernel_reboot_sysctls_init(void)
1281 register_sysctl_init("kernel", kern_reboot_table);
1284 #define kernel_reboot_sysctls_init() do { } while (0)
1285 #endif /* CONFIG_SYSCTL */
1287 static const struct attribute_group reboot_attr_group = {
1288 .attrs = reboot_attrs,
1291 static int __init reboot_ksysfs_init(void)
1293 struct kobject *reboot_kobj;
1296 reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
1300 ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
1302 kobject_put(reboot_kobj);
1306 kernel_reboot_sysctls_init();
1310 late_initcall(reboot_ksysfs_init);