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 usermodehelper_disable();
90 * register_reboot_notifier - Register function to be called at reboot time
91 * @nb: Info about notifier function to be called
93 * Registers a function with the list of functions
94 * to be called at reboot time.
96 * Currently always returns zero, as blocking_notifier_chain_register()
97 * always returns zero.
99 int register_reboot_notifier(struct notifier_block *nb)
101 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
103 EXPORT_SYMBOL(register_reboot_notifier);
106 * unregister_reboot_notifier - Unregister previously registered reboot notifier
107 * @nb: Hook to be unregistered
109 * Unregisters a previously registered reboot
112 * Returns zero on success, or %-ENOENT on failure.
114 int unregister_reboot_notifier(struct notifier_block *nb)
116 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
118 EXPORT_SYMBOL(unregister_reboot_notifier);
120 static void devm_unregister_reboot_notifier(struct device *dev, void *res)
122 WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
125 int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
127 struct notifier_block **rcnb;
130 rcnb = devres_alloc(devm_unregister_reboot_notifier,
131 sizeof(*rcnb), GFP_KERNEL);
135 ret = register_reboot_notifier(nb);
138 devres_add(dev, rcnb);
145 EXPORT_SYMBOL(devm_register_reboot_notifier);
148 * Notifier list for kernel code which wants to be called
149 * to restart the system.
151 static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
154 * register_restart_handler - Register function to be called to reset
156 * @nb: Info about handler function to be called
157 * @nb->priority: Handler priority. Handlers should follow the
158 * following guidelines for setting priorities.
159 * 0: Restart handler of last resort,
160 * with limited restart capabilities
161 * 128: Default restart handler; use if no other
162 * restart handler is expected to be available,
163 * and/or if restart functionality is
164 * sufficient to restart the entire system
165 * 255: Highest priority restart handler, will
166 * preempt all other restart handlers
168 * Registers a function with code to be called to restart the
171 * Registered functions will be called from machine_restart as last
172 * step of the restart sequence (if the architecture specific
173 * machine_restart function calls do_kernel_restart - see below
175 * Registered functions are expected to restart the system immediately.
176 * If more than one function is registered, the restart handler priority
177 * selects which function will be called first.
179 * Restart handlers are expected to be registered from non-architecture
180 * code, typically from drivers. A typical use case would be a system
181 * where restart functionality is provided through a watchdog. Multiple
182 * restart handlers may exist; for example, one restart handler might
183 * restart the entire system, while another only restarts the CPU.
184 * In such cases, the restart handler which only restarts part of the
185 * hardware is expected to register with low priority to ensure that
186 * it only runs if no other means to restart the system is available.
188 * Currently always returns zero, as atomic_notifier_chain_register()
189 * always returns zero.
191 int register_restart_handler(struct notifier_block *nb)
193 return atomic_notifier_chain_register(&restart_handler_list, nb);
195 EXPORT_SYMBOL(register_restart_handler);
198 * unregister_restart_handler - Unregister previously registered
200 * @nb: Hook to be unregistered
202 * Unregisters a previously registered restart handler function.
204 * Returns zero on success, or %-ENOENT on failure.
206 int unregister_restart_handler(struct notifier_block *nb)
208 return atomic_notifier_chain_unregister(&restart_handler_list, nb);
210 EXPORT_SYMBOL(unregister_restart_handler);
213 * do_kernel_restart - Execute kernel restart handler call chain
215 * Calls functions registered with register_restart_handler.
217 * Expected to be called from machine_restart as last step of the restart
220 * Restarts the system immediately if a restart handler function has been
221 * registered. Otherwise does nothing.
223 void do_kernel_restart(char *cmd)
225 atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
228 void migrate_to_reboot_cpu(void)
230 /* The boot cpu is always logical cpu 0 */
231 int cpu = reboot_cpu;
233 cpu_hotplug_disable();
235 /* Make certain the cpu I'm about to reboot on is online */
236 if (!cpu_online(cpu))
237 cpu = cpumask_first(cpu_online_mask);
239 /* Prevent races with other tasks migrating this task */
240 current->flags |= PF_NO_SETAFFINITY;
242 /* Make certain I only run on the appropriate processor */
243 set_cpus_allowed_ptr(current, cpumask_of(cpu));
247 * kernel_restart - reboot the system
248 * @cmd: pointer to buffer containing command to execute for restart
251 * Shutdown everything and perform a clean reboot.
252 * This is not safe to call in interrupt context.
254 void kernel_restart(char *cmd)
256 kernel_restart_prepare(cmd);
257 migrate_to_reboot_cpu();
260 pr_emerg("Restarting system\n");
262 pr_emerg("Restarting system with command '%s'\n", cmd);
263 kmsg_dump(KMSG_DUMP_SHUTDOWN);
264 machine_restart(cmd);
266 EXPORT_SYMBOL_GPL(kernel_restart);
268 static void kernel_shutdown_prepare(enum system_states state)
270 blocking_notifier_call_chain(&reboot_notifier_list,
271 (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
272 system_state = state;
273 usermodehelper_disable();
277 * kernel_halt - halt the system
279 * Shutdown everything and perform a clean system halt.
281 void kernel_halt(void)
283 kernel_shutdown_prepare(SYSTEM_HALT);
284 migrate_to_reboot_cpu();
286 pr_emerg("System halted\n");
287 kmsg_dump(KMSG_DUMP_SHUTDOWN);
290 EXPORT_SYMBOL_GPL(kernel_halt);
293 * Notifier list for kernel code which wants to be called
294 * to prepare system for power off.
296 static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
299 * Notifier list for kernel code which wants to be called
300 * to power off system.
302 static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
304 static int sys_off_notify(struct notifier_block *nb,
305 unsigned long mode, void *cmd)
307 struct sys_off_handler *handler;
308 struct sys_off_data data = {};
310 handler = container_of(nb, struct sys_off_handler, nb);
311 data.cb_data = handler->cb_data;
315 return handler->sys_off_cb(&data);
318 static struct sys_off_handler platform_sys_off_handler;
320 static struct sys_off_handler *alloc_sys_off_handler(int priority)
322 struct sys_off_handler *handler;
326 * Platforms like m68k can't allocate sys_off handler dynamically
327 * at the early boot time because memory allocator isn't available yet.
329 if (priority == SYS_OFF_PRIO_PLATFORM) {
330 handler = &platform_sys_off_handler;
331 if (handler->cb_data)
332 return ERR_PTR(-EBUSY);
334 if (system_state > SYSTEM_RUNNING)
339 handler = kzalloc(sizeof(*handler), flags);
341 return ERR_PTR(-ENOMEM);
347 static void free_sys_off_handler(struct sys_off_handler *handler)
349 if (handler == &platform_sys_off_handler)
350 memset(handler, 0, sizeof(*handler));
356 * register_sys_off_handler - Register sys-off handler
357 * @mode: Sys-off mode
358 * @priority: Handler priority
359 * @callback: Callback function
360 * @cb_data: Callback argument
362 * Registers system power-off or restart handler that will be invoked
363 * at the step corresponding to the given sys-off mode. Handler's callback
364 * should return NOTIFY_DONE to permit execution of the next handler in
365 * the call chain or NOTIFY_STOP to break the chain (in error case for
368 * Multiple handlers can be registered at the default priority level.
370 * Only one handler can be registered at the non-default priority level,
371 * otherwise ERR_PTR(-EBUSY) is returned.
373 * Returns a new instance of struct sys_off_handler on success, or
374 * an ERR_PTR()-encoded error code otherwise.
376 struct sys_off_handler *
377 register_sys_off_handler(enum sys_off_mode mode,
379 int (*callback)(struct sys_off_data *data),
382 struct sys_off_handler *handler;
385 handler = alloc_sys_off_handler(priority);
390 case SYS_OFF_MODE_POWER_OFF_PREPARE:
391 handler->list = &power_off_prep_handler_list;
392 handler->blocking = true;
395 case SYS_OFF_MODE_POWER_OFF:
396 handler->list = &power_off_handler_list;
399 case SYS_OFF_MODE_RESTART:
400 handler->list = &restart_handler_list;
404 free_sys_off_handler(handler);
405 return ERR_PTR(-EINVAL);
408 handler->nb.notifier_call = sys_off_notify;
409 handler->nb.priority = priority;
410 handler->sys_off_cb = callback;
411 handler->cb_data = cb_data;
412 handler->mode = mode;
414 if (handler->blocking) {
415 if (priority == SYS_OFF_PRIO_DEFAULT)
416 err = blocking_notifier_chain_register(handler->list,
419 err = blocking_notifier_chain_register_unique_prio(handler->list,
422 if (priority == SYS_OFF_PRIO_DEFAULT)
423 err = atomic_notifier_chain_register(handler->list,
426 err = atomic_notifier_chain_register_unique_prio(handler->list,
431 free_sys_off_handler(handler);
437 EXPORT_SYMBOL_GPL(register_sys_off_handler);
440 * unregister_sys_off_handler - Unregister sys-off handler
441 * @handler: Sys-off handler
443 * Unregisters given sys-off handler.
445 void unregister_sys_off_handler(struct sys_off_handler *handler)
449 if (IS_ERR_OR_NULL(handler))
452 if (handler->blocking)
453 err = blocking_notifier_chain_unregister(handler->list,
456 err = atomic_notifier_chain_unregister(handler->list,
459 /* sanity check, shall never happen */
462 free_sys_off_handler(handler);
464 EXPORT_SYMBOL_GPL(unregister_sys_off_handler);
466 static void devm_unregister_sys_off_handler(void *data)
468 struct sys_off_handler *handler = data;
470 unregister_sys_off_handler(handler);
474 * devm_register_sys_off_handler - Register sys-off handler
475 * @dev: Device that registers handler
476 * @mode: Sys-off mode
477 * @priority: Handler priority
478 * @callback: Callback function
479 * @cb_data: Callback argument
481 * Registers resource-managed sys-off handler.
483 * Returns zero on success, or error code on failure.
485 int devm_register_sys_off_handler(struct device *dev,
486 enum sys_off_mode mode,
488 int (*callback)(struct sys_off_data *data),
491 struct sys_off_handler *handler;
493 handler = register_sys_off_handler(mode, priority, callback, cb_data);
495 return PTR_ERR(handler);
497 return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler,
500 EXPORT_SYMBOL_GPL(devm_register_sys_off_handler);
503 * devm_register_power_off_handler - Register power-off handler
504 * @dev: Device that registers callback
505 * @callback: Callback function
506 * @cb_data: Callback's argument
508 * Registers resource-managed sys-off handler with a default priority
509 * and using power-off mode.
511 * Returns zero on success, or error code on failure.
513 int devm_register_power_off_handler(struct device *dev,
514 int (*callback)(struct sys_off_data *data),
517 return devm_register_sys_off_handler(dev,
518 SYS_OFF_MODE_POWER_OFF,
519 SYS_OFF_PRIO_DEFAULT,
522 EXPORT_SYMBOL_GPL(devm_register_power_off_handler);
525 * devm_register_restart_handler - Register restart handler
526 * @dev: Device that registers callback
527 * @callback: Callback function
528 * @cb_data: Callback's argument
530 * Registers resource-managed sys-off handler with a default priority
531 * and using restart mode.
533 * Returns zero on success, or error code on failure.
535 int devm_register_restart_handler(struct device *dev,
536 int (*callback)(struct sys_off_data *data),
539 return devm_register_sys_off_handler(dev,
540 SYS_OFF_MODE_RESTART,
541 SYS_OFF_PRIO_DEFAULT,
544 EXPORT_SYMBOL_GPL(devm_register_restart_handler);
546 static struct sys_off_handler *platform_power_off_handler;
548 static int platform_power_off_notify(struct sys_off_data *data)
550 void (*platform_power_power_off_cb)(void) = data->cb_data;
552 platform_power_power_off_cb();
558 * register_platform_power_off - Register platform-level power-off callback
559 * @power_off: Power-off callback
561 * Registers power-off callback that will be called as last step
562 * of the power-off sequence. This callback is expected to be invoked
563 * for the last resort. Only one platform power-off callback is allowed
564 * to be registered at a time.
566 * Returns zero on success, or error code on failure.
568 int register_platform_power_off(void (*power_off)(void))
570 struct sys_off_handler *handler;
572 handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
573 SYS_OFF_PRIO_PLATFORM,
574 platform_power_off_notify,
577 return PTR_ERR(handler);
579 platform_power_off_handler = handler;
583 EXPORT_SYMBOL_GPL(register_platform_power_off);
586 * unregister_platform_power_off - Unregister platform-level power-off callback
587 * @power_off: Power-off callback
589 * Unregisters previously registered platform power-off callback.
591 void unregister_platform_power_off(void (*power_off)(void))
593 if (platform_power_off_handler &&
594 platform_power_off_handler->cb_data == power_off) {
595 unregister_sys_off_handler(platform_power_off_handler);
596 platform_power_off_handler = NULL;
599 EXPORT_SYMBOL_GPL(unregister_platform_power_off);
601 static int legacy_pm_power_off(struct sys_off_data *data)
609 static void do_kernel_power_off_prepare(void)
611 blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL);
615 * do_kernel_power_off - Execute kernel power-off handler call chain
617 * Expected to be called as last step of the power-off sequence.
619 * Powers off the system immediately if a power-off handler function has
620 * been registered. Otherwise does nothing.
622 void do_kernel_power_off(void)
624 struct sys_off_handler *sys_off = NULL;
627 * Register sys-off handlers for legacy PM callback. This allows
628 * legacy PM callbacks temporary co-exist with the new sys-off API.
630 * TODO: Remove legacy handlers once all legacy PM users will be
631 * switched to the sys-off based APIs.
634 sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
635 SYS_OFF_PRIO_DEFAULT,
636 legacy_pm_power_off, NULL);
638 atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
640 unregister_sys_off_handler(sys_off);
644 * kernel_can_power_off - check whether system can be powered off
646 * Returns true if power-off handler is registered and system can be
647 * powered off, false otherwise.
649 bool kernel_can_power_off(void)
651 return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) ||
654 EXPORT_SYMBOL_GPL(kernel_can_power_off);
657 * kernel_power_off - power_off the system
659 * Shutdown everything and perform a clean system power_off.
661 void kernel_power_off(void)
663 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
664 do_kernel_power_off_prepare();
665 migrate_to_reboot_cpu();
667 pr_emerg("Power down\n");
668 kmsg_dump(KMSG_DUMP_SHUTDOWN);
671 EXPORT_SYMBOL_GPL(kernel_power_off);
673 DEFINE_MUTEX(system_transition_mutex);
676 * Reboot system call: for obvious reasons only root may call it,
677 * and even root needs to set up some magic numbers in the registers
678 * so that some mistake won't make this reboot the whole machine.
679 * You can also set the meaning of the ctrl-alt-del-key here.
681 * reboot doesn't sync: do that yourself before calling this.
683 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
686 struct pid_namespace *pid_ns = task_active_pid_ns(current);
690 /* We only trust the superuser with rebooting the system. */
691 if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
694 /* For safety, we require "magic" arguments. */
695 if (magic1 != LINUX_REBOOT_MAGIC1 ||
696 (magic2 != LINUX_REBOOT_MAGIC2 &&
697 magic2 != LINUX_REBOOT_MAGIC2A &&
698 magic2 != LINUX_REBOOT_MAGIC2B &&
699 magic2 != LINUX_REBOOT_MAGIC2C))
703 * If pid namespaces are enabled and the current task is in a child
704 * pid_namespace, the command is handled by reboot_pid_ns() which will
707 ret = reboot_pid_ns(pid_ns, cmd);
711 /* Instead of trying to make the power_off code look like
712 * halt when pm_power_off is not set do it the easy way.
714 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off())
715 cmd = LINUX_REBOOT_CMD_HALT;
717 mutex_lock(&system_transition_mutex);
719 case LINUX_REBOOT_CMD_RESTART:
720 kernel_restart(NULL);
723 case LINUX_REBOOT_CMD_CAD_ON:
727 case LINUX_REBOOT_CMD_CAD_OFF:
731 case LINUX_REBOOT_CMD_HALT:
735 case LINUX_REBOOT_CMD_POWER_OFF:
740 case LINUX_REBOOT_CMD_RESTART2:
741 ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
746 buffer[sizeof(buffer) - 1] = '\0';
748 kernel_restart(buffer);
751 #ifdef CONFIG_KEXEC_CORE
752 case LINUX_REBOOT_CMD_KEXEC:
753 ret = kernel_kexec();
757 #ifdef CONFIG_HIBERNATION
758 case LINUX_REBOOT_CMD_SW_SUSPEND:
767 mutex_unlock(&system_transition_mutex);
771 static void deferred_cad(struct work_struct *dummy)
773 kernel_restart(NULL);
777 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
778 * As it's called within an interrupt, it may NOT sync: the only choice
779 * is whether to reboot at once, or just ignore the ctrl-alt-del.
781 void ctrl_alt_del(void)
783 static DECLARE_WORK(cad_work, deferred_cad);
786 schedule_work(&cad_work);
788 kill_cad_pid(SIGINT, 1);
791 #define POWEROFF_CMD_PATH_LEN 256
792 static char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
793 static const char reboot_cmd[] = "/sbin/reboot";
795 static int run_cmd(const char *cmd)
798 static char *envp[] = {
800 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
804 argv = argv_split(GFP_KERNEL, cmd, NULL);
806 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
815 static int __orderly_reboot(void)
819 ret = run_cmd(reboot_cmd);
822 printk_prefer_direct_enter();
823 pr_warn("Failed to start orderly reboot: forcing the issue\n");
825 kernel_restart(NULL);
826 printk_prefer_direct_exit();
832 static int __orderly_poweroff(bool force)
836 ret = run_cmd(poweroff_cmd);
839 printk_prefer_direct_enter();
840 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
843 * I guess this should try to kick off some daemon to sync and
844 * poweroff asap. Or not even bother syncing if we're doing an
845 * emergency shutdown?
849 printk_prefer_direct_exit();
855 static bool poweroff_force;
857 static void poweroff_work_func(struct work_struct *work)
859 __orderly_poweroff(poweroff_force);
862 static DECLARE_WORK(poweroff_work, poweroff_work_func);
865 * orderly_poweroff - Trigger an orderly system poweroff
866 * @force: force poweroff if command execution fails
868 * This may be called from any context to trigger a system shutdown.
869 * If the orderly shutdown fails, it will force an immediate shutdown.
871 void orderly_poweroff(bool force)
873 if (force) /* do not override the pending "true" */
874 poweroff_force = true;
875 schedule_work(&poweroff_work);
877 EXPORT_SYMBOL_GPL(orderly_poweroff);
879 static void reboot_work_func(struct work_struct *work)
884 static DECLARE_WORK(reboot_work, reboot_work_func);
887 * orderly_reboot - Trigger an orderly system reboot
889 * This may be called from any context to trigger a system reboot.
890 * If the orderly reboot fails, it will force an immediate reboot.
892 void orderly_reboot(void)
894 schedule_work(&reboot_work);
896 EXPORT_SYMBOL_GPL(orderly_reboot);
899 * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
900 * @work: work_struct associated with the emergency poweroff function
902 * This function is called in very critical situations to force
903 * a kernel poweroff after a configurable timeout value.
905 static void hw_failure_emergency_poweroff_func(struct work_struct *work)
907 printk_prefer_direct_enter();
910 * We have reached here after the emergency shutdown waiting period has
911 * expired. This means orderly_poweroff has not been able to shut off
912 * the system for some reason.
914 * Try to shut down the system immediately using kernel_power_off
917 pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
921 * Worst of the worst case trigger emergency restart
923 pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
926 printk_prefer_direct_exit();
929 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
930 hw_failure_emergency_poweroff_func);
933 * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
935 * This may be called from any critical situation to trigger a system shutdown
936 * after a given period of time. If time is negative this is not scheduled.
938 static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
940 if (poweroff_delay_ms <= 0)
942 schedule_delayed_work(&hw_failure_emergency_poweroff_work,
943 msecs_to_jiffies(poweroff_delay_ms));
947 * hw_protection_shutdown - Trigger an emergency system poweroff
949 * @reason: Reason of emergency shutdown to be printed.
950 * @ms_until_forced: Time to wait for orderly shutdown before tiggering a
951 * forced shudown. Negative value disables the forced
954 * Initiate an emergency system shutdown in order to protect hardware from
955 * further damage. Usage examples include a thermal protection or a voltage or
956 * current regulator failures.
957 * NOTE: The request is ignored if protection shutdown is already pending even
958 * if the previous request has given a large timeout for forced shutdown.
959 * Can be called from any context.
961 void hw_protection_shutdown(const char *reason, int ms_until_forced)
963 static atomic_t allow_proceed = ATOMIC_INIT(1);
965 printk_prefer_direct_enter();
967 pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
969 /* Shutdown should be initiated only once. */
970 if (!atomic_dec_and_test(&allow_proceed))
974 * Queue a backup emergency shutdown in the event of
975 * orderly_poweroff failure
977 hw_failure_emergency_poweroff(ms_until_forced);
978 orderly_poweroff(true);
980 printk_prefer_direct_exit();
982 EXPORT_SYMBOL_GPL(hw_protection_shutdown);
984 static int __init reboot_setup(char *str)
987 enum reboot_mode *mode;
990 * Having anything passed on the command line via
991 * reboot= will cause us to disable DMI checking
996 if (!strncmp(str, "panic_", 6)) {
997 mode = &panic_reboot_mode;
1000 mode = &reboot_mode;
1005 *mode = REBOOT_WARM;
1009 *mode = REBOOT_COLD;
1013 *mode = REBOOT_HARD;
1018 * reboot_cpu is s[mp]#### with #### being the processor
1019 * to be used for rebooting. Skip 's' or 'smp' prefix.
1021 str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
1023 if (isdigit(str[0])) {
1024 int cpu = simple_strtoul(str, NULL, 0);
1026 if (cpu >= num_possible_cpus()) {
1027 pr_err("Ignoring the CPU number in reboot= option. "
1028 "CPU %d exceeds possible cpu number %d\n",
1029 cpu, num_possible_cpus());
1034 *mode = REBOOT_SOFT;
1038 *mode = REBOOT_GPIO;
1055 str = strchr(str, ',');
1063 __setup("reboot=", reboot_setup);
1067 #define REBOOT_COLD_STR "cold"
1068 #define REBOOT_WARM_STR "warm"
1069 #define REBOOT_HARD_STR "hard"
1070 #define REBOOT_SOFT_STR "soft"
1071 #define REBOOT_GPIO_STR "gpio"
1072 #define REBOOT_UNDEFINED_STR "undefined"
1074 #define BOOT_TRIPLE_STR "triple"
1075 #define BOOT_KBD_STR "kbd"
1076 #define BOOT_BIOS_STR "bios"
1077 #define BOOT_ACPI_STR "acpi"
1078 #define BOOT_EFI_STR "efi"
1079 #define BOOT_PCI_STR "pci"
1081 static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1085 switch (reboot_mode) {
1087 val = REBOOT_COLD_STR;
1090 val = REBOOT_WARM_STR;
1093 val = REBOOT_HARD_STR;
1096 val = REBOOT_SOFT_STR;
1099 val = REBOOT_GPIO_STR;
1102 val = REBOOT_UNDEFINED_STR;
1105 return sprintf(buf, "%s\n", val);
1107 static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
1108 const char *buf, size_t count)
1110 if (!capable(CAP_SYS_BOOT))
1113 if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
1114 reboot_mode = REBOOT_COLD;
1115 else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
1116 reboot_mode = REBOOT_WARM;
1117 else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
1118 reboot_mode = REBOOT_HARD;
1119 else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
1120 reboot_mode = REBOOT_SOFT;
1121 else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
1122 reboot_mode = REBOOT_GPIO;
1130 static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
1133 static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1135 return sprintf(buf, "%d\n", reboot_force);
1137 static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
1138 const char *buf, size_t count)
1142 if (!capable(CAP_SYS_BOOT))
1145 if (kstrtobool(buf, &res))
1153 static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
1155 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1159 switch (reboot_type) {
1161 val = BOOT_TRIPLE_STR;
1167 val = BOOT_BIOS_STR;
1170 val = BOOT_ACPI_STR;
1175 case BOOT_CF9_FORCE:
1179 val = REBOOT_UNDEFINED_STR;
1182 return sprintf(buf, "%s\n", val);
1184 static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
1185 const char *buf, size_t count)
1187 if (!capable(CAP_SYS_BOOT))
1190 if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
1191 reboot_type = BOOT_TRIPLE;
1192 else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
1193 reboot_type = BOOT_KBD;
1194 else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
1195 reboot_type = BOOT_BIOS;
1196 else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
1197 reboot_type = BOOT_ACPI;
1198 else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
1199 reboot_type = BOOT_EFI;
1200 else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
1201 reboot_type = BOOT_CF9_FORCE;
1209 static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
1213 static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1215 return sprintf(buf, "%d\n", reboot_cpu);
1217 static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
1218 const char *buf, size_t count)
1220 unsigned int cpunum;
1223 if (!capable(CAP_SYS_BOOT))
1226 rc = kstrtouint(buf, 0, &cpunum);
1231 if (cpunum >= num_possible_cpus())
1235 reboot_cpu = cpunum;
1239 static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
1242 static struct attribute *reboot_attrs[] = {
1243 &reboot_mode_attr.attr,
1245 &reboot_force_attr.attr,
1246 &reboot_type_attr.attr,
1249 &reboot_cpu_attr.attr,
1254 #ifdef CONFIG_SYSCTL
1255 static struct ctl_table kern_reboot_table[] = {
1257 .procname = "poweroff_cmd",
1258 .data = &poweroff_cmd,
1259 .maxlen = POWEROFF_CMD_PATH_LEN,
1261 .proc_handler = proc_dostring,
1264 .procname = "ctrl-alt-del",
1266 .maxlen = sizeof(int),
1268 .proc_handler = proc_dointvec,
1273 static void __init kernel_reboot_sysctls_init(void)
1275 register_sysctl_init("kernel", kern_reboot_table);
1278 #define kernel_reboot_sysctls_init() do { } while (0)
1279 #endif /* CONFIG_SYSCTL */
1281 static const struct attribute_group reboot_attr_group = {
1282 .attrs = reboot_attrs,
1285 static int __init reboot_ksysfs_init(void)
1287 struct kobject *reboot_kobj;
1290 reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
1294 ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
1296 kobject_put(reboot_kobj);
1300 kernel_reboot_sysctls_init();
1304 late_initcall(reboot_ksysfs_init);