1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 215d94b82SRobin Holt /* 315d94b82SRobin Holt * linux/kernel/reboot.c 415d94b82SRobin Holt * 515d94b82SRobin Holt * Copyright (C) 2013 Linus Torvalds 615d94b82SRobin Holt */ 715d94b82SRobin Holt 8972ee83dSRobin Holt #define pr_fmt(fmt) "reboot: " fmt 9972ee83dSRobin Holt 10dfa19b11SMatti Vaittinen #include <linux/atomic.h> 111b3a5d02SRobin Holt #include <linux/ctype.h> 1215d94b82SRobin Holt #include <linux/export.h> 1315d94b82SRobin Holt #include <linux/kexec.h> 1415d94b82SRobin Holt #include <linux/kmod.h> 1515d94b82SRobin Holt #include <linux/kmsg_dump.h> 1615d94b82SRobin Holt #include <linux/reboot.h> 1715d94b82SRobin Holt #include <linux/suspend.h> 1815d94b82SRobin Holt #include <linux/syscalls.h> 1915d94b82SRobin Holt #include <linux/syscore_ops.h> 2015d94b82SRobin Holt #include <linux/uaccess.h> 2115d94b82SRobin Holt 2215d94b82SRobin Holt /* 2315d94b82SRobin Holt * this indicates whether you can reboot with ctrl-alt-del: the default is yes 2415d94b82SRobin Holt */ 2515d94b82SRobin Holt 2615d94b82SRobin Holt int C_A_D = 1; 2715d94b82SRobin Holt struct pid *cad_pid; 2815d94b82SRobin Holt EXPORT_SYMBOL(cad_pid); 2915d94b82SRobin Holt 30fb37409aSMike Rapoport #if defined(CONFIG_ARM) 311b3a5d02SRobin Holt #define DEFAULT_REBOOT_MODE = REBOOT_HARD 321b3a5d02SRobin Holt #else 331b3a5d02SRobin Holt #define DEFAULT_REBOOT_MODE 341b3a5d02SRobin Holt #endif 351b3a5d02SRobin Holt enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE; 36d46b3f5bSShawn Guo EXPORT_SYMBOL_GPL(reboot_mode); 37b287a25aSAaro Koskinen enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED; 381b3a5d02SRobin Holt 39e2f0b88eSChuansheng Liu /* 40e2f0b88eSChuansheng Liu * This variable is used privately to keep track of whether or not 41e2f0b88eSChuansheng Liu * reboot_type is still set to its default value (i.e., reboot= hasn't 42e2f0b88eSChuansheng Liu * been set on the command line). This is needed so that we can 43e2f0b88eSChuansheng Liu * suppress DMI scanning for reboot quirks. Without it, it's 44e2f0b88eSChuansheng Liu * impossible to override a faulty reboot quirk without recompiling. 45e2f0b88eSChuansheng Liu */ 46e2f0b88eSChuansheng Liu int reboot_default = 1; 471b3a5d02SRobin Holt int reboot_cpu; 481b3a5d02SRobin Holt enum reboot_type reboot_type = BOOT_ACPI; 491b3a5d02SRobin Holt int reboot_force; 501b3a5d02SRobin Holt 51232edc2fSDmitry Osipenko struct sys_off_handler { 52232edc2fSDmitry Osipenko struct notifier_block nb; 53232edc2fSDmitry Osipenko int (*sys_off_cb)(struct sys_off_data *data); 54232edc2fSDmitry Osipenko void *cb_data; 55232edc2fSDmitry Osipenko enum sys_off_mode mode; 56232edc2fSDmitry Osipenko bool blocking; 57232edc2fSDmitry Osipenko void *list; 58232edc2fSDmitry Osipenko }; 59232edc2fSDmitry Osipenko 6015d94b82SRobin Holt /* 6115d94b82SRobin Holt * If set, this is used for preparing the system to power off. 6215d94b82SRobin Holt */ 6315d94b82SRobin Holt 6415d94b82SRobin Holt void (*pm_power_off_prepare)(void); 6574f008f2SOleksij Rempel EXPORT_SYMBOL_GPL(pm_power_off_prepare); 6615d94b82SRobin Holt 6715d94b82SRobin Holt /** 6815d94b82SRobin Holt * emergency_restart - reboot the system 6915d94b82SRobin Holt * 7015d94b82SRobin Holt * Without shutting down any hardware or taking any locks 7115d94b82SRobin Holt * reboot the system. This is called when we know we are in 7215d94b82SRobin Holt * trouble so this is our best effort to reboot. This is 7315d94b82SRobin Holt * safe to call in interrupt context. 7415d94b82SRobin Holt */ 7515d94b82SRobin Holt void emergency_restart(void) 7615d94b82SRobin Holt { 7715d94b82SRobin Holt kmsg_dump(KMSG_DUMP_EMERG); 7815d94b82SRobin Holt machine_emergency_restart(); 7915d94b82SRobin Holt } 8015d94b82SRobin Holt EXPORT_SYMBOL_GPL(emergency_restart); 8115d94b82SRobin Holt 8215d94b82SRobin Holt void kernel_restart_prepare(char *cmd) 8315d94b82SRobin Holt { 8415d94b82SRobin Holt blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); 8515d94b82SRobin Holt system_state = SYSTEM_RESTART; 8615d94b82SRobin Holt usermodehelper_disable(); 8715d94b82SRobin Holt device_shutdown(); 8815d94b82SRobin Holt } 8915d94b82SRobin Holt 9015d94b82SRobin Holt /** 9115d94b82SRobin Holt * register_reboot_notifier - Register function to be called at reboot time 9215d94b82SRobin Holt * @nb: Info about notifier function to be called 9315d94b82SRobin Holt * 9415d94b82SRobin Holt * Registers a function with the list of functions 9515d94b82SRobin Holt * to be called at reboot time. 9615d94b82SRobin Holt * 9715d94b82SRobin Holt * Currently always returns zero, as blocking_notifier_chain_register() 9815d94b82SRobin Holt * always returns zero. 9915d94b82SRobin Holt */ 10015d94b82SRobin Holt int register_reboot_notifier(struct notifier_block *nb) 10115d94b82SRobin Holt { 10215d94b82SRobin Holt return blocking_notifier_chain_register(&reboot_notifier_list, nb); 10315d94b82SRobin Holt } 10415d94b82SRobin Holt EXPORT_SYMBOL(register_reboot_notifier); 10515d94b82SRobin Holt 10615d94b82SRobin Holt /** 10715d94b82SRobin Holt * unregister_reboot_notifier - Unregister previously registered reboot notifier 10815d94b82SRobin Holt * @nb: Hook to be unregistered 10915d94b82SRobin Holt * 11015d94b82SRobin Holt * Unregisters a previously registered reboot 11115d94b82SRobin Holt * notifier function. 11215d94b82SRobin Holt * 11315d94b82SRobin Holt * Returns zero on success, or %-ENOENT on failure. 11415d94b82SRobin Holt */ 11515d94b82SRobin Holt int unregister_reboot_notifier(struct notifier_block *nb) 11615d94b82SRobin Holt { 11715d94b82SRobin Holt return blocking_notifier_chain_unregister(&reboot_notifier_list, nb); 11815d94b82SRobin Holt } 11915d94b82SRobin Holt EXPORT_SYMBOL(unregister_reboot_notifier); 12015d94b82SRobin Holt 1212d8364baSAndrey Smirnov static void devm_unregister_reboot_notifier(struct device *dev, void *res) 1222d8364baSAndrey Smirnov { 1232d8364baSAndrey Smirnov WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res)); 1242d8364baSAndrey Smirnov } 1252d8364baSAndrey Smirnov 1262d8364baSAndrey Smirnov int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb) 1272d8364baSAndrey Smirnov { 1282d8364baSAndrey Smirnov struct notifier_block **rcnb; 1292d8364baSAndrey Smirnov int ret; 1302d8364baSAndrey Smirnov 1312d8364baSAndrey Smirnov rcnb = devres_alloc(devm_unregister_reboot_notifier, 1322d8364baSAndrey Smirnov sizeof(*rcnb), GFP_KERNEL); 1332d8364baSAndrey Smirnov if (!rcnb) 1342d8364baSAndrey Smirnov return -ENOMEM; 1352d8364baSAndrey Smirnov 1362d8364baSAndrey Smirnov ret = register_reboot_notifier(nb); 1372d8364baSAndrey Smirnov if (!ret) { 1382d8364baSAndrey Smirnov *rcnb = nb; 1392d8364baSAndrey Smirnov devres_add(dev, rcnb); 1402d8364baSAndrey Smirnov } else { 1412d8364baSAndrey Smirnov devres_free(rcnb); 1422d8364baSAndrey Smirnov } 1432d8364baSAndrey Smirnov 1442d8364baSAndrey Smirnov return ret; 1452d8364baSAndrey Smirnov } 1462d8364baSAndrey Smirnov EXPORT_SYMBOL(devm_register_reboot_notifier); 1472d8364baSAndrey Smirnov 148b63adb97SGuenter Roeck /* 149b63adb97SGuenter Roeck * Notifier list for kernel code which wants to be called 150b63adb97SGuenter Roeck * to restart the system. 151b63adb97SGuenter Roeck */ 152b63adb97SGuenter Roeck static ATOMIC_NOTIFIER_HEAD(restart_handler_list); 153b63adb97SGuenter Roeck 154b63adb97SGuenter Roeck /** 155b63adb97SGuenter Roeck * register_restart_handler - Register function to be called to reset 156b63adb97SGuenter Roeck * the system 157b63adb97SGuenter Roeck * @nb: Info about handler function to be called 158b63adb97SGuenter Roeck * @nb->priority: Handler priority. Handlers should follow the 159b63adb97SGuenter Roeck * following guidelines for setting priorities. 160b63adb97SGuenter Roeck * 0: Restart handler of last resort, 161b63adb97SGuenter Roeck * with limited restart capabilities 162b63adb97SGuenter Roeck * 128: Default restart handler; use if no other 163b63adb97SGuenter Roeck * restart handler is expected to be available, 164b63adb97SGuenter Roeck * and/or if restart functionality is 165b63adb97SGuenter Roeck * sufficient to restart the entire system 166b63adb97SGuenter Roeck * 255: Highest priority restart handler, will 167b63adb97SGuenter Roeck * preempt all other restart handlers 168b63adb97SGuenter Roeck * 169b63adb97SGuenter Roeck * Registers a function with code to be called to restart the 170b63adb97SGuenter Roeck * system. 171b63adb97SGuenter Roeck * 172b63adb97SGuenter Roeck * Registered functions will be called from machine_restart as last 173b63adb97SGuenter Roeck * step of the restart sequence (if the architecture specific 174b63adb97SGuenter Roeck * machine_restart function calls do_kernel_restart - see below 175b63adb97SGuenter Roeck * for details). 176b63adb97SGuenter Roeck * Registered functions are expected to restart the system immediately. 177b63adb97SGuenter Roeck * If more than one function is registered, the restart handler priority 178b63adb97SGuenter Roeck * selects which function will be called first. 179b63adb97SGuenter Roeck * 180b63adb97SGuenter Roeck * Restart handlers are expected to be registered from non-architecture 181b63adb97SGuenter Roeck * code, typically from drivers. A typical use case would be a system 182b63adb97SGuenter Roeck * where restart functionality is provided through a watchdog. Multiple 183b63adb97SGuenter Roeck * restart handlers may exist; for example, one restart handler might 184b63adb97SGuenter Roeck * restart the entire system, while another only restarts the CPU. 185b63adb97SGuenter Roeck * In such cases, the restart handler which only restarts part of the 186b63adb97SGuenter Roeck * hardware is expected to register with low priority to ensure that 187b63adb97SGuenter Roeck * it only runs if no other means to restart the system is available. 188b63adb97SGuenter Roeck * 189b63adb97SGuenter Roeck * Currently always returns zero, as atomic_notifier_chain_register() 190b63adb97SGuenter Roeck * always returns zero. 191b63adb97SGuenter Roeck */ 192b63adb97SGuenter Roeck int register_restart_handler(struct notifier_block *nb) 193b63adb97SGuenter Roeck { 194b63adb97SGuenter Roeck return atomic_notifier_chain_register(&restart_handler_list, nb); 195b63adb97SGuenter Roeck } 196b63adb97SGuenter Roeck EXPORT_SYMBOL(register_restart_handler); 197b63adb97SGuenter Roeck 198b63adb97SGuenter Roeck /** 199b63adb97SGuenter Roeck * unregister_restart_handler - Unregister previously registered 200b63adb97SGuenter Roeck * restart handler 201b63adb97SGuenter Roeck * @nb: Hook to be unregistered 202b63adb97SGuenter Roeck * 203b63adb97SGuenter Roeck * Unregisters a previously registered restart handler function. 204b63adb97SGuenter Roeck * 205b63adb97SGuenter Roeck * Returns zero on success, or %-ENOENT on failure. 206b63adb97SGuenter Roeck */ 207b63adb97SGuenter Roeck int unregister_restart_handler(struct notifier_block *nb) 208b63adb97SGuenter Roeck { 209b63adb97SGuenter Roeck return atomic_notifier_chain_unregister(&restart_handler_list, nb); 210b63adb97SGuenter Roeck } 211b63adb97SGuenter Roeck EXPORT_SYMBOL(unregister_restart_handler); 212b63adb97SGuenter Roeck 213b63adb97SGuenter Roeck /** 214b63adb97SGuenter Roeck * do_kernel_restart - Execute kernel restart handler call chain 215b63adb97SGuenter Roeck * 216b63adb97SGuenter Roeck * Calls functions registered with register_restart_handler. 217b63adb97SGuenter Roeck * 218b63adb97SGuenter Roeck * Expected to be called from machine_restart as last step of the restart 219b63adb97SGuenter Roeck * sequence. 220b63adb97SGuenter Roeck * 221b63adb97SGuenter Roeck * Restarts the system immediately if a restart handler function has been 222b63adb97SGuenter Roeck * registered. Otherwise does nothing. 223b63adb97SGuenter Roeck */ 224b63adb97SGuenter Roeck void do_kernel_restart(char *cmd) 225b63adb97SGuenter Roeck { 226b63adb97SGuenter Roeck atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd); 227b63adb97SGuenter Roeck } 228b63adb97SGuenter Roeck 229c97102baSVivek Goyal void migrate_to_reboot_cpu(void) 23015d94b82SRobin Holt { 23115d94b82SRobin Holt /* The boot cpu is always logical cpu 0 */ 2321b3a5d02SRobin Holt int cpu = reboot_cpu; 23315d94b82SRobin Holt 23415d94b82SRobin Holt cpu_hotplug_disable(); 23515d94b82SRobin Holt 23615d94b82SRobin Holt /* Make certain the cpu I'm about to reboot on is online */ 23715d94b82SRobin Holt if (!cpu_online(cpu)) 23815d94b82SRobin Holt cpu = cpumask_first(cpu_online_mask); 23915d94b82SRobin Holt 24015d94b82SRobin Holt /* Prevent races with other tasks migrating this task */ 24115d94b82SRobin Holt current->flags |= PF_NO_SETAFFINITY; 24215d94b82SRobin Holt 24315d94b82SRobin Holt /* Make certain I only run on the appropriate processor */ 24415d94b82SRobin Holt set_cpus_allowed_ptr(current, cpumask_of(cpu)); 24515d94b82SRobin Holt } 24615d94b82SRobin Holt 24715d94b82SRobin Holt /** 24815d94b82SRobin Holt * kernel_restart - reboot the system 24915d94b82SRobin Holt * @cmd: pointer to buffer containing command to execute for restart 25015d94b82SRobin Holt * or %NULL 25115d94b82SRobin Holt * 25215d94b82SRobin Holt * Shutdown everything and perform a clean reboot. 25315d94b82SRobin Holt * This is not safe to call in interrupt context. 25415d94b82SRobin Holt */ 25515d94b82SRobin Holt void kernel_restart(char *cmd) 25615d94b82SRobin Holt { 25715d94b82SRobin Holt kernel_restart_prepare(cmd); 25815d94b82SRobin Holt migrate_to_reboot_cpu(); 25915d94b82SRobin Holt syscore_shutdown(); 26015d94b82SRobin Holt if (!cmd) 261972ee83dSRobin Holt pr_emerg("Restarting system\n"); 26215d94b82SRobin Holt else 263972ee83dSRobin Holt pr_emerg("Restarting system with command '%s'\n", cmd); 2646d3cf962SKees Cook kmsg_dump(KMSG_DUMP_SHUTDOWN); 26515d94b82SRobin Holt machine_restart(cmd); 26615d94b82SRobin Holt } 26715d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_restart); 26815d94b82SRobin Holt 26915d94b82SRobin Holt static void kernel_shutdown_prepare(enum system_states state) 27015d94b82SRobin Holt { 27115d94b82SRobin Holt blocking_notifier_call_chain(&reboot_notifier_list, 27215d94b82SRobin Holt (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL); 27315d94b82SRobin Holt system_state = state; 27415d94b82SRobin Holt usermodehelper_disable(); 27515d94b82SRobin Holt device_shutdown(); 27615d94b82SRobin Holt } 27715d94b82SRobin Holt /** 27815d94b82SRobin Holt * kernel_halt - halt the system 27915d94b82SRobin Holt * 28015d94b82SRobin Holt * Shutdown everything and perform a clean system halt. 28115d94b82SRobin Holt */ 28215d94b82SRobin Holt void kernel_halt(void) 28315d94b82SRobin Holt { 28415d94b82SRobin Holt kernel_shutdown_prepare(SYSTEM_HALT); 28515d94b82SRobin Holt migrate_to_reboot_cpu(); 28615d94b82SRobin Holt syscore_shutdown(); 287972ee83dSRobin Holt pr_emerg("System halted\n"); 2886d3cf962SKees Cook kmsg_dump(KMSG_DUMP_SHUTDOWN); 28915d94b82SRobin Holt machine_halt(); 29015d94b82SRobin Holt } 29115d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_halt); 29215d94b82SRobin Holt 293232edc2fSDmitry Osipenko /* 294232edc2fSDmitry Osipenko * Notifier list for kernel code which wants to be called 295232edc2fSDmitry Osipenko * to prepare system for power off. 296232edc2fSDmitry Osipenko */ 297232edc2fSDmitry Osipenko static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list); 298232edc2fSDmitry Osipenko 299232edc2fSDmitry Osipenko /* 300232edc2fSDmitry Osipenko * Notifier list for kernel code which wants to be called 301232edc2fSDmitry Osipenko * to power off system. 302232edc2fSDmitry Osipenko */ 303232edc2fSDmitry Osipenko static ATOMIC_NOTIFIER_HEAD(power_off_handler_list); 304232edc2fSDmitry Osipenko 305232edc2fSDmitry Osipenko static int sys_off_notify(struct notifier_block *nb, 306232edc2fSDmitry Osipenko unsigned long mode, void *cmd) 307232edc2fSDmitry Osipenko { 308232edc2fSDmitry Osipenko struct sys_off_handler *handler; 309232edc2fSDmitry Osipenko struct sys_off_data data = {}; 310232edc2fSDmitry Osipenko 311232edc2fSDmitry Osipenko handler = container_of(nb, struct sys_off_handler, nb); 312232edc2fSDmitry Osipenko data.cb_data = handler->cb_data; 313232edc2fSDmitry Osipenko data.mode = mode; 314232edc2fSDmitry Osipenko data.cmd = cmd; 315232edc2fSDmitry Osipenko 316232edc2fSDmitry Osipenko return handler->sys_off_cb(&data); 317232edc2fSDmitry Osipenko } 318232edc2fSDmitry Osipenko 319232edc2fSDmitry Osipenko /** 320232edc2fSDmitry Osipenko * register_sys_off_handler - Register sys-off handler 321232edc2fSDmitry Osipenko * @mode: Sys-off mode 322232edc2fSDmitry Osipenko * @priority: Handler priority 323232edc2fSDmitry Osipenko * @callback: Callback function 324232edc2fSDmitry Osipenko * @cb_data: Callback argument 325232edc2fSDmitry Osipenko * 326232edc2fSDmitry Osipenko * Registers system power-off or restart handler that will be invoked 327232edc2fSDmitry Osipenko * at the step corresponding to the given sys-off mode. Handler's callback 328232edc2fSDmitry Osipenko * should return NOTIFY_DONE to permit execution of the next handler in 329232edc2fSDmitry Osipenko * the call chain or NOTIFY_STOP to break the chain (in error case for 330232edc2fSDmitry Osipenko * example). 331232edc2fSDmitry Osipenko * 332232edc2fSDmitry Osipenko * Multiple handlers can be registered at the default priority level. 333232edc2fSDmitry Osipenko * 334232edc2fSDmitry Osipenko * Only one handler can be registered at the non-default priority level, 335232edc2fSDmitry Osipenko * otherwise ERR_PTR(-EBUSY) is returned. 336232edc2fSDmitry Osipenko * 337232edc2fSDmitry Osipenko * Returns a new instance of struct sys_off_handler on success, or 338232edc2fSDmitry Osipenko * an ERR_PTR()-encoded error code otherwise. 339232edc2fSDmitry Osipenko */ 340232edc2fSDmitry Osipenko struct sys_off_handler * 341232edc2fSDmitry Osipenko register_sys_off_handler(enum sys_off_mode mode, 342232edc2fSDmitry Osipenko int priority, 343232edc2fSDmitry Osipenko int (*callback)(struct sys_off_data *data), 344232edc2fSDmitry Osipenko void *cb_data) 345232edc2fSDmitry Osipenko { 346232edc2fSDmitry Osipenko struct sys_off_handler *handler; 347232edc2fSDmitry Osipenko int err; 348232edc2fSDmitry Osipenko 349232edc2fSDmitry Osipenko handler = kzalloc(sizeof(*handler), GFP_KERNEL); 350232edc2fSDmitry Osipenko if (!handler) 351232edc2fSDmitry Osipenko return ERR_PTR(-ENOMEM); 352232edc2fSDmitry Osipenko 353232edc2fSDmitry Osipenko switch (mode) { 354232edc2fSDmitry Osipenko case SYS_OFF_MODE_POWER_OFF_PREPARE: 355232edc2fSDmitry Osipenko handler->list = &power_off_prep_handler_list; 356232edc2fSDmitry Osipenko handler->blocking = true; 357232edc2fSDmitry Osipenko break; 358232edc2fSDmitry Osipenko 359232edc2fSDmitry Osipenko case SYS_OFF_MODE_POWER_OFF: 360232edc2fSDmitry Osipenko handler->list = &power_off_handler_list; 361232edc2fSDmitry Osipenko break; 362232edc2fSDmitry Osipenko 363232edc2fSDmitry Osipenko case SYS_OFF_MODE_RESTART: 364232edc2fSDmitry Osipenko handler->list = &restart_handler_list; 365232edc2fSDmitry Osipenko break; 366232edc2fSDmitry Osipenko 367232edc2fSDmitry Osipenko default: 368232edc2fSDmitry Osipenko kfree(handler); 369232edc2fSDmitry Osipenko return ERR_PTR(-EINVAL); 370232edc2fSDmitry Osipenko } 371232edc2fSDmitry Osipenko 372232edc2fSDmitry Osipenko handler->nb.notifier_call = sys_off_notify; 373232edc2fSDmitry Osipenko handler->nb.priority = priority; 374232edc2fSDmitry Osipenko handler->sys_off_cb = callback; 375232edc2fSDmitry Osipenko handler->cb_data = cb_data; 376232edc2fSDmitry Osipenko handler->mode = mode; 377232edc2fSDmitry Osipenko 378232edc2fSDmitry Osipenko if (handler->blocking) { 379232edc2fSDmitry Osipenko if (priority == SYS_OFF_PRIO_DEFAULT) 380232edc2fSDmitry Osipenko err = blocking_notifier_chain_register(handler->list, 381232edc2fSDmitry Osipenko &handler->nb); 382232edc2fSDmitry Osipenko else 383232edc2fSDmitry Osipenko err = blocking_notifier_chain_register_unique_prio(handler->list, 384232edc2fSDmitry Osipenko &handler->nb); 385232edc2fSDmitry Osipenko } else { 386232edc2fSDmitry Osipenko if (priority == SYS_OFF_PRIO_DEFAULT) 387232edc2fSDmitry Osipenko err = atomic_notifier_chain_register(handler->list, 388232edc2fSDmitry Osipenko &handler->nb); 389232edc2fSDmitry Osipenko else 390232edc2fSDmitry Osipenko err = atomic_notifier_chain_register_unique_prio(handler->list, 391232edc2fSDmitry Osipenko &handler->nb); 392232edc2fSDmitry Osipenko } 393232edc2fSDmitry Osipenko 394232edc2fSDmitry Osipenko if (err) { 395232edc2fSDmitry Osipenko kfree(handler); 396232edc2fSDmitry Osipenko return ERR_PTR(err); 397232edc2fSDmitry Osipenko } 398232edc2fSDmitry Osipenko 399232edc2fSDmitry Osipenko return handler; 400232edc2fSDmitry Osipenko } 401232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(register_sys_off_handler); 402232edc2fSDmitry Osipenko 403232edc2fSDmitry Osipenko /** 404232edc2fSDmitry Osipenko * unregister_sys_off_handler - Unregister sys-off handler 405232edc2fSDmitry Osipenko * @handler: Sys-off handler 406232edc2fSDmitry Osipenko * 407232edc2fSDmitry Osipenko * Unregisters given sys-off handler. 408232edc2fSDmitry Osipenko */ 409232edc2fSDmitry Osipenko void unregister_sys_off_handler(struct sys_off_handler *handler) 410232edc2fSDmitry Osipenko { 411232edc2fSDmitry Osipenko int err; 412232edc2fSDmitry Osipenko 413232edc2fSDmitry Osipenko if (!handler) 414232edc2fSDmitry Osipenko return; 415232edc2fSDmitry Osipenko 416232edc2fSDmitry Osipenko if (handler->blocking) 417232edc2fSDmitry Osipenko err = blocking_notifier_chain_unregister(handler->list, 418232edc2fSDmitry Osipenko &handler->nb); 419232edc2fSDmitry Osipenko else 420232edc2fSDmitry Osipenko err = atomic_notifier_chain_unregister(handler->list, 421232edc2fSDmitry Osipenko &handler->nb); 422232edc2fSDmitry Osipenko 423232edc2fSDmitry Osipenko /* sanity check, shall never happen */ 424232edc2fSDmitry Osipenko WARN_ON(err); 425232edc2fSDmitry Osipenko 426232edc2fSDmitry Osipenko kfree(handler); 427232edc2fSDmitry Osipenko } 428232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(unregister_sys_off_handler); 429232edc2fSDmitry Osipenko 430232edc2fSDmitry Osipenko static void devm_unregister_sys_off_handler(void *data) 431232edc2fSDmitry Osipenko { 432232edc2fSDmitry Osipenko struct sys_off_handler *handler = data; 433232edc2fSDmitry Osipenko 434232edc2fSDmitry Osipenko unregister_sys_off_handler(handler); 435232edc2fSDmitry Osipenko } 436232edc2fSDmitry Osipenko 437232edc2fSDmitry Osipenko /** 438232edc2fSDmitry Osipenko * devm_register_sys_off_handler - Register sys-off handler 439232edc2fSDmitry Osipenko * @dev: Device that registers handler 440232edc2fSDmitry Osipenko * @mode: Sys-off mode 441232edc2fSDmitry Osipenko * @priority: Handler priority 442232edc2fSDmitry Osipenko * @callback: Callback function 443232edc2fSDmitry Osipenko * @cb_data: Callback argument 444232edc2fSDmitry Osipenko * 445232edc2fSDmitry Osipenko * Registers resource-managed sys-off handler. 446232edc2fSDmitry Osipenko * 447232edc2fSDmitry Osipenko * Returns zero on success, or error code on failure. 448232edc2fSDmitry Osipenko */ 449232edc2fSDmitry Osipenko int devm_register_sys_off_handler(struct device *dev, 450232edc2fSDmitry Osipenko enum sys_off_mode mode, 451232edc2fSDmitry Osipenko int priority, 452232edc2fSDmitry Osipenko int (*callback)(struct sys_off_data *data), 453232edc2fSDmitry Osipenko void *cb_data) 454232edc2fSDmitry Osipenko { 455232edc2fSDmitry Osipenko struct sys_off_handler *handler; 456232edc2fSDmitry Osipenko 457232edc2fSDmitry Osipenko handler = register_sys_off_handler(mode, priority, callback, cb_data); 458232edc2fSDmitry Osipenko if (IS_ERR(handler)) 459232edc2fSDmitry Osipenko return PTR_ERR(handler); 460232edc2fSDmitry Osipenko 461232edc2fSDmitry Osipenko return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler, 462232edc2fSDmitry Osipenko handler); 463232edc2fSDmitry Osipenko } 464232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(devm_register_sys_off_handler); 465232edc2fSDmitry Osipenko 466*7b9a3de9SDmitry Osipenko static int legacy_pm_power_off_prepare(struct sys_off_data *data) 467*7b9a3de9SDmitry Osipenko { 468*7b9a3de9SDmitry Osipenko if (pm_power_off_prepare) 469*7b9a3de9SDmitry Osipenko pm_power_off_prepare(); 470*7b9a3de9SDmitry Osipenko 471*7b9a3de9SDmitry Osipenko return NOTIFY_DONE; 472*7b9a3de9SDmitry Osipenko } 473*7b9a3de9SDmitry Osipenko 474*7b9a3de9SDmitry Osipenko static int legacy_pm_power_off(struct sys_off_data *data) 475*7b9a3de9SDmitry Osipenko { 476*7b9a3de9SDmitry Osipenko if (pm_power_off) 477*7b9a3de9SDmitry Osipenko pm_power_off(); 478*7b9a3de9SDmitry Osipenko 479*7b9a3de9SDmitry Osipenko return NOTIFY_DONE; 480*7b9a3de9SDmitry Osipenko } 481*7b9a3de9SDmitry Osipenko 482*7b9a3de9SDmitry Osipenko /* 483*7b9a3de9SDmitry Osipenko * Register sys-off handlers for legacy PM callbacks. This allows legacy 484*7b9a3de9SDmitry Osipenko * PM callbacks co-exist with the new sys-off API. 485*7b9a3de9SDmitry Osipenko * 486*7b9a3de9SDmitry Osipenko * TODO: Remove legacy handlers once all legacy PM users will be switched 487*7b9a3de9SDmitry Osipenko * to the sys-off based APIs. 488*7b9a3de9SDmitry Osipenko */ 489*7b9a3de9SDmitry Osipenko static int __init legacy_pm_init(void) 490*7b9a3de9SDmitry Osipenko { 491*7b9a3de9SDmitry Osipenko register_sys_off_handler(SYS_OFF_MODE_POWER_OFF_PREPARE, 492*7b9a3de9SDmitry Osipenko SYS_OFF_PRIO_DEFAULT, 493*7b9a3de9SDmitry Osipenko legacy_pm_power_off_prepare, NULL); 494*7b9a3de9SDmitry Osipenko 495*7b9a3de9SDmitry Osipenko register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT, 496*7b9a3de9SDmitry Osipenko legacy_pm_power_off, NULL); 497*7b9a3de9SDmitry Osipenko 498*7b9a3de9SDmitry Osipenko return 0; 499*7b9a3de9SDmitry Osipenko } 500*7b9a3de9SDmitry Osipenko core_initcall(legacy_pm_init); 501*7b9a3de9SDmitry Osipenko 502*7b9a3de9SDmitry Osipenko static void do_kernel_power_off_prepare(void) 503*7b9a3de9SDmitry Osipenko { 504*7b9a3de9SDmitry Osipenko blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL); 505*7b9a3de9SDmitry Osipenko } 506*7b9a3de9SDmitry Osipenko 50715d94b82SRobin Holt /** 50815d94b82SRobin Holt * kernel_power_off - power_off the system 50915d94b82SRobin Holt * 51015d94b82SRobin Holt * Shutdown everything and perform a clean system power_off. 51115d94b82SRobin Holt */ 51215d94b82SRobin Holt void kernel_power_off(void) 51315d94b82SRobin Holt { 51415d94b82SRobin Holt kernel_shutdown_prepare(SYSTEM_POWER_OFF); 515*7b9a3de9SDmitry Osipenko do_kernel_power_off_prepare(); 51615d94b82SRobin Holt migrate_to_reboot_cpu(); 51715d94b82SRobin Holt syscore_shutdown(); 518972ee83dSRobin Holt pr_emerg("Power down\n"); 5196d3cf962SKees Cook kmsg_dump(KMSG_DUMP_SHUTDOWN); 52015d94b82SRobin Holt machine_power_off(); 52115d94b82SRobin Holt } 52215d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_power_off); 52315d94b82SRobin Holt 52455f2503cSPingfan Liu DEFINE_MUTEX(system_transition_mutex); 52515d94b82SRobin Holt 52615d94b82SRobin Holt /* 52715d94b82SRobin Holt * Reboot system call: for obvious reasons only root may call it, 52815d94b82SRobin Holt * and even root needs to set up some magic numbers in the registers 52915d94b82SRobin Holt * so that some mistake won't make this reboot the whole machine. 53015d94b82SRobin Holt * You can also set the meaning of the ctrl-alt-del-key here. 53115d94b82SRobin Holt * 53215d94b82SRobin Holt * reboot doesn't sync: do that yourself before calling this. 53315d94b82SRobin Holt */ 53415d94b82SRobin Holt SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd, 53515d94b82SRobin Holt void __user *, arg) 53615d94b82SRobin Holt { 53715d94b82SRobin Holt struct pid_namespace *pid_ns = task_active_pid_ns(current); 53815d94b82SRobin Holt char buffer[256]; 53915d94b82SRobin Holt int ret = 0; 54015d94b82SRobin Holt 54115d94b82SRobin Holt /* We only trust the superuser with rebooting the system. */ 54215d94b82SRobin Holt if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT)) 54315d94b82SRobin Holt return -EPERM; 54415d94b82SRobin Holt 54515d94b82SRobin Holt /* For safety, we require "magic" arguments. */ 54615d94b82SRobin Holt if (magic1 != LINUX_REBOOT_MAGIC1 || 54715d94b82SRobin Holt (magic2 != LINUX_REBOOT_MAGIC2 && 54815d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2A && 54915d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2B && 55015d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2C)) 55115d94b82SRobin Holt return -EINVAL; 55215d94b82SRobin Holt 55315d94b82SRobin Holt /* 55415d94b82SRobin Holt * If pid namespaces are enabled and the current task is in a child 55515d94b82SRobin Holt * pid_namespace, the command is handled by reboot_pid_ns() which will 55615d94b82SRobin Holt * call do_exit(). 55715d94b82SRobin Holt */ 55815d94b82SRobin Holt ret = reboot_pid_ns(pid_ns, cmd); 55915d94b82SRobin Holt if (ret) 56015d94b82SRobin Holt return ret; 56115d94b82SRobin Holt 56215d94b82SRobin Holt /* Instead of trying to make the power_off code look like 56315d94b82SRobin Holt * halt when pm_power_off is not set do it the easy way. 56415d94b82SRobin Holt */ 56515d94b82SRobin Holt if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off) 56615d94b82SRobin Holt cmd = LINUX_REBOOT_CMD_HALT; 56715d94b82SRobin Holt 56855f2503cSPingfan Liu mutex_lock(&system_transition_mutex); 56915d94b82SRobin Holt switch (cmd) { 57015d94b82SRobin Holt case LINUX_REBOOT_CMD_RESTART: 57115d94b82SRobin Holt kernel_restart(NULL); 57215d94b82SRobin Holt break; 57315d94b82SRobin Holt 57415d94b82SRobin Holt case LINUX_REBOOT_CMD_CAD_ON: 57515d94b82SRobin Holt C_A_D = 1; 57615d94b82SRobin Holt break; 57715d94b82SRobin Holt 57815d94b82SRobin Holt case LINUX_REBOOT_CMD_CAD_OFF: 57915d94b82SRobin Holt C_A_D = 0; 58015d94b82SRobin Holt break; 58115d94b82SRobin Holt 58215d94b82SRobin Holt case LINUX_REBOOT_CMD_HALT: 58315d94b82SRobin Holt kernel_halt(); 58415d94b82SRobin Holt do_exit(0); 58515d94b82SRobin Holt 58615d94b82SRobin Holt case LINUX_REBOOT_CMD_POWER_OFF: 58715d94b82SRobin Holt kernel_power_off(); 58815d94b82SRobin Holt do_exit(0); 58915d94b82SRobin Holt break; 59015d94b82SRobin Holt 59115d94b82SRobin Holt case LINUX_REBOOT_CMD_RESTART2: 592972ee83dSRobin Holt ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1); 593972ee83dSRobin Holt if (ret < 0) { 59415d94b82SRobin Holt ret = -EFAULT; 59515d94b82SRobin Holt break; 59615d94b82SRobin Holt } 59715d94b82SRobin Holt buffer[sizeof(buffer) - 1] = '\0'; 59815d94b82SRobin Holt 59915d94b82SRobin Holt kernel_restart(buffer); 60015d94b82SRobin Holt break; 60115d94b82SRobin Holt 6022965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 60315d94b82SRobin Holt case LINUX_REBOOT_CMD_KEXEC: 60415d94b82SRobin Holt ret = kernel_kexec(); 60515d94b82SRobin Holt break; 60615d94b82SRobin Holt #endif 60715d94b82SRobin Holt 60815d94b82SRobin Holt #ifdef CONFIG_HIBERNATION 60915d94b82SRobin Holt case LINUX_REBOOT_CMD_SW_SUSPEND: 61015d94b82SRobin Holt ret = hibernate(); 61115d94b82SRobin Holt break; 61215d94b82SRobin Holt #endif 61315d94b82SRobin Holt 61415d94b82SRobin Holt default: 61515d94b82SRobin Holt ret = -EINVAL; 61615d94b82SRobin Holt break; 61715d94b82SRobin Holt } 61855f2503cSPingfan Liu mutex_unlock(&system_transition_mutex); 61915d94b82SRobin Holt return ret; 62015d94b82SRobin Holt } 62115d94b82SRobin Holt 62215d94b82SRobin Holt static void deferred_cad(struct work_struct *dummy) 62315d94b82SRobin Holt { 62415d94b82SRobin Holt kernel_restart(NULL); 62515d94b82SRobin Holt } 62615d94b82SRobin Holt 62715d94b82SRobin Holt /* 62815d94b82SRobin Holt * This function gets called by ctrl-alt-del - ie the keyboard interrupt. 62915d94b82SRobin Holt * As it's called within an interrupt, it may NOT sync: the only choice 63015d94b82SRobin Holt * is whether to reboot at once, or just ignore the ctrl-alt-del. 63115d94b82SRobin Holt */ 63215d94b82SRobin Holt void ctrl_alt_del(void) 63315d94b82SRobin Holt { 63415d94b82SRobin Holt static DECLARE_WORK(cad_work, deferred_cad); 63515d94b82SRobin Holt 63615d94b82SRobin Holt if (C_A_D) 63715d94b82SRobin Holt schedule_work(&cad_work); 63815d94b82SRobin Holt else 63915d94b82SRobin Holt kill_cad_pid(SIGINT, 1); 64015d94b82SRobin Holt } 64115d94b82SRobin Holt 64215d94b82SRobin Holt char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff"; 6437a54f46bSJoel Stanley static const char reboot_cmd[] = "/sbin/reboot"; 64415d94b82SRobin Holt 6457a54f46bSJoel Stanley static int run_cmd(const char *cmd) 64615d94b82SRobin Holt { 64715d94b82SRobin Holt char **argv; 64815d94b82SRobin Holt static char *envp[] = { 64915d94b82SRobin Holt "HOME=/", 65015d94b82SRobin Holt "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 65115d94b82SRobin Holt NULL 65215d94b82SRobin Holt }; 65315d94b82SRobin Holt int ret; 6547a54f46bSJoel Stanley argv = argv_split(GFP_KERNEL, cmd, NULL); 65515d94b82SRobin Holt if (argv) { 65615d94b82SRobin Holt ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); 65715d94b82SRobin Holt argv_free(argv); 65815d94b82SRobin Holt } else { 65915d94b82SRobin Holt ret = -ENOMEM; 66015d94b82SRobin Holt } 66115d94b82SRobin Holt 6627a54f46bSJoel Stanley return ret; 6637a54f46bSJoel Stanley } 6647a54f46bSJoel Stanley 6657a54f46bSJoel Stanley static int __orderly_reboot(void) 6667a54f46bSJoel Stanley { 6677a54f46bSJoel Stanley int ret; 6687a54f46bSJoel Stanley 6697a54f46bSJoel Stanley ret = run_cmd(reboot_cmd); 6707a54f46bSJoel Stanley 6717a54f46bSJoel Stanley if (ret) { 6727a54f46bSJoel Stanley pr_warn("Failed to start orderly reboot: forcing the issue\n"); 6737a54f46bSJoel Stanley emergency_sync(); 6747a54f46bSJoel Stanley kernel_restart(NULL); 6757a54f46bSJoel Stanley } 6767a54f46bSJoel Stanley 6777a54f46bSJoel Stanley return ret; 6787a54f46bSJoel Stanley } 6797a54f46bSJoel Stanley 6807a54f46bSJoel Stanley static int __orderly_poweroff(bool force) 6817a54f46bSJoel Stanley { 6827a54f46bSJoel Stanley int ret; 6837a54f46bSJoel Stanley 6847a54f46bSJoel Stanley ret = run_cmd(poweroff_cmd); 6857a54f46bSJoel Stanley 68615d94b82SRobin Holt if (ret && force) { 687972ee83dSRobin Holt pr_warn("Failed to start orderly shutdown: forcing the issue\n"); 6887a54f46bSJoel Stanley 68915d94b82SRobin Holt /* 69015d94b82SRobin Holt * I guess this should try to kick off some daemon to sync and 69115d94b82SRobin Holt * poweroff asap. Or not even bother syncing if we're doing an 69215d94b82SRobin Holt * emergency shutdown? 69315d94b82SRobin Holt */ 69415d94b82SRobin Holt emergency_sync(); 69515d94b82SRobin Holt kernel_power_off(); 69615d94b82SRobin Holt } 69715d94b82SRobin Holt 69815d94b82SRobin Holt return ret; 69915d94b82SRobin Holt } 70015d94b82SRobin Holt 70115d94b82SRobin Holt static bool poweroff_force; 70215d94b82SRobin Holt 70315d94b82SRobin Holt static void poweroff_work_func(struct work_struct *work) 70415d94b82SRobin Holt { 70515d94b82SRobin Holt __orderly_poweroff(poweroff_force); 70615d94b82SRobin Holt } 70715d94b82SRobin Holt 70815d94b82SRobin Holt static DECLARE_WORK(poweroff_work, poweroff_work_func); 70915d94b82SRobin Holt 71015d94b82SRobin Holt /** 71115d94b82SRobin Holt * orderly_poweroff - Trigger an orderly system poweroff 71215d94b82SRobin Holt * @force: force poweroff if command execution fails 71315d94b82SRobin Holt * 71415d94b82SRobin Holt * This may be called from any context to trigger a system shutdown. 71515d94b82SRobin Holt * If the orderly shutdown fails, it will force an immediate shutdown. 71615d94b82SRobin Holt */ 7177a54f46bSJoel Stanley void orderly_poweroff(bool force) 71815d94b82SRobin Holt { 71915d94b82SRobin Holt if (force) /* do not override the pending "true" */ 72015d94b82SRobin Holt poweroff_force = true; 72115d94b82SRobin Holt schedule_work(&poweroff_work); 72215d94b82SRobin Holt } 72315d94b82SRobin Holt EXPORT_SYMBOL_GPL(orderly_poweroff); 7241b3a5d02SRobin Holt 7257a54f46bSJoel Stanley static void reboot_work_func(struct work_struct *work) 7267a54f46bSJoel Stanley { 7277a54f46bSJoel Stanley __orderly_reboot(); 7287a54f46bSJoel Stanley } 7297a54f46bSJoel Stanley 7307a54f46bSJoel Stanley static DECLARE_WORK(reboot_work, reboot_work_func); 7317a54f46bSJoel Stanley 7327a54f46bSJoel Stanley /** 7337a54f46bSJoel Stanley * orderly_reboot - Trigger an orderly system reboot 7347a54f46bSJoel Stanley * 7357a54f46bSJoel Stanley * This may be called from any context to trigger a system reboot. 7367a54f46bSJoel Stanley * If the orderly reboot fails, it will force an immediate reboot. 7377a54f46bSJoel Stanley */ 7387a54f46bSJoel Stanley void orderly_reboot(void) 7397a54f46bSJoel Stanley { 7407a54f46bSJoel Stanley schedule_work(&reboot_work); 7417a54f46bSJoel Stanley } 7427a54f46bSJoel Stanley EXPORT_SYMBOL_GPL(orderly_reboot); 7437a54f46bSJoel Stanley 744dfa19b11SMatti Vaittinen /** 745dfa19b11SMatti Vaittinen * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay 746dfa19b11SMatti Vaittinen * @work: work_struct associated with the emergency poweroff function 747dfa19b11SMatti Vaittinen * 748dfa19b11SMatti Vaittinen * This function is called in very critical situations to force 749dfa19b11SMatti Vaittinen * a kernel poweroff after a configurable timeout value. 750dfa19b11SMatti Vaittinen */ 751dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff_func(struct work_struct *work) 752dfa19b11SMatti Vaittinen { 753dfa19b11SMatti Vaittinen /* 754dfa19b11SMatti Vaittinen * We have reached here after the emergency shutdown waiting period has 755dfa19b11SMatti Vaittinen * expired. This means orderly_poweroff has not been able to shut off 756dfa19b11SMatti Vaittinen * the system for some reason. 757dfa19b11SMatti Vaittinen * 758dfa19b11SMatti Vaittinen * Try to shut down the system immediately using kernel_power_off 759dfa19b11SMatti Vaittinen * if populated 760dfa19b11SMatti Vaittinen */ 761dfa19b11SMatti Vaittinen pr_emerg("Hardware protection timed-out. Trying forced poweroff\n"); 762dfa19b11SMatti Vaittinen kernel_power_off(); 763dfa19b11SMatti Vaittinen 764dfa19b11SMatti Vaittinen /* 765dfa19b11SMatti Vaittinen * Worst of the worst case trigger emergency restart 766dfa19b11SMatti Vaittinen */ 767dfa19b11SMatti Vaittinen pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n"); 768dfa19b11SMatti Vaittinen emergency_restart(); 769dfa19b11SMatti Vaittinen } 770dfa19b11SMatti Vaittinen 771dfa19b11SMatti Vaittinen static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work, 772dfa19b11SMatti Vaittinen hw_failure_emergency_poweroff_func); 773dfa19b11SMatti Vaittinen 774dfa19b11SMatti Vaittinen /** 775dfa19b11SMatti Vaittinen * hw_failure_emergency_poweroff - Trigger an emergency system poweroff 776dfa19b11SMatti Vaittinen * 777dfa19b11SMatti Vaittinen * This may be called from any critical situation to trigger a system shutdown 778dfa19b11SMatti Vaittinen * after a given period of time. If time is negative this is not scheduled. 779dfa19b11SMatti Vaittinen */ 780dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff(int poweroff_delay_ms) 781dfa19b11SMatti Vaittinen { 782dfa19b11SMatti Vaittinen if (poweroff_delay_ms <= 0) 783dfa19b11SMatti Vaittinen return; 784dfa19b11SMatti Vaittinen schedule_delayed_work(&hw_failure_emergency_poweroff_work, 785dfa19b11SMatti Vaittinen msecs_to_jiffies(poweroff_delay_ms)); 786dfa19b11SMatti Vaittinen } 787dfa19b11SMatti Vaittinen 788dfa19b11SMatti Vaittinen /** 789dfa19b11SMatti Vaittinen * hw_protection_shutdown - Trigger an emergency system poweroff 790dfa19b11SMatti Vaittinen * 791dfa19b11SMatti Vaittinen * @reason: Reason of emergency shutdown to be printed. 792dfa19b11SMatti Vaittinen * @ms_until_forced: Time to wait for orderly shutdown before tiggering a 793dfa19b11SMatti Vaittinen * forced shudown. Negative value disables the forced 794dfa19b11SMatti Vaittinen * shutdown. 795dfa19b11SMatti Vaittinen * 796dfa19b11SMatti Vaittinen * Initiate an emergency system shutdown in order to protect hardware from 797dfa19b11SMatti Vaittinen * further damage. Usage examples include a thermal protection or a voltage or 798dfa19b11SMatti Vaittinen * current regulator failures. 799dfa19b11SMatti Vaittinen * NOTE: The request is ignored if protection shutdown is already pending even 800dfa19b11SMatti Vaittinen * if the previous request has given a large timeout for forced shutdown. 801dfa19b11SMatti Vaittinen * Can be called from any context. 802dfa19b11SMatti Vaittinen */ 803dfa19b11SMatti Vaittinen void hw_protection_shutdown(const char *reason, int ms_until_forced) 804dfa19b11SMatti Vaittinen { 805dfa19b11SMatti Vaittinen static atomic_t allow_proceed = ATOMIC_INIT(1); 806dfa19b11SMatti Vaittinen 807dfa19b11SMatti Vaittinen pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason); 808dfa19b11SMatti Vaittinen 809dfa19b11SMatti Vaittinen /* Shutdown should be initiated only once. */ 810dfa19b11SMatti Vaittinen if (!atomic_dec_and_test(&allow_proceed)) 811dfa19b11SMatti Vaittinen return; 812dfa19b11SMatti Vaittinen 813dfa19b11SMatti Vaittinen /* 814dfa19b11SMatti Vaittinen * Queue a backup emergency shutdown in the event of 815dfa19b11SMatti Vaittinen * orderly_poweroff failure 816dfa19b11SMatti Vaittinen */ 817dfa19b11SMatti Vaittinen hw_failure_emergency_poweroff(ms_until_forced); 818dfa19b11SMatti Vaittinen orderly_poweroff(true); 819dfa19b11SMatti Vaittinen } 820dfa19b11SMatti Vaittinen EXPORT_SYMBOL_GPL(hw_protection_shutdown); 821dfa19b11SMatti Vaittinen 8221b3a5d02SRobin Holt static int __init reboot_setup(char *str) 8231b3a5d02SRobin Holt { 8241b3a5d02SRobin Holt for (;;) { 825b287a25aSAaro Koskinen enum reboot_mode *mode; 826b287a25aSAaro Koskinen 8271b3a5d02SRobin Holt /* 8281b3a5d02SRobin Holt * Having anything passed on the command line via 8291b3a5d02SRobin Holt * reboot= will cause us to disable DMI checking 8301b3a5d02SRobin Holt * below. 8311b3a5d02SRobin Holt */ 8321b3a5d02SRobin Holt reboot_default = 0; 8331b3a5d02SRobin Holt 834b287a25aSAaro Koskinen if (!strncmp(str, "panic_", 6)) { 835b287a25aSAaro Koskinen mode = &panic_reboot_mode; 836b287a25aSAaro Koskinen str += 6; 837b287a25aSAaro Koskinen } else { 838b287a25aSAaro Koskinen mode = &reboot_mode; 839b287a25aSAaro Koskinen } 840b287a25aSAaro Koskinen 8411b3a5d02SRobin Holt switch (*str) { 8421b3a5d02SRobin Holt case 'w': 843b287a25aSAaro Koskinen *mode = REBOOT_WARM; 8441b3a5d02SRobin Holt break; 8451b3a5d02SRobin Holt 8461b3a5d02SRobin Holt case 'c': 847b287a25aSAaro Koskinen *mode = REBOOT_COLD; 8481b3a5d02SRobin Holt break; 8491b3a5d02SRobin Holt 8501b3a5d02SRobin Holt case 'h': 851b287a25aSAaro Koskinen *mode = REBOOT_HARD; 8521b3a5d02SRobin Holt break; 8531b3a5d02SRobin Holt 8541b3a5d02SRobin Holt case 's': 855f9a90501SMatteo Croce /* 856f9a90501SMatteo Croce * reboot_cpu is s[mp]#### with #### being the processor 857f9a90501SMatteo Croce * to be used for rebooting. Skip 's' or 'smp' prefix. 858f9a90501SMatteo Croce */ 859f9a90501SMatteo Croce str += str[1] == 'm' && str[2] == 'p' ? 3 : 1; 860f9a90501SMatteo Croce 861f9a90501SMatteo Croce if (isdigit(str[0])) { 862f9a90501SMatteo Croce int cpu = simple_strtoul(str, NULL, 0); 863f9a90501SMatteo Croce 864f9a90501SMatteo Croce if (cpu >= num_possible_cpus()) { 865df5b0ab3SMatteo Croce pr_err("Ignoring the CPU number in reboot= option. " 866df5b0ab3SMatteo Croce "CPU %d exceeds possible cpu number %d\n", 867f9a90501SMatteo Croce cpu, num_possible_cpus()); 868df5b0ab3SMatteo Croce break; 869df5b0ab3SMatteo Croce } 870f9a90501SMatteo Croce reboot_cpu = cpu; 871f9a90501SMatteo Croce } else 872f9a90501SMatteo Croce *mode = REBOOT_SOFT; 8731b3a5d02SRobin Holt break; 8748b92c4ffSMatteo Croce 8751b3a5d02SRobin Holt case 'g': 876b287a25aSAaro Koskinen *mode = REBOOT_GPIO; 8771b3a5d02SRobin Holt break; 8781b3a5d02SRobin Holt 8791b3a5d02SRobin Holt case 'b': 8801b3a5d02SRobin Holt case 'a': 8811b3a5d02SRobin Holt case 'k': 8821b3a5d02SRobin Holt case 't': 8831b3a5d02SRobin Holt case 'e': 8841b3a5d02SRobin Holt case 'p': 8851b3a5d02SRobin Holt reboot_type = *str; 8861b3a5d02SRobin Holt break; 8871b3a5d02SRobin Holt 8881b3a5d02SRobin Holt case 'f': 8891b3a5d02SRobin Holt reboot_force = 1; 8901b3a5d02SRobin Holt break; 8911b3a5d02SRobin Holt } 8921b3a5d02SRobin Holt 8931b3a5d02SRobin Holt str = strchr(str, ','); 8941b3a5d02SRobin Holt if (str) 8951b3a5d02SRobin Holt str++; 8961b3a5d02SRobin Holt else 8971b3a5d02SRobin Holt break; 8981b3a5d02SRobin Holt } 8991b3a5d02SRobin Holt return 1; 9001b3a5d02SRobin Holt } 9011b3a5d02SRobin Holt __setup("reboot=", reboot_setup); 9022c622ed0SMatteo Croce 9032c622ed0SMatteo Croce #ifdef CONFIG_SYSFS 9042c622ed0SMatteo Croce 9052c622ed0SMatteo Croce #define REBOOT_COLD_STR "cold" 9062c622ed0SMatteo Croce #define REBOOT_WARM_STR "warm" 9072c622ed0SMatteo Croce #define REBOOT_HARD_STR "hard" 9082c622ed0SMatteo Croce #define REBOOT_SOFT_STR "soft" 9092c622ed0SMatteo Croce #define REBOOT_GPIO_STR "gpio" 9102c622ed0SMatteo Croce #define REBOOT_UNDEFINED_STR "undefined" 9112c622ed0SMatteo Croce 9122c622ed0SMatteo Croce #define BOOT_TRIPLE_STR "triple" 9132c622ed0SMatteo Croce #define BOOT_KBD_STR "kbd" 9142c622ed0SMatteo Croce #define BOOT_BIOS_STR "bios" 9152c622ed0SMatteo Croce #define BOOT_ACPI_STR "acpi" 9162c622ed0SMatteo Croce #define BOOT_EFI_STR "efi" 9170c5c0179SMatteo Croce #define BOOT_PCI_STR "pci" 9182c622ed0SMatteo Croce 9192c622ed0SMatteo Croce static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 9202c622ed0SMatteo Croce { 9212c622ed0SMatteo Croce const char *val; 9222c622ed0SMatteo Croce 9232c622ed0SMatteo Croce switch (reboot_mode) { 9242c622ed0SMatteo Croce case REBOOT_COLD: 9252c622ed0SMatteo Croce val = REBOOT_COLD_STR; 9262c622ed0SMatteo Croce break; 9272c622ed0SMatteo Croce case REBOOT_WARM: 9282c622ed0SMatteo Croce val = REBOOT_WARM_STR; 9292c622ed0SMatteo Croce break; 9302c622ed0SMatteo Croce case REBOOT_HARD: 9312c622ed0SMatteo Croce val = REBOOT_HARD_STR; 9322c622ed0SMatteo Croce break; 9332c622ed0SMatteo Croce case REBOOT_SOFT: 9342c622ed0SMatteo Croce val = REBOOT_SOFT_STR; 9352c622ed0SMatteo Croce break; 9362c622ed0SMatteo Croce case REBOOT_GPIO: 9372c622ed0SMatteo Croce val = REBOOT_GPIO_STR; 9382c622ed0SMatteo Croce break; 9392c622ed0SMatteo Croce default: 9402c622ed0SMatteo Croce val = REBOOT_UNDEFINED_STR; 9412c622ed0SMatteo Croce } 9422c622ed0SMatteo Croce 9432c622ed0SMatteo Croce return sprintf(buf, "%s\n", val); 9442c622ed0SMatteo Croce } 9452c622ed0SMatteo Croce static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr, 9462c622ed0SMatteo Croce const char *buf, size_t count) 9472c622ed0SMatteo Croce { 9482c622ed0SMatteo Croce if (!capable(CAP_SYS_BOOT)) 9492c622ed0SMatteo Croce return -EPERM; 9502c622ed0SMatteo Croce 9512c622ed0SMatteo Croce if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR))) 9522c622ed0SMatteo Croce reboot_mode = REBOOT_COLD; 9532c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR))) 9542c622ed0SMatteo Croce reboot_mode = REBOOT_WARM; 9552c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR))) 9562c622ed0SMatteo Croce reboot_mode = REBOOT_HARD; 9572c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR))) 9582c622ed0SMatteo Croce reboot_mode = REBOOT_SOFT; 9592c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR))) 9602c622ed0SMatteo Croce reboot_mode = REBOOT_GPIO; 9612c622ed0SMatteo Croce else 9622c622ed0SMatteo Croce return -EINVAL; 9632c622ed0SMatteo Croce 9641a9d079fSMatteo Croce reboot_default = 0; 9651a9d079fSMatteo Croce 9662c622ed0SMatteo Croce return count; 9672c622ed0SMatteo Croce } 9682c622ed0SMatteo Croce static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode); 9692c622ed0SMatteo Croce 97040247e55SMatteo Croce #ifdef CONFIG_X86 97140247e55SMatteo Croce static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 97240247e55SMatteo Croce { 97340247e55SMatteo Croce return sprintf(buf, "%d\n", reboot_force); 97440247e55SMatteo Croce } 97540247e55SMatteo Croce static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr, 97640247e55SMatteo Croce const char *buf, size_t count) 97740247e55SMatteo Croce { 97840247e55SMatteo Croce bool res; 97940247e55SMatteo Croce 98040247e55SMatteo Croce if (!capable(CAP_SYS_BOOT)) 98140247e55SMatteo Croce return -EPERM; 98240247e55SMatteo Croce 98340247e55SMatteo Croce if (kstrtobool(buf, &res)) 98440247e55SMatteo Croce return -EINVAL; 98540247e55SMatteo Croce 98640247e55SMatteo Croce reboot_default = 0; 98740247e55SMatteo Croce reboot_force = res; 98840247e55SMatteo Croce 98940247e55SMatteo Croce return count; 99040247e55SMatteo Croce } 99140247e55SMatteo Croce static struct kobj_attribute reboot_force_attr = __ATTR_RW(force); 99240247e55SMatteo Croce 9932c622ed0SMatteo Croce static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 9942c622ed0SMatteo Croce { 9952c622ed0SMatteo Croce const char *val; 9962c622ed0SMatteo Croce 9972c622ed0SMatteo Croce switch (reboot_type) { 9982c622ed0SMatteo Croce case BOOT_TRIPLE: 9992c622ed0SMatteo Croce val = BOOT_TRIPLE_STR; 10002c622ed0SMatteo Croce break; 10012c622ed0SMatteo Croce case BOOT_KBD: 10022c622ed0SMatteo Croce val = BOOT_KBD_STR; 10032c622ed0SMatteo Croce break; 10042c622ed0SMatteo Croce case BOOT_BIOS: 10052c622ed0SMatteo Croce val = BOOT_BIOS_STR; 10062c622ed0SMatteo Croce break; 10072c622ed0SMatteo Croce case BOOT_ACPI: 10082c622ed0SMatteo Croce val = BOOT_ACPI_STR; 10092c622ed0SMatteo Croce break; 10102c622ed0SMatteo Croce case BOOT_EFI: 10112c622ed0SMatteo Croce val = BOOT_EFI_STR; 10122c622ed0SMatteo Croce break; 10132c622ed0SMatteo Croce case BOOT_CF9_FORCE: 10140c5c0179SMatteo Croce val = BOOT_PCI_STR; 10152c622ed0SMatteo Croce break; 10162c622ed0SMatteo Croce default: 10172c622ed0SMatteo Croce val = REBOOT_UNDEFINED_STR; 10182c622ed0SMatteo Croce } 10192c622ed0SMatteo Croce 10202c622ed0SMatteo Croce return sprintf(buf, "%s\n", val); 10212c622ed0SMatteo Croce } 10222c622ed0SMatteo Croce static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr, 10232c622ed0SMatteo Croce const char *buf, size_t count) 10242c622ed0SMatteo Croce { 10252c622ed0SMatteo Croce if (!capable(CAP_SYS_BOOT)) 10262c622ed0SMatteo Croce return -EPERM; 10272c622ed0SMatteo Croce 10282c622ed0SMatteo Croce if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR))) 10292c622ed0SMatteo Croce reboot_type = BOOT_TRIPLE; 10302c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR))) 10312c622ed0SMatteo Croce reboot_type = BOOT_KBD; 10322c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR))) 10332c622ed0SMatteo Croce reboot_type = BOOT_BIOS; 10342c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR))) 10352c622ed0SMatteo Croce reboot_type = BOOT_ACPI; 10362c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR))) 10372c622ed0SMatteo Croce reboot_type = BOOT_EFI; 10380c5c0179SMatteo Croce else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR))) 10392c622ed0SMatteo Croce reboot_type = BOOT_CF9_FORCE; 10402c622ed0SMatteo Croce else 10412c622ed0SMatteo Croce return -EINVAL; 10422c622ed0SMatteo Croce 10431a9d079fSMatteo Croce reboot_default = 0; 10441a9d079fSMatteo Croce 10452c622ed0SMatteo Croce return count; 10462c622ed0SMatteo Croce } 10472c622ed0SMatteo Croce static struct kobj_attribute reboot_type_attr = __ATTR_RW(type); 104840247e55SMatteo Croce #endif 10492c622ed0SMatteo Croce 105040247e55SMatteo Croce #ifdef CONFIG_SMP 10512c622ed0SMatteo Croce static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 10522c622ed0SMatteo Croce { 10532c622ed0SMatteo Croce return sprintf(buf, "%d\n", reboot_cpu); 10542c622ed0SMatteo Croce } 10552c622ed0SMatteo Croce static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr, 10562c622ed0SMatteo Croce const char *buf, size_t count) 10572c622ed0SMatteo Croce { 10582c622ed0SMatteo Croce unsigned int cpunum; 10592c622ed0SMatteo Croce int rc; 10602c622ed0SMatteo Croce 10612c622ed0SMatteo Croce if (!capable(CAP_SYS_BOOT)) 10622c622ed0SMatteo Croce return -EPERM; 10632c622ed0SMatteo Croce 10642c622ed0SMatteo Croce rc = kstrtouint(buf, 0, &cpunum); 10652c622ed0SMatteo Croce 10662c622ed0SMatteo Croce if (rc) 10672c622ed0SMatteo Croce return rc; 10682c622ed0SMatteo Croce 10692c622ed0SMatteo Croce if (cpunum >= num_possible_cpus()) 10702c622ed0SMatteo Croce return -ERANGE; 10712c622ed0SMatteo Croce 10721a9d079fSMatteo Croce reboot_default = 0; 10732c622ed0SMatteo Croce reboot_cpu = cpunum; 10742c622ed0SMatteo Croce 10752c622ed0SMatteo Croce return count; 10762c622ed0SMatteo Croce } 10772c622ed0SMatteo Croce static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu); 107840247e55SMatteo Croce #endif 10792c622ed0SMatteo Croce 10802c622ed0SMatteo Croce static struct attribute *reboot_attrs[] = { 10812c622ed0SMatteo Croce &reboot_mode_attr.attr, 108240247e55SMatteo Croce #ifdef CONFIG_X86 10832c622ed0SMatteo Croce &reboot_force_attr.attr, 108440247e55SMatteo Croce &reboot_type_attr.attr, 108540247e55SMatteo Croce #endif 108640247e55SMatteo Croce #ifdef CONFIG_SMP 108740247e55SMatteo Croce &reboot_cpu_attr.attr, 108840247e55SMatteo Croce #endif 10892c622ed0SMatteo Croce NULL, 10902c622ed0SMatteo Croce }; 10912c622ed0SMatteo Croce 10922c622ed0SMatteo Croce static const struct attribute_group reboot_attr_group = { 10932c622ed0SMatteo Croce .attrs = reboot_attrs, 10942c622ed0SMatteo Croce }; 10952c622ed0SMatteo Croce 10962c622ed0SMatteo Croce static int __init reboot_ksysfs_init(void) 10972c622ed0SMatteo Croce { 10982c622ed0SMatteo Croce struct kobject *reboot_kobj; 10992c622ed0SMatteo Croce int ret; 11002c622ed0SMatteo Croce 11012c622ed0SMatteo Croce reboot_kobj = kobject_create_and_add("reboot", kernel_kobj); 11022c622ed0SMatteo Croce if (!reboot_kobj) 11032c622ed0SMatteo Croce return -ENOMEM; 11042c622ed0SMatteo Croce 11052c622ed0SMatteo Croce ret = sysfs_create_group(reboot_kobj, &reboot_attr_group); 11062c622ed0SMatteo Croce if (ret) { 11072c622ed0SMatteo Croce kobject_put(reboot_kobj); 11082c622ed0SMatteo Croce return ret; 11092c622ed0SMatteo Croce } 11102c622ed0SMatteo Croce 11112c622ed0SMatteo Croce return 0; 11122c622ed0SMatteo Croce } 11132c622ed0SMatteo Croce late_initcall(reboot_ksysfs_init); 11142c622ed0SMatteo Croce 11152c622ed0SMatteo Croce #endif 1116