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 4667b9a3de9SDmitry Osipenko static int legacy_pm_power_off_prepare(struct sys_off_data *data) 4677b9a3de9SDmitry Osipenko { 4687b9a3de9SDmitry Osipenko if (pm_power_off_prepare) 4697b9a3de9SDmitry Osipenko pm_power_off_prepare(); 4707b9a3de9SDmitry Osipenko 4717b9a3de9SDmitry Osipenko return NOTIFY_DONE; 4727b9a3de9SDmitry Osipenko } 4737b9a3de9SDmitry Osipenko 4747b9a3de9SDmitry Osipenko static int legacy_pm_power_off(struct sys_off_data *data) 4757b9a3de9SDmitry Osipenko { 4767b9a3de9SDmitry Osipenko if (pm_power_off) 4777b9a3de9SDmitry Osipenko pm_power_off(); 4787b9a3de9SDmitry Osipenko 4797b9a3de9SDmitry Osipenko return NOTIFY_DONE; 4807b9a3de9SDmitry Osipenko } 4817b9a3de9SDmitry Osipenko 4827b9a3de9SDmitry Osipenko /* 4837b9a3de9SDmitry Osipenko * Register sys-off handlers for legacy PM callbacks. This allows legacy 4847b9a3de9SDmitry Osipenko * PM callbacks co-exist with the new sys-off API. 4857b9a3de9SDmitry Osipenko * 4867b9a3de9SDmitry Osipenko * TODO: Remove legacy handlers once all legacy PM users will be switched 4877b9a3de9SDmitry Osipenko * to the sys-off based APIs. 4887b9a3de9SDmitry Osipenko */ 4897b9a3de9SDmitry Osipenko static int __init legacy_pm_init(void) 4907b9a3de9SDmitry Osipenko { 4917b9a3de9SDmitry Osipenko register_sys_off_handler(SYS_OFF_MODE_POWER_OFF_PREPARE, 4927b9a3de9SDmitry Osipenko SYS_OFF_PRIO_DEFAULT, 4937b9a3de9SDmitry Osipenko legacy_pm_power_off_prepare, NULL); 4947b9a3de9SDmitry Osipenko 4957b9a3de9SDmitry Osipenko register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT, 4967b9a3de9SDmitry Osipenko legacy_pm_power_off, NULL); 4977b9a3de9SDmitry Osipenko 4987b9a3de9SDmitry Osipenko return 0; 4997b9a3de9SDmitry Osipenko } 5007b9a3de9SDmitry Osipenko core_initcall(legacy_pm_init); 5017b9a3de9SDmitry Osipenko 5027b9a3de9SDmitry Osipenko static void do_kernel_power_off_prepare(void) 5037b9a3de9SDmitry Osipenko { 5047b9a3de9SDmitry Osipenko blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL); 5057b9a3de9SDmitry Osipenko } 5067b9a3de9SDmitry Osipenko 50715d94b82SRobin Holt /** 508*2b6aa733SDmitry Osipenko * do_kernel_power_off - Execute kernel power-off handler call chain 509*2b6aa733SDmitry Osipenko * 510*2b6aa733SDmitry Osipenko * Expected to be called as last step of the power-off sequence. 511*2b6aa733SDmitry Osipenko * 512*2b6aa733SDmitry Osipenko * Powers off the system immediately if a power-off handler function has 513*2b6aa733SDmitry Osipenko * been registered. Otherwise does nothing. 514*2b6aa733SDmitry Osipenko */ 515*2b6aa733SDmitry Osipenko void do_kernel_power_off(void) 516*2b6aa733SDmitry Osipenko { 517*2b6aa733SDmitry Osipenko atomic_notifier_call_chain(&power_off_handler_list, 0, NULL); 518*2b6aa733SDmitry Osipenko } 519*2b6aa733SDmitry Osipenko 520*2b6aa733SDmitry Osipenko /** 52115d94b82SRobin Holt * kernel_power_off - power_off the system 52215d94b82SRobin Holt * 52315d94b82SRobin Holt * Shutdown everything and perform a clean system power_off. 52415d94b82SRobin Holt */ 52515d94b82SRobin Holt void kernel_power_off(void) 52615d94b82SRobin Holt { 52715d94b82SRobin Holt kernel_shutdown_prepare(SYSTEM_POWER_OFF); 5287b9a3de9SDmitry Osipenko do_kernel_power_off_prepare(); 52915d94b82SRobin Holt migrate_to_reboot_cpu(); 53015d94b82SRobin Holt syscore_shutdown(); 531972ee83dSRobin Holt pr_emerg("Power down\n"); 5326d3cf962SKees Cook kmsg_dump(KMSG_DUMP_SHUTDOWN); 53315d94b82SRobin Holt machine_power_off(); 53415d94b82SRobin Holt } 53515d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_power_off); 53615d94b82SRobin Holt 53755f2503cSPingfan Liu DEFINE_MUTEX(system_transition_mutex); 53815d94b82SRobin Holt 53915d94b82SRobin Holt /* 54015d94b82SRobin Holt * Reboot system call: for obvious reasons only root may call it, 54115d94b82SRobin Holt * and even root needs to set up some magic numbers in the registers 54215d94b82SRobin Holt * so that some mistake won't make this reboot the whole machine. 54315d94b82SRobin Holt * You can also set the meaning of the ctrl-alt-del-key here. 54415d94b82SRobin Holt * 54515d94b82SRobin Holt * reboot doesn't sync: do that yourself before calling this. 54615d94b82SRobin Holt */ 54715d94b82SRobin Holt SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd, 54815d94b82SRobin Holt void __user *, arg) 54915d94b82SRobin Holt { 55015d94b82SRobin Holt struct pid_namespace *pid_ns = task_active_pid_ns(current); 55115d94b82SRobin Holt char buffer[256]; 55215d94b82SRobin Holt int ret = 0; 55315d94b82SRobin Holt 55415d94b82SRobin Holt /* We only trust the superuser with rebooting the system. */ 55515d94b82SRobin Holt if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT)) 55615d94b82SRobin Holt return -EPERM; 55715d94b82SRobin Holt 55815d94b82SRobin Holt /* For safety, we require "magic" arguments. */ 55915d94b82SRobin Holt if (magic1 != LINUX_REBOOT_MAGIC1 || 56015d94b82SRobin Holt (magic2 != LINUX_REBOOT_MAGIC2 && 56115d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2A && 56215d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2B && 56315d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2C)) 56415d94b82SRobin Holt return -EINVAL; 56515d94b82SRobin Holt 56615d94b82SRobin Holt /* 56715d94b82SRobin Holt * If pid namespaces are enabled and the current task is in a child 56815d94b82SRobin Holt * pid_namespace, the command is handled by reboot_pid_ns() which will 56915d94b82SRobin Holt * call do_exit(). 57015d94b82SRobin Holt */ 57115d94b82SRobin Holt ret = reboot_pid_ns(pid_ns, cmd); 57215d94b82SRobin Holt if (ret) 57315d94b82SRobin Holt return ret; 57415d94b82SRobin Holt 57515d94b82SRobin Holt /* Instead of trying to make the power_off code look like 57615d94b82SRobin Holt * halt when pm_power_off is not set do it the easy way. 57715d94b82SRobin Holt */ 57815d94b82SRobin Holt if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off) 57915d94b82SRobin Holt cmd = LINUX_REBOOT_CMD_HALT; 58015d94b82SRobin Holt 58155f2503cSPingfan Liu mutex_lock(&system_transition_mutex); 58215d94b82SRobin Holt switch (cmd) { 58315d94b82SRobin Holt case LINUX_REBOOT_CMD_RESTART: 58415d94b82SRobin Holt kernel_restart(NULL); 58515d94b82SRobin Holt break; 58615d94b82SRobin Holt 58715d94b82SRobin Holt case LINUX_REBOOT_CMD_CAD_ON: 58815d94b82SRobin Holt C_A_D = 1; 58915d94b82SRobin Holt break; 59015d94b82SRobin Holt 59115d94b82SRobin Holt case LINUX_REBOOT_CMD_CAD_OFF: 59215d94b82SRobin Holt C_A_D = 0; 59315d94b82SRobin Holt break; 59415d94b82SRobin Holt 59515d94b82SRobin Holt case LINUX_REBOOT_CMD_HALT: 59615d94b82SRobin Holt kernel_halt(); 59715d94b82SRobin Holt do_exit(0); 59815d94b82SRobin Holt 59915d94b82SRobin Holt case LINUX_REBOOT_CMD_POWER_OFF: 60015d94b82SRobin Holt kernel_power_off(); 60115d94b82SRobin Holt do_exit(0); 60215d94b82SRobin Holt break; 60315d94b82SRobin Holt 60415d94b82SRobin Holt case LINUX_REBOOT_CMD_RESTART2: 605972ee83dSRobin Holt ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1); 606972ee83dSRobin Holt if (ret < 0) { 60715d94b82SRobin Holt ret = -EFAULT; 60815d94b82SRobin Holt break; 60915d94b82SRobin Holt } 61015d94b82SRobin Holt buffer[sizeof(buffer) - 1] = '\0'; 61115d94b82SRobin Holt 61215d94b82SRobin Holt kernel_restart(buffer); 61315d94b82SRobin Holt break; 61415d94b82SRobin Holt 6152965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 61615d94b82SRobin Holt case LINUX_REBOOT_CMD_KEXEC: 61715d94b82SRobin Holt ret = kernel_kexec(); 61815d94b82SRobin Holt break; 61915d94b82SRobin Holt #endif 62015d94b82SRobin Holt 62115d94b82SRobin Holt #ifdef CONFIG_HIBERNATION 62215d94b82SRobin Holt case LINUX_REBOOT_CMD_SW_SUSPEND: 62315d94b82SRobin Holt ret = hibernate(); 62415d94b82SRobin Holt break; 62515d94b82SRobin Holt #endif 62615d94b82SRobin Holt 62715d94b82SRobin Holt default: 62815d94b82SRobin Holt ret = -EINVAL; 62915d94b82SRobin Holt break; 63015d94b82SRobin Holt } 63155f2503cSPingfan Liu mutex_unlock(&system_transition_mutex); 63215d94b82SRobin Holt return ret; 63315d94b82SRobin Holt } 63415d94b82SRobin Holt 63515d94b82SRobin Holt static void deferred_cad(struct work_struct *dummy) 63615d94b82SRobin Holt { 63715d94b82SRobin Holt kernel_restart(NULL); 63815d94b82SRobin Holt } 63915d94b82SRobin Holt 64015d94b82SRobin Holt /* 64115d94b82SRobin Holt * This function gets called by ctrl-alt-del - ie the keyboard interrupt. 64215d94b82SRobin Holt * As it's called within an interrupt, it may NOT sync: the only choice 64315d94b82SRobin Holt * is whether to reboot at once, or just ignore the ctrl-alt-del. 64415d94b82SRobin Holt */ 64515d94b82SRobin Holt void ctrl_alt_del(void) 64615d94b82SRobin Holt { 64715d94b82SRobin Holt static DECLARE_WORK(cad_work, deferred_cad); 64815d94b82SRobin Holt 64915d94b82SRobin Holt if (C_A_D) 65015d94b82SRobin Holt schedule_work(&cad_work); 65115d94b82SRobin Holt else 65215d94b82SRobin Holt kill_cad_pid(SIGINT, 1); 65315d94b82SRobin Holt } 65415d94b82SRobin Holt 65515d94b82SRobin Holt char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff"; 6567a54f46bSJoel Stanley static const char reboot_cmd[] = "/sbin/reboot"; 65715d94b82SRobin Holt 6587a54f46bSJoel Stanley static int run_cmd(const char *cmd) 65915d94b82SRobin Holt { 66015d94b82SRobin Holt char **argv; 66115d94b82SRobin Holt static char *envp[] = { 66215d94b82SRobin Holt "HOME=/", 66315d94b82SRobin Holt "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 66415d94b82SRobin Holt NULL 66515d94b82SRobin Holt }; 66615d94b82SRobin Holt int ret; 6677a54f46bSJoel Stanley argv = argv_split(GFP_KERNEL, cmd, NULL); 66815d94b82SRobin Holt if (argv) { 66915d94b82SRobin Holt ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); 67015d94b82SRobin Holt argv_free(argv); 67115d94b82SRobin Holt } else { 67215d94b82SRobin Holt ret = -ENOMEM; 67315d94b82SRobin Holt } 67415d94b82SRobin Holt 6757a54f46bSJoel Stanley return ret; 6767a54f46bSJoel Stanley } 6777a54f46bSJoel Stanley 6787a54f46bSJoel Stanley static int __orderly_reboot(void) 6797a54f46bSJoel Stanley { 6807a54f46bSJoel Stanley int ret; 6817a54f46bSJoel Stanley 6827a54f46bSJoel Stanley ret = run_cmd(reboot_cmd); 6837a54f46bSJoel Stanley 6847a54f46bSJoel Stanley if (ret) { 6857a54f46bSJoel Stanley pr_warn("Failed to start orderly reboot: forcing the issue\n"); 6867a54f46bSJoel Stanley emergency_sync(); 6877a54f46bSJoel Stanley kernel_restart(NULL); 6887a54f46bSJoel Stanley } 6897a54f46bSJoel Stanley 6907a54f46bSJoel Stanley return ret; 6917a54f46bSJoel Stanley } 6927a54f46bSJoel Stanley 6937a54f46bSJoel Stanley static int __orderly_poweroff(bool force) 6947a54f46bSJoel Stanley { 6957a54f46bSJoel Stanley int ret; 6967a54f46bSJoel Stanley 6977a54f46bSJoel Stanley ret = run_cmd(poweroff_cmd); 6987a54f46bSJoel Stanley 69915d94b82SRobin Holt if (ret && force) { 700972ee83dSRobin Holt pr_warn("Failed to start orderly shutdown: forcing the issue\n"); 7017a54f46bSJoel Stanley 70215d94b82SRobin Holt /* 70315d94b82SRobin Holt * I guess this should try to kick off some daemon to sync and 70415d94b82SRobin Holt * poweroff asap. Or not even bother syncing if we're doing an 70515d94b82SRobin Holt * emergency shutdown? 70615d94b82SRobin Holt */ 70715d94b82SRobin Holt emergency_sync(); 70815d94b82SRobin Holt kernel_power_off(); 70915d94b82SRobin Holt } 71015d94b82SRobin Holt 71115d94b82SRobin Holt return ret; 71215d94b82SRobin Holt } 71315d94b82SRobin Holt 71415d94b82SRobin Holt static bool poweroff_force; 71515d94b82SRobin Holt 71615d94b82SRobin Holt static void poweroff_work_func(struct work_struct *work) 71715d94b82SRobin Holt { 71815d94b82SRobin Holt __orderly_poweroff(poweroff_force); 71915d94b82SRobin Holt } 72015d94b82SRobin Holt 72115d94b82SRobin Holt static DECLARE_WORK(poweroff_work, poweroff_work_func); 72215d94b82SRobin Holt 72315d94b82SRobin Holt /** 72415d94b82SRobin Holt * orderly_poweroff - Trigger an orderly system poweroff 72515d94b82SRobin Holt * @force: force poweroff if command execution fails 72615d94b82SRobin Holt * 72715d94b82SRobin Holt * This may be called from any context to trigger a system shutdown. 72815d94b82SRobin Holt * If the orderly shutdown fails, it will force an immediate shutdown. 72915d94b82SRobin Holt */ 7307a54f46bSJoel Stanley void orderly_poweroff(bool force) 73115d94b82SRobin Holt { 73215d94b82SRobin Holt if (force) /* do not override the pending "true" */ 73315d94b82SRobin Holt poweroff_force = true; 73415d94b82SRobin Holt schedule_work(&poweroff_work); 73515d94b82SRobin Holt } 73615d94b82SRobin Holt EXPORT_SYMBOL_GPL(orderly_poweroff); 7371b3a5d02SRobin Holt 7387a54f46bSJoel Stanley static void reboot_work_func(struct work_struct *work) 7397a54f46bSJoel Stanley { 7407a54f46bSJoel Stanley __orderly_reboot(); 7417a54f46bSJoel Stanley } 7427a54f46bSJoel Stanley 7437a54f46bSJoel Stanley static DECLARE_WORK(reboot_work, reboot_work_func); 7447a54f46bSJoel Stanley 7457a54f46bSJoel Stanley /** 7467a54f46bSJoel Stanley * orderly_reboot - Trigger an orderly system reboot 7477a54f46bSJoel Stanley * 7487a54f46bSJoel Stanley * This may be called from any context to trigger a system reboot. 7497a54f46bSJoel Stanley * If the orderly reboot fails, it will force an immediate reboot. 7507a54f46bSJoel Stanley */ 7517a54f46bSJoel Stanley void orderly_reboot(void) 7527a54f46bSJoel Stanley { 7537a54f46bSJoel Stanley schedule_work(&reboot_work); 7547a54f46bSJoel Stanley } 7557a54f46bSJoel Stanley EXPORT_SYMBOL_GPL(orderly_reboot); 7567a54f46bSJoel Stanley 757dfa19b11SMatti Vaittinen /** 758dfa19b11SMatti Vaittinen * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay 759dfa19b11SMatti Vaittinen * @work: work_struct associated with the emergency poweroff function 760dfa19b11SMatti Vaittinen * 761dfa19b11SMatti Vaittinen * This function is called in very critical situations to force 762dfa19b11SMatti Vaittinen * a kernel poweroff after a configurable timeout value. 763dfa19b11SMatti Vaittinen */ 764dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff_func(struct work_struct *work) 765dfa19b11SMatti Vaittinen { 766dfa19b11SMatti Vaittinen /* 767dfa19b11SMatti Vaittinen * We have reached here after the emergency shutdown waiting period has 768dfa19b11SMatti Vaittinen * expired. This means orderly_poweroff has not been able to shut off 769dfa19b11SMatti Vaittinen * the system for some reason. 770dfa19b11SMatti Vaittinen * 771dfa19b11SMatti Vaittinen * Try to shut down the system immediately using kernel_power_off 772dfa19b11SMatti Vaittinen * if populated 773dfa19b11SMatti Vaittinen */ 774dfa19b11SMatti Vaittinen pr_emerg("Hardware protection timed-out. Trying forced poweroff\n"); 775dfa19b11SMatti Vaittinen kernel_power_off(); 776dfa19b11SMatti Vaittinen 777dfa19b11SMatti Vaittinen /* 778dfa19b11SMatti Vaittinen * Worst of the worst case trigger emergency restart 779dfa19b11SMatti Vaittinen */ 780dfa19b11SMatti Vaittinen pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n"); 781dfa19b11SMatti Vaittinen emergency_restart(); 782dfa19b11SMatti Vaittinen } 783dfa19b11SMatti Vaittinen 784dfa19b11SMatti Vaittinen static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work, 785dfa19b11SMatti Vaittinen hw_failure_emergency_poweroff_func); 786dfa19b11SMatti Vaittinen 787dfa19b11SMatti Vaittinen /** 788dfa19b11SMatti Vaittinen * hw_failure_emergency_poweroff - Trigger an emergency system poweroff 789dfa19b11SMatti Vaittinen * 790dfa19b11SMatti Vaittinen * This may be called from any critical situation to trigger a system shutdown 791dfa19b11SMatti Vaittinen * after a given period of time. If time is negative this is not scheduled. 792dfa19b11SMatti Vaittinen */ 793dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff(int poweroff_delay_ms) 794dfa19b11SMatti Vaittinen { 795dfa19b11SMatti Vaittinen if (poweroff_delay_ms <= 0) 796dfa19b11SMatti Vaittinen return; 797dfa19b11SMatti Vaittinen schedule_delayed_work(&hw_failure_emergency_poweroff_work, 798dfa19b11SMatti Vaittinen msecs_to_jiffies(poweroff_delay_ms)); 799dfa19b11SMatti Vaittinen } 800dfa19b11SMatti Vaittinen 801dfa19b11SMatti Vaittinen /** 802dfa19b11SMatti Vaittinen * hw_protection_shutdown - Trigger an emergency system poweroff 803dfa19b11SMatti Vaittinen * 804dfa19b11SMatti Vaittinen * @reason: Reason of emergency shutdown to be printed. 805dfa19b11SMatti Vaittinen * @ms_until_forced: Time to wait for orderly shutdown before tiggering a 806dfa19b11SMatti Vaittinen * forced shudown. Negative value disables the forced 807dfa19b11SMatti Vaittinen * shutdown. 808dfa19b11SMatti Vaittinen * 809dfa19b11SMatti Vaittinen * Initiate an emergency system shutdown in order to protect hardware from 810dfa19b11SMatti Vaittinen * further damage. Usage examples include a thermal protection or a voltage or 811dfa19b11SMatti Vaittinen * current regulator failures. 812dfa19b11SMatti Vaittinen * NOTE: The request is ignored if protection shutdown is already pending even 813dfa19b11SMatti Vaittinen * if the previous request has given a large timeout for forced shutdown. 814dfa19b11SMatti Vaittinen * Can be called from any context. 815dfa19b11SMatti Vaittinen */ 816dfa19b11SMatti Vaittinen void hw_protection_shutdown(const char *reason, int ms_until_forced) 817dfa19b11SMatti Vaittinen { 818dfa19b11SMatti Vaittinen static atomic_t allow_proceed = ATOMIC_INIT(1); 819dfa19b11SMatti Vaittinen 820dfa19b11SMatti Vaittinen pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason); 821dfa19b11SMatti Vaittinen 822dfa19b11SMatti Vaittinen /* Shutdown should be initiated only once. */ 823dfa19b11SMatti Vaittinen if (!atomic_dec_and_test(&allow_proceed)) 824dfa19b11SMatti Vaittinen return; 825dfa19b11SMatti Vaittinen 826dfa19b11SMatti Vaittinen /* 827dfa19b11SMatti Vaittinen * Queue a backup emergency shutdown in the event of 828dfa19b11SMatti Vaittinen * orderly_poweroff failure 829dfa19b11SMatti Vaittinen */ 830dfa19b11SMatti Vaittinen hw_failure_emergency_poweroff(ms_until_forced); 831dfa19b11SMatti Vaittinen orderly_poweroff(true); 832dfa19b11SMatti Vaittinen } 833dfa19b11SMatti Vaittinen EXPORT_SYMBOL_GPL(hw_protection_shutdown); 834dfa19b11SMatti Vaittinen 8351b3a5d02SRobin Holt static int __init reboot_setup(char *str) 8361b3a5d02SRobin Holt { 8371b3a5d02SRobin Holt for (;;) { 838b287a25aSAaro Koskinen enum reboot_mode *mode; 839b287a25aSAaro Koskinen 8401b3a5d02SRobin Holt /* 8411b3a5d02SRobin Holt * Having anything passed on the command line via 8421b3a5d02SRobin Holt * reboot= will cause us to disable DMI checking 8431b3a5d02SRobin Holt * below. 8441b3a5d02SRobin Holt */ 8451b3a5d02SRobin Holt reboot_default = 0; 8461b3a5d02SRobin Holt 847b287a25aSAaro Koskinen if (!strncmp(str, "panic_", 6)) { 848b287a25aSAaro Koskinen mode = &panic_reboot_mode; 849b287a25aSAaro Koskinen str += 6; 850b287a25aSAaro Koskinen } else { 851b287a25aSAaro Koskinen mode = &reboot_mode; 852b287a25aSAaro Koskinen } 853b287a25aSAaro Koskinen 8541b3a5d02SRobin Holt switch (*str) { 8551b3a5d02SRobin Holt case 'w': 856b287a25aSAaro Koskinen *mode = REBOOT_WARM; 8571b3a5d02SRobin Holt break; 8581b3a5d02SRobin Holt 8591b3a5d02SRobin Holt case 'c': 860b287a25aSAaro Koskinen *mode = REBOOT_COLD; 8611b3a5d02SRobin Holt break; 8621b3a5d02SRobin Holt 8631b3a5d02SRobin Holt case 'h': 864b287a25aSAaro Koskinen *mode = REBOOT_HARD; 8651b3a5d02SRobin Holt break; 8661b3a5d02SRobin Holt 8671b3a5d02SRobin Holt case 's': 868f9a90501SMatteo Croce /* 869f9a90501SMatteo Croce * reboot_cpu is s[mp]#### with #### being the processor 870f9a90501SMatteo Croce * to be used for rebooting. Skip 's' or 'smp' prefix. 871f9a90501SMatteo Croce */ 872f9a90501SMatteo Croce str += str[1] == 'm' && str[2] == 'p' ? 3 : 1; 873f9a90501SMatteo Croce 874f9a90501SMatteo Croce if (isdigit(str[0])) { 875f9a90501SMatteo Croce int cpu = simple_strtoul(str, NULL, 0); 876f9a90501SMatteo Croce 877f9a90501SMatteo Croce if (cpu >= num_possible_cpus()) { 878df5b0ab3SMatteo Croce pr_err("Ignoring the CPU number in reboot= option. " 879df5b0ab3SMatteo Croce "CPU %d exceeds possible cpu number %d\n", 880f9a90501SMatteo Croce cpu, num_possible_cpus()); 881df5b0ab3SMatteo Croce break; 882df5b0ab3SMatteo Croce } 883f9a90501SMatteo Croce reboot_cpu = cpu; 884f9a90501SMatteo Croce } else 885f9a90501SMatteo Croce *mode = REBOOT_SOFT; 8861b3a5d02SRobin Holt break; 8878b92c4ffSMatteo Croce 8881b3a5d02SRobin Holt case 'g': 889b287a25aSAaro Koskinen *mode = REBOOT_GPIO; 8901b3a5d02SRobin Holt break; 8911b3a5d02SRobin Holt 8921b3a5d02SRobin Holt case 'b': 8931b3a5d02SRobin Holt case 'a': 8941b3a5d02SRobin Holt case 'k': 8951b3a5d02SRobin Holt case 't': 8961b3a5d02SRobin Holt case 'e': 8971b3a5d02SRobin Holt case 'p': 8981b3a5d02SRobin Holt reboot_type = *str; 8991b3a5d02SRobin Holt break; 9001b3a5d02SRobin Holt 9011b3a5d02SRobin Holt case 'f': 9021b3a5d02SRobin Holt reboot_force = 1; 9031b3a5d02SRobin Holt break; 9041b3a5d02SRobin Holt } 9051b3a5d02SRobin Holt 9061b3a5d02SRobin Holt str = strchr(str, ','); 9071b3a5d02SRobin Holt if (str) 9081b3a5d02SRobin Holt str++; 9091b3a5d02SRobin Holt else 9101b3a5d02SRobin Holt break; 9111b3a5d02SRobin Holt } 9121b3a5d02SRobin Holt return 1; 9131b3a5d02SRobin Holt } 9141b3a5d02SRobin Holt __setup("reboot=", reboot_setup); 9152c622ed0SMatteo Croce 9162c622ed0SMatteo Croce #ifdef CONFIG_SYSFS 9172c622ed0SMatteo Croce 9182c622ed0SMatteo Croce #define REBOOT_COLD_STR "cold" 9192c622ed0SMatteo Croce #define REBOOT_WARM_STR "warm" 9202c622ed0SMatteo Croce #define REBOOT_HARD_STR "hard" 9212c622ed0SMatteo Croce #define REBOOT_SOFT_STR "soft" 9222c622ed0SMatteo Croce #define REBOOT_GPIO_STR "gpio" 9232c622ed0SMatteo Croce #define REBOOT_UNDEFINED_STR "undefined" 9242c622ed0SMatteo Croce 9252c622ed0SMatteo Croce #define BOOT_TRIPLE_STR "triple" 9262c622ed0SMatteo Croce #define BOOT_KBD_STR "kbd" 9272c622ed0SMatteo Croce #define BOOT_BIOS_STR "bios" 9282c622ed0SMatteo Croce #define BOOT_ACPI_STR "acpi" 9292c622ed0SMatteo Croce #define BOOT_EFI_STR "efi" 9300c5c0179SMatteo Croce #define BOOT_PCI_STR "pci" 9312c622ed0SMatteo Croce 9322c622ed0SMatteo Croce static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 9332c622ed0SMatteo Croce { 9342c622ed0SMatteo Croce const char *val; 9352c622ed0SMatteo Croce 9362c622ed0SMatteo Croce switch (reboot_mode) { 9372c622ed0SMatteo Croce case REBOOT_COLD: 9382c622ed0SMatteo Croce val = REBOOT_COLD_STR; 9392c622ed0SMatteo Croce break; 9402c622ed0SMatteo Croce case REBOOT_WARM: 9412c622ed0SMatteo Croce val = REBOOT_WARM_STR; 9422c622ed0SMatteo Croce break; 9432c622ed0SMatteo Croce case REBOOT_HARD: 9442c622ed0SMatteo Croce val = REBOOT_HARD_STR; 9452c622ed0SMatteo Croce break; 9462c622ed0SMatteo Croce case REBOOT_SOFT: 9472c622ed0SMatteo Croce val = REBOOT_SOFT_STR; 9482c622ed0SMatteo Croce break; 9492c622ed0SMatteo Croce case REBOOT_GPIO: 9502c622ed0SMatteo Croce val = REBOOT_GPIO_STR; 9512c622ed0SMatteo Croce break; 9522c622ed0SMatteo Croce default: 9532c622ed0SMatteo Croce val = REBOOT_UNDEFINED_STR; 9542c622ed0SMatteo Croce } 9552c622ed0SMatteo Croce 9562c622ed0SMatteo Croce return sprintf(buf, "%s\n", val); 9572c622ed0SMatteo Croce } 9582c622ed0SMatteo Croce static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr, 9592c622ed0SMatteo Croce const char *buf, size_t count) 9602c622ed0SMatteo Croce { 9612c622ed0SMatteo Croce if (!capable(CAP_SYS_BOOT)) 9622c622ed0SMatteo Croce return -EPERM; 9632c622ed0SMatteo Croce 9642c622ed0SMatteo Croce if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR))) 9652c622ed0SMatteo Croce reboot_mode = REBOOT_COLD; 9662c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR))) 9672c622ed0SMatteo Croce reboot_mode = REBOOT_WARM; 9682c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR))) 9692c622ed0SMatteo Croce reboot_mode = REBOOT_HARD; 9702c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR))) 9712c622ed0SMatteo Croce reboot_mode = REBOOT_SOFT; 9722c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR))) 9732c622ed0SMatteo Croce reboot_mode = REBOOT_GPIO; 9742c622ed0SMatteo Croce else 9752c622ed0SMatteo Croce return -EINVAL; 9762c622ed0SMatteo Croce 9771a9d079fSMatteo Croce reboot_default = 0; 9781a9d079fSMatteo Croce 9792c622ed0SMatteo Croce return count; 9802c622ed0SMatteo Croce } 9812c622ed0SMatteo Croce static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode); 9822c622ed0SMatteo Croce 98340247e55SMatteo Croce #ifdef CONFIG_X86 98440247e55SMatteo Croce static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 98540247e55SMatteo Croce { 98640247e55SMatteo Croce return sprintf(buf, "%d\n", reboot_force); 98740247e55SMatteo Croce } 98840247e55SMatteo Croce static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr, 98940247e55SMatteo Croce const char *buf, size_t count) 99040247e55SMatteo Croce { 99140247e55SMatteo Croce bool res; 99240247e55SMatteo Croce 99340247e55SMatteo Croce if (!capable(CAP_SYS_BOOT)) 99440247e55SMatteo Croce return -EPERM; 99540247e55SMatteo Croce 99640247e55SMatteo Croce if (kstrtobool(buf, &res)) 99740247e55SMatteo Croce return -EINVAL; 99840247e55SMatteo Croce 99940247e55SMatteo Croce reboot_default = 0; 100040247e55SMatteo Croce reboot_force = res; 100140247e55SMatteo Croce 100240247e55SMatteo Croce return count; 100340247e55SMatteo Croce } 100440247e55SMatteo Croce static struct kobj_attribute reboot_force_attr = __ATTR_RW(force); 100540247e55SMatteo Croce 10062c622ed0SMatteo Croce static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 10072c622ed0SMatteo Croce { 10082c622ed0SMatteo Croce const char *val; 10092c622ed0SMatteo Croce 10102c622ed0SMatteo Croce switch (reboot_type) { 10112c622ed0SMatteo Croce case BOOT_TRIPLE: 10122c622ed0SMatteo Croce val = BOOT_TRIPLE_STR; 10132c622ed0SMatteo Croce break; 10142c622ed0SMatteo Croce case BOOT_KBD: 10152c622ed0SMatteo Croce val = BOOT_KBD_STR; 10162c622ed0SMatteo Croce break; 10172c622ed0SMatteo Croce case BOOT_BIOS: 10182c622ed0SMatteo Croce val = BOOT_BIOS_STR; 10192c622ed0SMatteo Croce break; 10202c622ed0SMatteo Croce case BOOT_ACPI: 10212c622ed0SMatteo Croce val = BOOT_ACPI_STR; 10222c622ed0SMatteo Croce break; 10232c622ed0SMatteo Croce case BOOT_EFI: 10242c622ed0SMatteo Croce val = BOOT_EFI_STR; 10252c622ed0SMatteo Croce break; 10262c622ed0SMatteo Croce case BOOT_CF9_FORCE: 10270c5c0179SMatteo Croce val = BOOT_PCI_STR; 10282c622ed0SMatteo Croce break; 10292c622ed0SMatteo Croce default: 10302c622ed0SMatteo Croce val = REBOOT_UNDEFINED_STR; 10312c622ed0SMatteo Croce } 10322c622ed0SMatteo Croce 10332c622ed0SMatteo Croce return sprintf(buf, "%s\n", val); 10342c622ed0SMatteo Croce } 10352c622ed0SMatteo Croce static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr, 10362c622ed0SMatteo Croce const char *buf, size_t count) 10372c622ed0SMatteo Croce { 10382c622ed0SMatteo Croce if (!capable(CAP_SYS_BOOT)) 10392c622ed0SMatteo Croce return -EPERM; 10402c622ed0SMatteo Croce 10412c622ed0SMatteo Croce if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR))) 10422c622ed0SMatteo Croce reboot_type = BOOT_TRIPLE; 10432c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR))) 10442c622ed0SMatteo Croce reboot_type = BOOT_KBD; 10452c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR))) 10462c622ed0SMatteo Croce reboot_type = BOOT_BIOS; 10472c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR))) 10482c622ed0SMatteo Croce reboot_type = BOOT_ACPI; 10492c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR))) 10502c622ed0SMatteo Croce reboot_type = BOOT_EFI; 10510c5c0179SMatteo Croce else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR))) 10522c622ed0SMatteo Croce reboot_type = BOOT_CF9_FORCE; 10532c622ed0SMatteo Croce else 10542c622ed0SMatteo Croce return -EINVAL; 10552c622ed0SMatteo Croce 10561a9d079fSMatteo Croce reboot_default = 0; 10571a9d079fSMatteo Croce 10582c622ed0SMatteo Croce return count; 10592c622ed0SMatteo Croce } 10602c622ed0SMatteo Croce static struct kobj_attribute reboot_type_attr = __ATTR_RW(type); 106140247e55SMatteo Croce #endif 10622c622ed0SMatteo Croce 106340247e55SMatteo Croce #ifdef CONFIG_SMP 10642c622ed0SMatteo Croce static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 10652c622ed0SMatteo Croce { 10662c622ed0SMatteo Croce return sprintf(buf, "%d\n", reboot_cpu); 10672c622ed0SMatteo Croce } 10682c622ed0SMatteo Croce static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr, 10692c622ed0SMatteo Croce const char *buf, size_t count) 10702c622ed0SMatteo Croce { 10712c622ed0SMatteo Croce unsigned int cpunum; 10722c622ed0SMatteo Croce int rc; 10732c622ed0SMatteo Croce 10742c622ed0SMatteo Croce if (!capable(CAP_SYS_BOOT)) 10752c622ed0SMatteo Croce return -EPERM; 10762c622ed0SMatteo Croce 10772c622ed0SMatteo Croce rc = kstrtouint(buf, 0, &cpunum); 10782c622ed0SMatteo Croce 10792c622ed0SMatteo Croce if (rc) 10802c622ed0SMatteo Croce return rc; 10812c622ed0SMatteo Croce 10822c622ed0SMatteo Croce if (cpunum >= num_possible_cpus()) 10832c622ed0SMatteo Croce return -ERANGE; 10842c622ed0SMatteo Croce 10851a9d079fSMatteo Croce reboot_default = 0; 10862c622ed0SMatteo Croce reboot_cpu = cpunum; 10872c622ed0SMatteo Croce 10882c622ed0SMatteo Croce return count; 10892c622ed0SMatteo Croce } 10902c622ed0SMatteo Croce static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu); 109140247e55SMatteo Croce #endif 10922c622ed0SMatteo Croce 10932c622ed0SMatteo Croce static struct attribute *reboot_attrs[] = { 10942c622ed0SMatteo Croce &reboot_mode_attr.attr, 109540247e55SMatteo Croce #ifdef CONFIG_X86 10962c622ed0SMatteo Croce &reboot_force_attr.attr, 109740247e55SMatteo Croce &reboot_type_attr.attr, 109840247e55SMatteo Croce #endif 109940247e55SMatteo Croce #ifdef CONFIG_SMP 110040247e55SMatteo Croce &reboot_cpu_attr.attr, 110140247e55SMatteo Croce #endif 11022c622ed0SMatteo Croce NULL, 11032c622ed0SMatteo Croce }; 11042c622ed0SMatteo Croce 11052c622ed0SMatteo Croce static const struct attribute_group reboot_attr_group = { 11062c622ed0SMatteo Croce .attrs = reboot_attrs, 11072c622ed0SMatteo Croce }; 11082c622ed0SMatteo Croce 11092c622ed0SMatteo Croce static int __init reboot_ksysfs_init(void) 11102c622ed0SMatteo Croce { 11112c622ed0SMatteo Croce struct kobject *reboot_kobj; 11122c622ed0SMatteo Croce int ret; 11132c622ed0SMatteo Croce 11142c622ed0SMatteo Croce reboot_kobj = kobject_create_and_add("reboot", kernel_kobj); 11152c622ed0SMatteo Croce if (!reboot_kobj) 11162c622ed0SMatteo Croce return -ENOMEM; 11172c622ed0SMatteo Croce 11182c622ed0SMatteo Croce ret = sysfs_create_group(reboot_kobj, &reboot_attr_group); 11192c622ed0SMatteo Croce if (ret) { 11202c622ed0SMatteo Croce kobject_put(reboot_kobj); 11212c622ed0SMatteo Croce return ret; 11222c622ed0SMatteo Croce } 11232c622ed0SMatteo Croce 11242c622ed0SMatteo Croce return 0; 11252c622ed0SMatteo Croce } 11262c622ed0SMatteo Croce late_initcall(reboot_ksysfs_init); 11272c622ed0SMatteo Croce 11282c622ed0SMatteo Croce #endif 1129