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 /* 61*5d34b41aSDmitry Osipenko * Temporary stub that prevents linkage failure while we're in process 62*5d34b41aSDmitry Osipenko * of removing all uses of legacy pm_power_off() around the kernel. 63*5d34b41aSDmitry Osipenko */ 64*5d34b41aSDmitry Osipenko void __weak (*pm_power_off)(void); 65*5d34b41aSDmitry Osipenko 66*5d34b41aSDmitry Osipenko /* 6715d94b82SRobin Holt * If set, this is used for preparing the system to power off. 6815d94b82SRobin Holt */ 6915d94b82SRobin Holt 7015d94b82SRobin Holt void (*pm_power_off_prepare)(void); 7174f008f2SOleksij Rempel EXPORT_SYMBOL_GPL(pm_power_off_prepare); 7215d94b82SRobin Holt 7315d94b82SRobin Holt /** 7415d94b82SRobin Holt * emergency_restart - reboot the system 7515d94b82SRobin Holt * 7615d94b82SRobin Holt * Without shutting down any hardware or taking any locks 7715d94b82SRobin Holt * reboot the system. This is called when we know we are in 7815d94b82SRobin Holt * trouble so this is our best effort to reboot. This is 7915d94b82SRobin Holt * safe to call in interrupt context. 8015d94b82SRobin Holt */ 8115d94b82SRobin Holt void emergency_restart(void) 8215d94b82SRobin Holt { 8315d94b82SRobin Holt kmsg_dump(KMSG_DUMP_EMERG); 8415d94b82SRobin Holt machine_emergency_restart(); 8515d94b82SRobin Holt } 8615d94b82SRobin Holt EXPORT_SYMBOL_GPL(emergency_restart); 8715d94b82SRobin Holt 8815d94b82SRobin Holt void kernel_restart_prepare(char *cmd) 8915d94b82SRobin Holt { 9015d94b82SRobin Holt blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); 9115d94b82SRobin Holt system_state = SYSTEM_RESTART; 9215d94b82SRobin Holt usermodehelper_disable(); 9315d94b82SRobin Holt device_shutdown(); 9415d94b82SRobin Holt } 9515d94b82SRobin Holt 9615d94b82SRobin Holt /** 9715d94b82SRobin Holt * register_reboot_notifier - Register function to be called at reboot time 9815d94b82SRobin Holt * @nb: Info about notifier function to be called 9915d94b82SRobin Holt * 10015d94b82SRobin Holt * Registers a function with the list of functions 10115d94b82SRobin Holt * to be called at reboot time. 10215d94b82SRobin Holt * 10315d94b82SRobin Holt * Currently always returns zero, as blocking_notifier_chain_register() 10415d94b82SRobin Holt * always returns zero. 10515d94b82SRobin Holt */ 10615d94b82SRobin Holt int register_reboot_notifier(struct notifier_block *nb) 10715d94b82SRobin Holt { 10815d94b82SRobin Holt return blocking_notifier_chain_register(&reboot_notifier_list, nb); 10915d94b82SRobin Holt } 11015d94b82SRobin Holt EXPORT_SYMBOL(register_reboot_notifier); 11115d94b82SRobin Holt 11215d94b82SRobin Holt /** 11315d94b82SRobin Holt * unregister_reboot_notifier - Unregister previously registered reboot notifier 11415d94b82SRobin Holt * @nb: Hook to be unregistered 11515d94b82SRobin Holt * 11615d94b82SRobin Holt * Unregisters a previously registered reboot 11715d94b82SRobin Holt * notifier function. 11815d94b82SRobin Holt * 11915d94b82SRobin Holt * Returns zero on success, or %-ENOENT on failure. 12015d94b82SRobin Holt */ 12115d94b82SRobin Holt int unregister_reboot_notifier(struct notifier_block *nb) 12215d94b82SRobin Holt { 12315d94b82SRobin Holt return blocking_notifier_chain_unregister(&reboot_notifier_list, nb); 12415d94b82SRobin Holt } 12515d94b82SRobin Holt EXPORT_SYMBOL(unregister_reboot_notifier); 12615d94b82SRobin Holt 1272d8364baSAndrey Smirnov static void devm_unregister_reboot_notifier(struct device *dev, void *res) 1282d8364baSAndrey Smirnov { 1292d8364baSAndrey Smirnov WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res)); 1302d8364baSAndrey Smirnov } 1312d8364baSAndrey Smirnov 1322d8364baSAndrey Smirnov int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb) 1332d8364baSAndrey Smirnov { 1342d8364baSAndrey Smirnov struct notifier_block **rcnb; 1352d8364baSAndrey Smirnov int ret; 1362d8364baSAndrey Smirnov 1372d8364baSAndrey Smirnov rcnb = devres_alloc(devm_unregister_reboot_notifier, 1382d8364baSAndrey Smirnov sizeof(*rcnb), GFP_KERNEL); 1392d8364baSAndrey Smirnov if (!rcnb) 1402d8364baSAndrey Smirnov return -ENOMEM; 1412d8364baSAndrey Smirnov 1422d8364baSAndrey Smirnov ret = register_reboot_notifier(nb); 1432d8364baSAndrey Smirnov if (!ret) { 1442d8364baSAndrey Smirnov *rcnb = nb; 1452d8364baSAndrey Smirnov devres_add(dev, rcnb); 1462d8364baSAndrey Smirnov } else { 1472d8364baSAndrey Smirnov devres_free(rcnb); 1482d8364baSAndrey Smirnov } 1492d8364baSAndrey Smirnov 1502d8364baSAndrey Smirnov return ret; 1512d8364baSAndrey Smirnov } 1522d8364baSAndrey Smirnov EXPORT_SYMBOL(devm_register_reboot_notifier); 1532d8364baSAndrey Smirnov 154b63adb97SGuenter Roeck /* 155b63adb97SGuenter Roeck * Notifier list for kernel code which wants to be called 156b63adb97SGuenter Roeck * to restart the system. 157b63adb97SGuenter Roeck */ 158b63adb97SGuenter Roeck static ATOMIC_NOTIFIER_HEAD(restart_handler_list); 159b63adb97SGuenter Roeck 160b63adb97SGuenter Roeck /** 161b63adb97SGuenter Roeck * register_restart_handler - Register function to be called to reset 162b63adb97SGuenter Roeck * the system 163b63adb97SGuenter Roeck * @nb: Info about handler function to be called 164b63adb97SGuenter Roeck * @nb->priority: Handler priority. Handlers should follow the 165b63adb97SGuenter Roeck * following guidelines for setting priorities. 166b63adb97SGuenter Roeck * 0: Restart handler of last resort, 167b63adb97SGuenter Roeck * with limited restart capabilities 168b63adb97SGuenter Roeck * 128: Default restart handler; use if no other 169b63adb97SGuenter Roeck * restart handler is expected to be available, 170b63adb97SGuenter Roeck * and/or if restart functionality is 171b63adb97SGuenter Roeck * sufficient to restart the entire system 172b63adb97SGuenter Roeck * 255: Highest priority restart handler, will 173b63adb97SGuenter Roeck * preempt all other restart handlers 174b63adb97SGuenter Roeck * 175b63adb97SGuenter Roeck * Registers a function with code to be called to restart the 176b63adb97SGuenter Roeck * system. 177b63adb97SGuenter Roeck * 178b63adb97SGuenter Roeck * Registered functions will be called from machine_restart as last 179b63adb97SGuenter Roeck * step of the restart sequence (if the architecture specific 180b63adb97SGuenter Roeck * machine_restart function calls do_kernel_restart - see below 181b63adb97SGuenter Roeck * for details). 182b63adb97SGuenter Roeck * Registered functions are expected to restart the system immediately. 183b63adb97SGuenter Roeck * If more than one function is registered, the restart handler priority 184b63adb97SGuenter Roeck * selects which function will be called first. 185b63adb97SGuenter Roeck * 186b63adb97SGuenter Roeck * Restart handlers are expected to be registered from non-architecture 187b63adb97SGuenter Roeck * code, typically from drivers. A typical use case would be a system 188b63adb97SGuenter Roeck * where restart functionality is provided through a watchdog. Multiple 189b63adb97SGuenter Roeck * restart handlers may exist; for example, one restart handler might 190b63adb97SGuenter Roeck * restart the entire system, while another only restarts the CPU. 191b63adb97SGuenter Roeck * In such cases, the restart handler which only restarts part of the 192b63adb97SGuenter Roeck * hardware is expected to register with low priority to ensure that 193b63adb97SGuenter Roeck * it only runs if no other means to restart the system is available. 194b63adb97SGuenter Roeck * 195b63adb97SGuenter Roeck * Currently always returns zero, as atomic_notifier_chain_register() 196b63adb97SGuenter Roeck * always returns zero. 197b63adb97SGuenter Roeck */ 198b63adb97SGuenter Roeck int register_restart_handler(struct notifier_block *nb) 199b63adb97SGuenter Roeck { 200b63adb97SGuenter Roeck return atomic_notifier_chain_register(&restart_handler_list, nb); 201b63adb97SGuenter Roeck } 202b63adb97SGuenter Roeck EXPORT_SYMBOL(register_restart_handler); 203b63adb97SGuenter Roeck 204b63adb97SGuenter Roeck /** 205b63adb97SGuenter Roeck * unregister_restart_handler - Unregister previously registered 206b63adb97SGuenter Roeck * restart handler 207b63adb97SGuenter Roeck * @nb: Hook to be unregistered 208b63adb97SGuenter Roeck * 209b63adb97SGuenter Roeck * Unregisters a previously registered restart handler function. 210b63adb97SGuenter Roeck * 211b63adb97SGuenter Roeck * Returns zero on success, or %-ENOENT on failure. 212b63adb97SGuenter Roeck */ 213b63adb97SGuenter Roeck int unregister_restart_handler(struct notifier_block *nb) 214b63adb97SGuenter Roeck { 215b63adb97SGuenter Roeck return atomic_notifier_chain_unregister(&restart_handler_list, nb); 216b63adb97SGuenter Roeck } 217b63adb97SGuenter Roeck EXPORT_SYMBOL(unregister_restart_handler); 218b63adb97SGuenter Roeck 219b63adb97SGuenter Roeck /** 220b63adb97SGuenter Roeck * do_kernel_restart - Execute kernel restart handler call chain 221b63adb97SGuenter Roeck * 222b63adb97SGuenter Roeck * Calls functions registered with register_restart_handler. 223b63adb97SGuenter Roeck * 224b63adb97SGuenter Roeck * Expected to be called from machine_restart as last step of the restart 225b63adb97SGuenter Roeck * sequence. 226b63adb97SGuenter Roeck * 227b63adb97SGuenter Roeck * Restarts the system immediately if a restart handler function has been 228b63adb97SGuenter Roeck * registered. Otherwise does nothing. 229b63adb97SGuenter Roeck */ 230b63adb97SGuenter Roeck void do_kernel_restart(char *cmd) 231b63adb97SGuenter Roeck { 232b63adb97SGuenter Roeck atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd); 233b63adb97SGuenter Roeck } 234b63adb97SGuenter Roeck 235c97102baSVivek Goyal void migrate_to_reboot_cpu(void) 23615d94b82SRobin Holt { 23715d94b82SRobin Holt /* The boot cpu is always logical cpu 0 */ 2381b3a5d02SRobin Holt int cpu = reboot_cpu; 23915d94b82SRobin Holt 24015d94b82SRobin Holt cpu_hotplug_disable(); 24115d94b82SRobin Holt 24215d94b82SRobin Holt /* Make certain the cpu I'm about to reboot on is online */ 24315d94b82SRobin Holt if (!cpu_online(cpu)) 24415d94b82SRobin Holt cpu = cpumask_first(cpu_online_mask); 24515d94b82SRobin Holt 24615d94b82SRobin Holt /* Prevent races with other tasks migrating this task */ 24715d94b82SRobin Holt current->flags |= PF_NO_SETAFFINITY; 24815d94b82SRobin Holt 24915d94b82SRobin Holt /* Make certain I only run on the appropriate processor */ 25015d94b82SRobin Holt set_cpus_allowed_ptr(current, cpumask_of(cpu)); 25115d94b82SRobin Holt } 25215d94b82SRobin Holt 25315d94b82SRobin Holt /** 25415d94b82SRobin Holt * kernel_restart - reboot the system 25515d94b82SRobin Holt * @cmd: pointer to buffer containing command to execute for restart 25615d94b82SRobin Holt * or %NULL 25715d94b82SRobin Holt * 25815d94b82SRobin Holt * Shutdown everything and perform a clean reboot. 25915d94b82SRobin Holt * This is not safe to call in interrupt context. 26015d94b82SRobin Holt */ 26115d94b82SRobin Holt void kernel_restart(char *cmd) 26215d94b82SRobin Holt { 26315d94b82SRobin Holt kernel_restart_prepare(cmd); 26415d94b82SRobin Holt migrate_to_reboot_cpu(); 26515d94b82SRobin Holt syscore_shutdown(); 26615d94b82SRobin Holt if (!cmd) 267972ee83dSRobin Holt pr_emerg("Restarting system\n"); 26815d94b82SRobin Holt else 269972ee83dSRobin Holt pr_emerg("Restarting system with command '%s'\n", cmd); 2706d3cf962SKees Cook kmsg_dump(KMSG_DUMP_SHUTDOWN); 27115d94b82SRobin Holt machine_restart(cmd); 27215d94b82SRobin Holt } 27315d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_restart); 27415d94b82SRobin Holt 27515d94b82SRobin Holt static void kernel_shutdown_prepare(enum system_states state) 27615d94b82SRobin Holt { 27715d94b82SRobin Holt blocking_notifier_call_chain(&reboot_notifier_list, 27815d94b82SRobin Holt (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL); 27915d94b82SRobin Holt system_state = state; 28015d94b82SRobin Holt usermodehelper_disable(); 28115d94b82SRobin Holt device_shutdown(); 28215d94b82SRobin Holt } 28315d94b82SRobin Holt /** 28415d94b82SRobin Holt * kernel_halt - halt the system 28515d94b82SRobin Holt * 28615d94b82SRobin Holt * Shutdown everything and perform a clean system halt. 28715d94b82SRobin Holt */ 28815d94b82SRobin Holt void kernel_halt(void) 28915d94b82SRobin Holt { 29015d94b82SRobin Holt kernel_shutdown_prepare(SYSTEM_HALT); 29115d94b82SRobin Holt migrate_to_reboot_cpu(); 29215d94b82SRobin Holt syscore_shutdown(); 293972ee83dSRobin Holt pr_emerg("System halted\n"); 2946d3cf962SKees Cook kmsg_dump(KMSG_DUMP_SHUTDOWN); 29515d94b82SRobin Holt machine_halt(); 29615d94b82SRobin Holt } 29715d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_halt); 29815d94b82SRobin Holt 299232edc2fSDmitry Osipenko /* 300232edc2fSDmitry Osipenko * Notifier list for kernel code which wants to be called 301232edc2fSDmitry Osipenko * to prepare system for power off. 302232edc2fSDmitry Osipenko */ 303232edc2fSDmitry Osipenko static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list); 304232edc2fSDmitry Osipenko 305232edc2fSDmitry Osipenko /* 306232edc2fSDmitry Osipenko * Notifier list for kernel code which wants to be called 307232edc2fSDmitry Osipenko * to power off system. 308232edc2fSDmitry Osipenko */ 309232edc2fSDmitry Osipenko static ATOMIC_NOTIFIER_HEAD(power_off_handler_list); 310232edc2fSDmitry Osipenko 311232edc2fSDmitry Osipenko static int sys_off_notify(struct notifier_block *nb, 312232edc2fSDmitry Osipenko unsigned long mode, void *cmd) 313232edc2fSDmitry Osipenko { 314232edc2fSDmitry Osipenko struct sys_off_handler *handler; 315232edc2fSDmitry Osipenko struct sys_off_data data = {}; 316232edc2fSDmitry Osipenko 317232edc2fSDmitry Osipenko handler = container_of(nb, struct sys_off_handler, nb); 318232edc2fSDmitry Osipenko data.cb_data = handler->cb_data; 319232edc2fSDmitry Osipenko data.mode = mode; 320232edc2fSDmitry Osipenko data.cmd = cmd; 321232edc2fSDmitry Osipenko 322232edc2fSDmitry Osipenko return handler->sys_off_cb(&data); 323232edc2fSDmitry Osipenko } 324232edc2fSDmitry Osipenko 325232edc2fSDmitry Osipenko /** 326232edc2fSDmitry Osipenko * register_sys_off_handler - Register sys-off handler 327232edc2fSDmitry Osipenko * @mode: Sys-off mode 328232edc2fSDmitry Osipenko * @priority: Handler priority 329232edc2fSDmitry Osipenko * @callback: Callback function 330232edc2fSDmitry Osipenko * @cb_data: Callback argument 331232edc2fSDmitry Osipenko * 332232edc2fSDmitry Osipenko * Registers system power-off or restart handler that will be invoked 333232edc2fSDmitry Osipenko * at the step corresponding to the given sys-off mode. Handler's callback 334232edc2fSDmitry Osipenko * should return NOTIFY_DONE to permit execution of the next handler in 335232edc2fSDmitry Osipenko * the call chain or NOTIFY_STOP to break the chain (in error case for 336232edc2fSDmitry Osipenko * example). 337232edc2fSDmitry Osipenko * 338232edc2fSDmitry Osipenko * Multiple handlers can be registered at the default priority level. 339232edc2fSDmitry Osipenko * 340232edc2fSDmitry Osipenko * Only one handler can be registered at the non-default priority level, 341232edc2fSDmitry Osipenko * otherwise ERR_PTR(-EBUSY) is returned. 342232edc2fSDmitry Osipenko * 343232edc2fSDmitry Osipenko * Returns a new instance of struct sys_off_handler on success, or 344232edc2fSDmitry Osipenko * an ERR_PTR()-encoded error code otherwise. 345232edc2fSDmitry Osipenko */ 346232edc2fSDmitry Osipenko struct sys_off_handler * 347232edc2fSDmitry Osipenko register_sys_off_handler(enum sys_off_mode mode, 348232edc2fSDmitry Osipenko int priority, 349232edc2fSDmitry Osipenko int (*callback)(struct sys_off_data *data), 350232edc2fSDmitry Osipenko void *cb_data) 351232edc2fSDmitry Osipenko { 352232edc2fSDmitry Osipenko struct sys_off_handler *handler; 353232edc2fSDmitry Osipenko int err; 354232edc2fSDmitry Osipenko 355232edc2fSDmitry Osipenko handler = kzalloc(sizeof(*handler), GFP_KERNEL); 356232edc2fSDmitry Osipenko if (!handler) 357232edc2fSDmitry Osipenko return ERR_PTR(-ENOMEM); 358232edc2fSDmitry Osipenko 359232edc2fSDmitry Osipenko switch (mode) { 360232edc2fSDmitry Osipenko case SYS_OFF_MODE_POWER_OFF_PREPARE: 361232edc2fSDmitry Osipenko handler->list = &power_off_prep_handler_list; 362232edc2fSDmitry Osipenko handler->blocking = true; 363232edc2fSDmitry Osipenko break; 364232edc2fSDmitry Osipenko 365232edc2fSDmitry Osipenko case SYS_OFF_MODE_POWER_OFF: 366232edc2fSDmitry Osipenko handler->list = &power_off_handler_list; 367232edc2fSDmitry Osipenko break; 368232edc2fSDmitry Osipenko 369232edc2fSDmitry Osipenko case SYS_OFF_MODE_RESTART: 370232edc2fSDmitry Osipenko handler->list = &restart_handler_list; 371232edc2fSDmitry Osipenko break; 372232edc2fSDmitry Osipenko 373232edc2fSDmitry Osipenko default: 374232edc2fSDmitry Osipenko kfree(handler); 375232edc2fSDmitry Osipenko return ERR_PTR(-EINVAL); 376232edc2fSDmitry Osipenko } 377232edc2fSDmitry Osipenko 378232edc2fSDmitry Osipenko handler->nb.notifier_call = sys_off_notify; 379232edc2fSDmitry Osipenko handler->nb.priority = priority; 380232edc2fSDmitry Osipenko handler->sys_off_cb = callback; 381232edc2fSDmitry Osipenko handler->cb_data = cb_data; 382232edc2fSDmitry Osipenko handler->mode = mode; 383232edc2fSDmitry Osipenko 384232edc2fSDmitry Osipenko if (handler->blocking) { 385232edc2fSDmitry Osipenko if (priority == SYS_OFF_PRIO_DEFAULT) 386232edc2fSDmitry Osipenko err = blocking_notifier_chain_register(handler->list, 387232edc2fSDmitry Osipenko &handler->nb); 388232edc2fSDmitry Osipenko else 389232edc2fSDmitry Osipenko err = blocking_notifier_chain_register_unique_prio(handler->list, 390232edc2fSDmitry Osipenko &handler->nb); 391232edc2fSDmitry Osipenko } else { 392232edc2fSDmitry Osipenko if (priority == SYS_OFF_PRIO_DEFAULT) 393232edc2fSDmitry Osipenko err = atomic_notifier_chain_register(handler->list, 394232edc2fSDmitry Osipenko &handler->nb); 395232edc2fSDmitry Osipenko else 396232edc2fSDmitry Osipenko err = atomic_notifier_chain_register_unique_prio(handler->list, 397232edc2fSDmitry Osipenko &handler->nb); 398232edc2fSDmitry Osipenko } 399232edc2fSDmitry Osipenko 400232edc2fSDmitry Osipenko if (err) { 401232edc2fSDmitry Osipenko kfree(handler); 402232edc2fSDmitry Osipenko return ERR_PTR(err); 403232edc2fSDmitry Osipenko } 404232edc2fSDmitry Osipenko 405232edc2fSDmitry Osipenko return handler; 406232edc2fSDmitry Osipenko } 407232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(register_sys_off_handler); 408232edc2fSDmitry Osipenko 409232edc2fSDmitry Osipenko /** 410232edc2fSDmitry Osipenko * unregister_sys_off_handler - Unregister sys-off handler 411232edc2fSDmitry Osipenko * @handler: Sys-off handler 412232edc2fSDmitry Osipenko * 413232edc2fSDmitry Osipenko * Unregisters given sys-off handler. 414232edc2fSDmitry Osipenko */ 415232edc2fSDmitry Osipenko void unregister_sys_off_handler(struct sys_off_handler *handler) 416232edc2fSDmitry Osipenko { 417232edc2fSDmitry Osipenko int err; 418232edc2fSDmitry Osipenko 419232edc2fSDmitry Osipenko if (!handler) 420232edc2fSDmitry Osipenko return; 421232edc2fSDmitry Osipenko 422232edc2fSDmitry Osipenko if (handler->blocking) 423232edc2fSDmitry Osipenko err = blocking_notifier_chain_unregister(handler->list, 424232edc2fSDmitry Osipenko &handler->nb); 425232edc2fSDmitry Osipenko else 426232edc2fSDmitry Osipenko err = atomic_notifier_chain_unregister(handler->list, 427232edc2fSDmitry Osipenko &handler->nb); 428232edc2fSDmitry Osipenko 429232edc2fSDmitry Osipenko /* sanity check, shall never happen */ 430232edc2fSDmitry Osipenko WARN_ON(err); 431232edc2fSDmitry Osipenko 432232edc2fSDmitry Osipenko kfree(handler); 433232edc2fSDmitry Osipenko } 434232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(unregister_sys_off_handler); 435232edc2fSDmitry Osipenko 436232edc2fSDmitry Osipenko static void devm_unregister_sys_off_handler(void *data) 437232edc2fSDmitry Osipenko { 438232edc2fSDmitry Osipenko struct sys_off_handler *handler = data; 439232edc2fSDmitry Osipenko 440232edc2fSDmitry Osipenko unregister_sys_off_handler(handler); 441232edc2fSDmitry Osipenko } 442232edc2fSDmitry Osipenko 443232edc2fSDmitry Osipenko /** 444232edc2fSDmitry Osipenko * devm_register_sys_off_handler - Register sys-off handler 445232edc2fSDmitry Osipenko * @dev: Device that registers handler 446232edc2fSDmitry Osipenko * @mode: Sys-off mode 447232edc2fSDmitry Osipenko * @priority: Handler priority 448232edc2fSDmitry Osipenko * @callback: Callback function 449232edc2fSDmitry Osipenko * @cb_data: Callback argument 450232edc2fSDmitry Osipenko * 451232edc2fSDmitry Osipenko * Registers resource-managed sys-off handler. 452232edc2fSDmitry Osipenko * 453232edc2fSDmitry Osipenko * Returns zero on success, or error code on failure. 454232edc2fSDmitry Osipenko */ 455232edc2fSDmitry Osipenko int devm_register_sys_off_handler(struct device *dev, 456232edc2fSDmitry Osipenko enum sys_off_mode mode, 457232edc2fSDmitry Osipenko int priority, 458232edc2fSDmitry Osipenko int (*callback)(struct sys_off_data *data), 459232edc2fSDmitry Osipenko void *cb_data) 460232edc2fSDmitry Osipenko { 461232edc2fSDmitry Osipenko struct sys_off_handler *handler; 462232edc2fSDmitry Osipenko 463232edc2fSDmitry Osipenko handler = register_sys_off_handler(mode, priority, callback, cb_data); 464232edc2fSDmitry Osipenko if (IS_ERR(handler)) 465232edc2fSDmitry Osipenko return PTR_ERR(handler); 466232edc2fSDmitry Osipenko 467232edc2fSDmitry Osipenko return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler, 468232edc2fSDmitry Osipenko handler); 469232edc2fSDmitry Osipenko } 470232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(devm_register_sys_off_handler); 471232edc2fSDmitry Osipenko 4727b9a3de9SDmitry Osipenko static int legacy_pm_power_off_prepare(struct sys_off_data *data) 4737b9a3de9SDmitry Osipenko { 4747b9a3de9SDmitry Osipenko if (pm_power_off_prepare) 4757b9a3de9SDmitry Osipenko pm_power_off_prepare(); 4767b9a3de9SDmitry Osipenko 4777b9a3de9SDmitry Osipenko return NOTIFY_DONE; 4787b9a3de9SDmitry Osipenko } 4797b9a3de9SDmitry Osipenko 4807b9a3de9SDmitry Osipenko static int legacy_pm_power_off(struct sys_off_data *data) 4817b9a3de9SDmitry Osipenko { 4827b9a3de9SDmitry Osipenko if (pm_power_off) 4837b9a3de9SDmitry Osipenko pm_power_off(); 4847b9a3de9SDmitry Osipenko 4857b9a3de9SDmitry Osipenko return NOTIFY_DONE; 4867b9a3de9SDmitry Osipenko } 4877b9a3de9SDmitry Osipenko 4887b9a3de9SDmitry Osipenko /* 4897b9a3de9SDmitry Osipenko * Register sys-off handlers for legacy PM callbacks. This allows legacy 4907b9a3de9SDmitry Osipenko * PM callbacks co-exist with the new sys-off API. 4917b9a3de9SDmitry Osipenko * 4927b9a3de9SDmitry Osipenko * TODO: Remove legacy handlers once all legacy PM users will be switched 4937b9a3de9SDmitry Osipenko * to the sys-off based APIs. 4947b9a3de9SDmitry Osipenko */ 4957b9a3de9SDmitry Osipenko static int __init legacy_pm_init(void) 4967b9a3de9SDmitry Osipenko { 4977b9a3de9SDmitry Osipenko register_sys_off_handler(SYS_OFF_MODE_POWER_OFF_PREPARE, 4987b9a3de9SDmitry Osipenko SYS_OFF_PRIO_DEFAULT, 4997b9a3de9SDmitry Osipenko legacy_pm_power_off_prepare, NULL); 5007b9a3de9SDmitry Osipenko 5017b9a3de9SDmitry Osipenko register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT, 5027b9a3de9SDmitry Osipenko legacy_pm_power_off, NULL); 5037b9a3de9SDmitry Osipenko 5047b9a3de9SDmitry Osipenko return 0; 5057b9a3de9SDmitry Osipenko } 5067b9a3de9SDmitry Osipenko core_initcall(legacy_pm_init); 5077b9a3de9SDmitry Osipenko 5087b9a3de9SDmitry Osipenko static void do_kernel_power_off_prepare(void) 5097b9a3de9SDmitry Osipenko { 5107b9a3de9SDmitry Osipenko blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL); 5117b9a3de9SDmitry Osipenko } 5127b9a3de9SDmitry Osipenko 51315d94b82SRobin Holt /** 5142b6aa733SDmitry Osipenko * do_kernel_power_off - Execute kernel power-off handler call chain 5152b6aa733SDmitry Osipenko * 5162b6aa733SDmitry Osipenko * Expected to be called as last step of the power-off sequence. 5172b6aa733SDmitry Osipenko * 5182b6aa733SDmitry Osipenko * Powers off the system immediately if a power-off handler function has 5192b6aa733SDmitry Osipenko * been registered. Otherwise does nothing. 5202b6aa733SDmitry Osipenko */ 5212b6aa733SDmitry Osipenko void do_kernel_power_off(void) 5222b6aa733SDmitry Osipenko { 5232b6aa733SDmitry Osipenko atomic_notifier_call_chain(&power_off_handler_list, 0, NULL); 5242b6aa733SDmitry Osipenko } 5252b6aa733SDmitry Osipenko 5262b6aa733SDmitry Osipenko /** 52715d94b82SRobin Holt * kernel_power_off - power_off the system 52815d94b82SRobin Holt * 52915d94b82SRobin Holt * Shutdown everything and perform a clean system power_off. 53015d94b82SRobin Holt */ 53115d94b82SRobin Holt void kernel_power_off(void) 53215d94b82SRobin Holt { 53315d94b82SRobin Holt kernel_shutdown_prepare(SYSTEM_POWER_OFF); 5347b9a3de9SDmitry Osipenko do_kernel_power_off_prepare(); 53515d94b82SRobin Holt migrate_to_reboot_cpu(); 53615d94b82SRobin Holt syscore_shutdown(); 537972ee83dSRobin Holt pr_emerg("Power down\n"); 5386d3cf962SKees Cook kmsg_dump(KMSG_DUMP_SHUTDOWN); 53915d94b82SRobin Holt machine_power_off(); 54015d94b82SRobin Holt } 54115d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_power_off); 54215d94b82SRobin Holt 54355f2503cSPingfan Liu DEFINE_MUTEX(system_transition_mutex); 54415d94b82SRobin Holt 54515d94b82SRobin Holt /* 54615d94b82SRobin Holt * Reboot system call: for obvious reasons only root may call it, 54715d94b82SRobin Holt * and even root needs to set up some magic numbers in the registers 54815d94b82SRobin Holt * so that some mistake won't make this reboot the whole machine. 54915d94b82SRobin Holt * You can also set the meaning of the ctrl-alt-del-key here. 55015d94b82SRobin Holt * 55115d94b82SRobin Holt * reboot doesn't sync: do that yourself before calling this. 55215d94b82SRobin Holt */ 55315d94b82SRobin Holt SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd, 55415d94b82SRobin Holt void __user *, arg) 55515d94b82SRobin Holt { 55615d94b82SRobin Holt struct pid_namespace *pid_ns = task_active_pid_ns(current); 55715d94b82SRobin Holt char buffer[256]; 55815d94b82SRobin Holt int ret = 0; 55915d94b82SRobin Holt 56015d94b82SRobin Holt /* We only trust the superuser with rebooting the system. */ 56115d94b82SRobin Holt if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT)) 56215d94b82SRobin Holt return -EPERM; 56315d94b82SRobin Holt 56415d94b82SRobin Holt /* For safety, we require "magic" arguments. */ 56515d94b82SRobin Holt if (magic1 != LINUX_REBOOT_MAGIC1 || 56615d94b82SRobin Holt (magic2 != LINUX_REBOOT_MAGIC2 && 56715d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2A && 56815d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2B && 56915d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2C)) 57015d94b82SRobin Holt return -EINVAL; 57115d94b82SRobin Holt 57215d94b82SRobin Holt /* 57315d94b82SRobin Holt * If pid namespaces are enabled and the current task is in a child 57415d94b82SRobin Holt * pid_namespace, the command is handled by reboot_pid_ns() which will 57515d94b82SRobin Holt * call do_exit(). 57615d94b82SRobin Holt */ 57715d94b82SRobin Holt ret = reboot_pid_ns(pid_ns, cmd); 57815d94b82SRobin Holt if (ret) 57915d94b82SRobin Holt return ret; 58015d94b82SRobin Holt 58115d94b82SRobin Holt /* Instead of trying to make the power_off code look like 58215d94b82SRobin Holt * halt when pm_power_off is not set do it the easy way. 58315d94b82SRobin Holt */ 58415d94b82SRobin Holt if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off) 58515d94b82SRobin Holt cmd = LINUX_REBOOT_CMD_HALT; 58615d94b82SRobin Holt 58755f2503cSPingfan Liu mutex_lock(&system_transition_mutex); 58815d94b82SRobin Holt switch (cmd) { 58915d94b82SRobin Holt case LINUX_REBOOT_CMD_RESTART: 59015d94b82SRobin Holt kernel_restart(NULL); 59115d94b82SRobin Holt break; 59215d94b82SRobin Holt 59315d94b82SRobin Holt case LINUX_REBOOT_CMD_CAD_ON: 59415d94b82SRobin Holt C_A_D = 1; 59515d94b82SRobin Holt break; 59615d94b82SRobin Holt 59715d94b82SRobin Holt case LINUX_REBOOT_CMD_CAD_OFF: 59815d94b82SRobin Holt C_A_D = 0; 59915d94b82SRobin Holt break; 60015d94b82SRobin Holt 60115d94b82SRobin Holt case LINUX_REBOOT_CMD_HALT: 60215d94b82SRobin Holt kernel_halt(); 60315d94b82SRobin Holt do_exit(0); 60415d94b82SRobin Holt 60515d94b82SRobin Holt case LINUX_REBOOT_CMD_POWER_OFF: 60615d94b82SRobin Holt kernel_power_off(); 60715d94b82SRobin Holt do_exit(0); 60815d94b82SRobin Holt break; 60915d94b82SRobin Holt 61015d94b82SRobin Holt case LINUX_REBOOT_CMD_RESTART2: 611972ee83dSRobin Holt ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1); 612972ee83dSRobin Holt if (ret < 0) { 61315d94b82SRobin Holt ret = -EFAULT; 61415d94b82SRobin Holt break; 61515d94b82SRobin Holt } 61615d94b82SRobin Holt buffer[sizeof(buffer) - 1] = '\0'; 61715d94b82SRobin Holt 61815d94b82SRobin Holt kernel_restart(buffer); 61915d94b82SRobin Holt break; 62015d94b82SRobin Holt 6212965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 62215d94b82SRobin Holt case LINUX_REBOOT_CMD_KEXEC: 62315d94b82SRobin Holt ret = kernel_kexec(); 62415d94b82SRobin Holt break; 62515d94b82SRobin Holt #endif 62615d94b82SRobin Holt 62715d94b82SRobin Holt #ifdef CONFIG_HIBERNATION 62815d94b82SRobin Holt case LINUX_REBOOT_CMD_SW_SUSPEND: 62915d94b82SRobin Holt ret = hibernate(); 63015d94b82SRobin Holt break; 63115d94b82SRobin Holt #endif 63215d94b82SRobin Holt 63315d94b82SRobin Holt default: 63415d94b82SRobin Holt ret = -EINVAL; 63515d94b82SRobin Holt break; 63615d94b82SRobin Holt } 63755f2503cSPingfan Liu mutex_unlock(&system_transition_mutex); 63815d94b82SRobin Holt return ret; 63915d94b82SRobin Holt } 64015d94b82SRobin Holt 64115d94b82SRobin Holt static void deferred_cad(struct work_struct *dummy) 64215d94b82SRobin Holt { 64315d94b82SRobin Holt kernel_restart(NULL); 64415d94b82SRobin Holt } 64515d94b82SRobin Holt 64615d94b82SRobin Holt /* 64715d94b82SRobin Holt * This function gets called by ctrl-alt-del - ie the keyboard interrupt. 64815d94b82SRobin Holt * As it's called within an interrupt, it may NOT sync: the only choice 64915d94b82SRobin Holt * is whether to reboot at once, or just ignore the ctrl-alt-del. 65015d94b82SRobin Holt */ 65115d94b82SRobin Holt void ctrl_alt_del(void) 65215d94b82SRobin Holt { 65315d94b82SRobin Holt static DECLARE_WORK(cad_work, deferred_cad); 65415d94b82SRobin Holt 65515d94b82SRobin Holt if (C_A_D) 65615d94b82SRobin Holt schedule_work(&cad_work); 65715d94b82SRobin Holt else 65815d94b82SRobin Holt kill_cad_pid(SIGINT, 1); 65915d94b82SRobin Holt } 66015d94b82SRobin Holt 66115d94b82SRobin Holt char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff"; 6627a54f46bSJoel Stanley static const char reboot_cmd[] = "/sbin/reboot"; 66315d94b82SRobin Holt 6647a54f46bSJoel Stanley static int run_cmd(const char *cmd) 66515d94b82SRobin Holt { 66615d94b82SRobin Holt char **argv; 66715d94b82SRobin Holt static char *envp[] = { 66815d94b82SRobin Holt "HOME=/", 66915d94b82SRobin Holt "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 67015d94b82SRobin Holt NULL 67115d94b82SRobin Holt }; 67215d94b82SRobin Holt int ret; 6737a54f46bSJoel Stanley argv = argv_split(GFP_KERNEL, cmd, NULL); 67415d94b82SRobin Holt if (argv) { 67515d94b82SRobin Holt ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); 67615d94b82SRobin Holt argv_free(argv); 67715d94b82SRobin Holt } else { 67815d94b82SRobin Holt ret = -ENOMEM; 67915d94b82SRobin Holt } 68015d94b82SRobin Holt 6817a54f46bSJoel Stanley return ret; 6827a54f46bSJoel Stanley } 6837a54f46bSJoel Stanley 6847a54f46bSJoel Stanley static int __orderly_reboot(void) 6857a54f46bSJoel Stanley { 6867a54f46bSJoel Stanley int ret; 6877a54f46bSJoel Stanley 6887a54f46bSJoel Stanley ret = run_cmd(reboot_cmd); 6897a54f46bSJoel Stanley 6907a54f46bSJoel Stanley if (ret) { 6917a54f46bSJoel Stanley pr_warn("Failed to start orderly reboot: forcing the issue\n"); 6927a54f46bSJoel Stanley emergency_sync(); 6937a54f46bSJoel Stanley kernel_restart(NULL); 6947a54f46bSJoel Stanley } 6957a54f46bSJoel Stanley 6967a54f46bSJoel Stanley return ret; 6977a54f46bSJoel Stanley } 6987a54f46bSJoel Stanley 6997a54f46bSJoel Stanley static int __orderly_poweroff(bool force) 7007a54f46bSJoel Stanley { 7017a54f46bSJoel Stanley int ret; 7027a54f46bSJoel Stanley 7037a54f46bSJoel Stanley ret = run_cmd(poweroff_cmd); 7047a54f46bSJoel Stanley 70515d94b82SRobin Holt if (ret && force) { 706972ee83dSRobin Holt pr_warn("Failed to start orderly shutdown: forcing the issue\n"); 7077a54f46bSJoel Stanley 70815d94b82SRobin Holt /* 70915d94b82SRobin Holt * I guess this should try to kick off some daemon to sync and 71015d94b82SRobin Holt * poweroff asap. Or not even bother syncing if we're doing an 71115d94b82SRobin Holt * emergency shutdown? 71215d94b82SRobin Holt */ 71315d94b82SRobin Holt emergency_sync(); 71415d94b82SRobin Holt kernel_power_off(); 71515d94b82SRobin Holt } 71615d94b82SRobin Holt 71715d94b82SRobin Holt return ret; 71815d94b82SRobin Holt } 71915d94b82SRobin Holt 72015d94b82SRobin Holt static bool poweroff_force; 72115d94b82SRobin Holt 72215d94b82SRobin Holt static void poweroff_work_func(struct work_struct *work) 72315d94b82SRobin Holt { 72415d94b82SRobin Holt __orderly_poweroff(poweroff_force); 72515d94b82SRobin Holt } 72615d94b82SRobin Holt 72715d94b82SRobin Holt static DECLARE_WORK(poweroff_work, poweroff_work_func); 72815d94b82SRobin Holt 72915d94b82SRobin Holt /** 73015d94b82SRobin Holt * orderly_poweroff - Trigger an orderly system poweroff 73115d94b82SRobin Holt * @force: force poweroff if command execution fails 73215d94b82SRobin Holt * 73315d94b82SRobin Holt * This may be called from any context to trigger a system shutdown. 73415d94b82SRobin Holt * If the orderly shutdown fails, it will force an immediate shutdown. 73515d94b82SRobin Holt */ 7367a54f46bSJoel Stanley void orderly_poweroff(bool force) 73715d94b82SRobin Holt { 73815d94b82SRobin Holt if (force) /* do not override the pending "true" */ 73915d94b82SRobin Holt poweroff_force = true; 74015d94b82SRobin Holt schedule_work(&poweroff_work); 74115d94b82SRobin Holt } 74215d94b82SRobin Holt EXPORT_SYMBOL_GPL(orderly_poweroff); 7431b3a5d02SRobin Holt 7447a54f46bSJoel Stanley static void reboot_work_func(struct work_struct *work) 7457a54f46bSJoel Stanley { 7467a54f46bSJoel Stanley __orderly_reboot(); 7477a54f46bSJoel Stanley } 7487a54f46bSJoel Stanley 7497a54f46bSJoel Stanley static DECLARE_WORK(reboot_work, reboot_work_func); 7507a54f46bSJoel Stanley 7517a54f46bSJoel Stanley /** 7527a54f46bSJoel Stanley * orderly_reboot - Trigger an orderly system reboot 7537a54f46bSJoel Stanley * 7547a54f46bSJoel Stanley * This may be called from any context to trigger a system reboot. 7557a54f46bSJoel Stanley * If the orderly reboot fails, it will force an immediate reboot. 7567a54f46bSJoel Stanley */ 7577a54f46bSJoel Stanley void orderly_reboot(void) 7587a54f46bSJoel Stanley { 7597a54f46bSJoel Stanley schedule_work(&reboot_work); 7607a54f46bSJoel Stanley } 7617a54f46bSJoel Stanley EXPORT_SYMBOL_GPL(orderly_reboot); 7627a54f46bSJoel Stanley 763dfa19b11SMatti Vaittinen /** 764dfa19b11SMatti Vaittinen * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay 765dfa19b11SMatti Vaittinen * @work: work_struct associated with the emergency poweroff function 766dfa19b11SMatti Vaittinen * 767dfa19b11SMatti Vaittinen * This function is called in very critical situations to force 768dfa19b11SMatti Vaittinen * a kernel poweroff after a configurable timeout value. 769dfa19b11SMatti Vaittinen */ 770dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff_func(struct work_struct *work) 771dfa19b11SMatti Vaittinen { 772dfa19b11SMatti Vaittinen /* 773dfa19b11SMatti Vaittinen * We have reached here after the emergency shutdown waiting period has 774dfa19b11SMatti Vaittinen * expired. This means orderly_poweroff has not been able to shut off 775dfa19b11SMatti Vaittinen * the system for some reason. 776dfa19b11SMatti Vaittinen * 777dfa19b11SMatti Vaittinen * Try to shut down the system immediately using kernel_power_off 778dfa19b11SMatti Vaittinen * if populated 779dfa19b11SMatti Vaittinen */ 780dfa19b11SMatti Vaittinen pr_emerg("Hardware protection timed-out. Trying forced poweroff\n"); 781dfa19b11SMatti Vaittinen kernel_power_off(); 782dfa19b11SMatti Vaittinen 783dfa19b11SMatti Vaittinen /* 784dfa19b11SMatti Vaittinen * Worst of the worst case trigger emergency restart 785dfa19b11SMatti Vaittinen */ 786dfa19b11SMatti Vaittinen pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n"); 787dfa19b11SMatti Vaittinen emergency_restart(); 788dfa19b11SMatti Vaittinen } 789dfa19b11SMatti Vaittinen 790dfa19b11SMatti Vaittinen static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work, 791dfa19b11SMatti Vaittinen hw_failure_emergency_poweroff_func); 792dfa19b11SMatti Vaittinen 793dfa19b11SMatti Vaittinen /** 794dfa19b11SMatti Vaittinen * hw_failure_emergency_poweroff - Trigger an emergency system poweroff 795dfa19b11SMatti Vaittinen * 796dfa19b11SMatti Vaittinen * This may be called from any critical situation to trigger a system shutdown 797dfa19b11SMatti Vaittinen * after a given period of time. If time is negative this is not scheduled. 798dfa19b11SMatti Vaittinen */ 799dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff(int poweroff_delay_ms) 800dfa19b11SMatti Vaittinen { 801dfa19b11SMatti Vaittinen if (poweroff_delay_ms <= 0) 802dfa19b11SMatti Vaittinen return; 803dfa19b11SMatti Vaittinen schedule_delayed_work(&hw_failure_emergency_poweroff_work, 804dfa19b11SMatti Vaittinen msecs_to_jiffies(poweroff_delay_ms)); 805dfa19b11SMatti Vaittinen } 806dfa19b11SMatti Vaittinen 807dfa19b11SMatti Vaittinen /** 808dfa19b11SMatti Vaittinen * hw_protection_shutdown - Trigger an emergency system poweroff 809dfa19b11SMatti Vaittinen * 810dfa19b11SMatti Vaittinen * @reason: Reason of emergency shutdown to be printed. 811dfa19b11SMatti Vaittinen * @ms_until_forced: Time to wait for orderly shutdown before tiggering a 812dfa19b11SMatti Vaittinen * forced shudown. Negative value disables the forced 813dfa19b11SMatti Vaittinen * shutdown. 814dfa19b11SMatti Vaittinen * 815dfa19b11SMatti Vaittinen * Initiate an emergency system shutdown in order to protect hardware from 816dfa19b11SMatti Vaittinen * further damage. Usage examples include a thermal protection or a voltage or 817dfa19b11SMatti Vaittinen * current regulator failures. 818dfa19b11SMatti Vaittinen * NOTE: The request is ignored if protection shutdown is already pending even 819dfa19b11SMatti Vaittinen * if the previous request has given a large timeout for forced shutdown. 820dfa19b11SMatti Vaittinen * Can be called from any context. 821dfa19b11SMatti Vaittinen */ 822dfa19b11SMatti Vaittinen void hw_protection_shutdown(const char *reason, int ms_until_forced) 823dfa19b11SMatti Vaittinen { 824dfa19b11SMatti Vaittinen static atomic_t allow_proceed = ATOMIC_INIT(1); 825dfa19b11SMatti Vaittinen 826dfa19b11SMatti Vaittinen pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason); 827dfa19b11SMatti Vaittinen 828dfa19b11SMatti Vaittinen /* Shutdown should be initiated only once. */ 829dfa19b11SMatti Vaittinen if (!atomic_dec_and_test(&allow_proceed)) 830dfa19b11SMatti Vaittinen return; 831dfa19b11SMatti Vaittinen 832dfa19b11SMatti Vaittinen /* 833dfa19b11SMatti Vaittinen * Queue a backup emergency shutdown in the event of 834dfa19b11SMatti Vaittinen * orderly_poweroff failure 835dfa19b11SMatti Vaittinen */ 836dfa19b11SMatti Vaittinen hw_failure_emergency_poweroff(ms_until_forced); 837dfa19b11SMatti Vaittinen orderly_poweroff(true); 838dfa19b11SMatti Vaittinen } 839dfa19b11SMatti Vaittinen EXPORT_SYMBOL_GPL(hw_protection_shutdown); 840dfa19b11SMatti Vaittinen 8411b3a5d02SRobin Holt static int __init reboot_setup(char *str) 8421b3a5d02SRobin Holt { 8431b3a5d02SRobin Holt for (;;) { 844b287a25aSAaro Koskinen enum reboot_mode *mode; 845b287a25aSAaro Koskinen 8461b3a5d02SRobin Holt /* 8471b3a5d02SRobin Holt * Having anything passed on the command line via 8481b3a5d02SRobin Holt * reboot= will cause us to disable DMI checking 8491b3a5d02SRobin Holt * below. 8501b3a5d02SRobin Holt */ 8511b3a5d02SRobin Holt reboot_default = 0; 8521b3a5d02SRobin Holt 853b287a25aSAaro Koskinen if (!strncmp(str, "panic_", 6)) { 854b287a25aSAaro Koskinen mode = &panic_reboot_mode; 855b287a25aSAaro Koskinen str += 6; 856b287a25aSAaro Koskinen } else { 857b287a25aSAaro Koskinen mode = &reboot_mode; 858b287a25aSAaro Koskinen } 859b287a25aSAaro Koskinen 8601b3a5d02SRobin Holt switch (*str) { 8611b3a5d02SRobin Holt case 'w': 862b287a25aSAaro Koskinen *mode = REBOOT_WARM; 8631b3a5d02SRobin Holt break; 8641b3a5d02SRobin Holt 8651b3a5d02SRobin Holt case 'c': 866b287a25aSAaro Koskinen *mode = REBOOT_COLD; 8671b3a5d02SRobin Holt break; 8681b3a5d02SRobin Holt 8691b3a5d02SRobin Holt case 'h': 870b287a25aSAaro Koskinen *mode = REBOOT_HARD; 8711b3a5d02SRobin Holt break; 8721b3a5d02SRobin Holt 8731b3a5d02SRobin Holt case 's': 874f9a90501SMatteo Croce /* 875f9a90501SMatteo Croce * reboot_cpu is s[mp]#### with #### being the processor 876f9a90501SMatteo Croce * to be used for rebooting. Skip 's' or 'smp' prefix. 877f9a90501SMatteo Croce */ 878f9a90501SMatteo Croce str += str[1] == 'm' && str[2] == 'p' ? 3 : 1; 879f9a90501SMatteo Croce 880f9a90501SMatteo Croce if (isdigit(str[0])) { 881f9a90501SMatteo Croce int cpu = simple_strtoul(str, NULL, 0); 882f9a90501SMatteo Croce 883f9a90501SMatteo Croce if (cpu >= num_possible_cpus()) { 884df5b0ab3SMatteo Croce pr_err("Ignoring the CPU number in reboot= option. " 885df5b0ab3SMatteo Croce "CPU %d exceeds possible cpu number %d\n", 886f9a90501SMatteo Croce cpu, num_possible_cpus()); 887df5b0ab3SMatteo Croce break; 888df5b0ab3SMatteo Croce } 889f9a90501SMatteo Croce reboot_cpu = cpu; 890f9a90501SMatteo Croce } else 891f9a90501SMatteo Croce *mode = REBOOT_SOFT; 8921b3a5d02SRobin Holt break; 8938b92c4ffSMatteo Croce 8941b3a5d02SRobin Holt case 'g': 895b287a25aSAaro Koskinen *mode = REBOOT_GPIO; 8961b3a5d02SRobin Holt break; 8971b3a5d02SRobin Holt 8981b3a5d02SRobin Holt case 'b': 8991b3a5d02SRobin Holt case 'a': 9001b3a5d02SRobin Holt case 'k': 9011b3a5d02SRobin Holt case 't': 9021b3a5d02SRobin Holt case 'e': 9031b3a5d02SRobin Holt case 'p': 9041b3a5d02SRobin Holt reboot_type = *str; 9051b3a5d02SRobin Holt break; 9061b3a5d02SRobin Holt 9071b3a5d02SRobin Holt case 'f': 9081b3a5d02SRobin Holt reboot_force = 1; 9091b3a5d02SRobin Holt break; 9101b3a5d02SRobin Holt } 9111b3a5d02SRobin Holt 9121b3a5d02SRobin Holt str = strchr(str, ','); 9131b3a5d02SRobin Holt if (str) 9141b3a5d02SRobin Holt str++; 9151b3a5d02SRobin Holt else 9161b3a5d02SRobin Holt break; 9171b3a5d02SRobin Holt } 9181b3a5d02SRobin Holt return 1; 9191b3a5d02SRobin Holt } 9201b3a5d02SRobin Holt __setup("reboot=", reboot_setup); 9212c622ed0SMatteo Croce 9222c622ed0SMatteo Croce #ifdef CONFIG_SYSFS 9232c622ed0SMatteo Croce 9242c622ed0SMatteo Croce #define REBOOT_COLD_STR "cold" 9252c622ed0SMatteo Croce #define REBOOT_WARM_STR "warm" 9262c622ed0SMatteo Croce #define REBOOT_HARD_STR "hard" 9272c622ed0SMatteo Croce #define REBOOT_SOFT_STR "soft" 9282c622ed0SMatteo Croce #define REBOOT_GPIO_STR "gpio" 9292c622ed0SMatteo Croce #define REBOOT_UNDEFINED_STR "undefined" 9302c622ed0SMatteo Croce 9312c622ed0SMatteo Croce #define BOOT_TRIPLE_STR "triple" 9322c622ed0SMatteo Croce #define BOOT_KBD_STR "kbd" 9332c622ed0SMatteo Croce #define BOOT_BIOS_STR "bios" 9342c622ed0SMatteo Croce #define BOOT_ACPI_STR "acpi" 9352c622ed0SMatteo Croce #define BOOT_EFI_STR "efi" 9360c5c0179SMatteo Croce #define BOOT_PCI_STR "pci" 9372c622ed0SMatteo Croce 9382c622ed0SMatteo Croce static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 9392c622ed0SMatteo Croce { 9402c622ed0SMatteo Croce const char *val; 9412c622ed0SMatteo Croce 9422c622ed0SMatteo Croce switch (reboot_mode) { 9432c622ed0SMatteo Croce case REBOOT_COLD: 9442c622ed0SMatteo Croce val = REBOOT_COLD_STR; 9452c622ed0SMatteo Croce break; 9462c622ed0SMatteo Croce case REBOOT_WARM: 9472c622ed0SMatteo Croce val = REBOOT_WARM_STR; 9482c622ed0SMatteo Croce break; 9492c622ed0SMatteo Croce case REBOOT_HARD: 9502c622ed0SMatteo Croce val = REBOOT_HARD_STR; 9512c622ed0SMatteo Croce break; 9522c622ed0SMatteo Croce case REBOOT_SOFT: 9532c622ed0SMatteo Croce val = REBOOT_SOFT_STR; 9542c622ed0SMatteo Croce break; 9552c622ed0SMatteo Croce case REBOOT_GPIO: 9562c622ed0SMatteo Croce val = REBOOT_GPIO_STR; 9572c622ed0SMatteo Croce break; 9582c622ed0SMatteo Croce default: 9592c622ed0SMatteo Croce val = REBOOT_UNDEFINED_STR; 9602c622ed0SMatteo Croce } 9612c622ed0SMatteo Croce 9622c622ed0SMatteo Croce return sprintf(buf, "%s\n", val); 9632c622ed0SMatteo Croce } 9642c622ed0SMatteo Croce static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr, 9652c622ed0SMatteo Croce const char *buf, size_t count) 9662c622ed0SMatteo Croce { 9672c622ed0SMatteo Croce if (!capable(CAP_SYS_BOOT)) 9682c622ed0SMatteo Croce return -EPERM; 9692c622ed0SMatteo Croce 9702c622ed0SMatteo Croce if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR))) 9712c622ed0SMatteo Croce reboot_mode = REBOOT_COLD; 9722c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR))) 9732c622ed0SMatteo Croce reboot_mode = REBOOT_WARM; 9742c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR))) 9752c622ed0SMatteo Croce reboot_mode = REBOOT_HARD; 9762c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR))) 9772c622ed0SMatteo Croce reboot_mode = REBOOT_SOFT; 9782c622ed0SMatteo Croce else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR))) 9792c622ed0SMatteo Croce reboot_mode = REBOOT_GPIO; 9802c622ed0SMatteo Croce else 9812c622ed0SMatteo Croce return -EINVAL; 9822c622ed0SMatteo Croce 9831a9d079fSMatteo Croce reboot_default = 0; 9841a9d079fSMatteo Croce 9852c622ed0SMatteo Croce return count; 9862c622ed0SMatteo Croce } 9872c622ed0SMatteo Croce static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode); 9882c622ed0SMatteo Croce 98940247e55SMatteo Croce #ifdef CONFIG_X86 99040247e55SMatteo Croce static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 99140247e55SMatteo Croce { 99240247e55SMatteo Croce return sprintf(buf, "%d\n", reboot_force); 99340247e55SMatteo Croce } 99440247e55SMatteo Croce static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr, 99540247e55SMatteo Croce const char *buf, size_t count) 99640247e55SMatteo Croce { 99740247e55SMatteo Croce bool res; 99840247e55SMatteo Croce 99940247e55SMatteo Croce if (!capable(CAP_SYS_BOOT)) 100040247e55SMatteo Croce return -EPERM; 100140247e55SMatteo Croce 100240247e55SMatteo Croce if (kstrtobool(buf, &res)) 100340247e55SMatteo Croce return -EINVAL; 100440247e55SMatteo Croce 100540247e55SMatteo Croce reboot_default = 0; 100640247e55SMatteo Croce reboot_force = res; 100740247e55SMatteo Croce 100840247e55SMatteo Croce return count; 100940247e55SMatteo Croce } 101040247e55SMatteo Croce static struct kobj_attribute reboot_force_attr = __ATTR_RW(force); 101140247e55SMatteo Croce 10122c622ed0SMatteo Croce static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 10132c622ed0SMatteo Croce { 10142c622ed0SMatteo Croce const char *val; 10152c622ed0SMatteo Croce 10162c622ed0SMatteo Croce switch (reboot_type) { 10172c622ed0SMatteo Croce case BOOT_TRIPLE: 10182c622ed0SMatteo Croce val = BOOT_TRIPLE_STR; 10192c622ed0SMatteo Croce break; 10202c622ed0SMatteo Croce case BOOT_KBD: 10212c622ed0SMatteo Croce val = BOOT_KBD_STR; 10222c622ed0SMatteo Croce break; 10232c622ed0SMatteo Croce case BOOT_BIOS: 10242c622ed0SMatteo Croce val = BOOT_BIOS_STR; 10252c622ed0SMatteo Croce break; 10262c622ed0SMatteo Croce case BOOT_ACPI: 10272c622ed0SMatteo Croce val = BOOT_ACPI_STR; 10282c622ed0SMatteo Croce break; 10292c622ed0SMatteo Croce case BOOT_EFI: 10302c622ed0SMatteo Croce val = BOOT_EFI_STR; 10312c622ed0SMatteo Croce break; 10322c622ed0SMatteo Croce case BOOT_CF9_FORCE: 10330c5c0179SMatteo Croce val = BOOT_PCI_STR; 10342c622ed0SMatteo Croce break; 10352c622ed0SMatteo Croce default: 10362c622ed0SMatteo Croce val = REBOOT_UNDEFINED_STR; 10372c622ed0SMatteo Croce } 10382c622ed0SMatteo Croce 10392c622ed0SMatteo Croce return sprintf(buf, "%s\n", val); 10402c622ed0SMatteo Croce } 10412c622ed0SMatteo Croce static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr, 10422c622ed0SMatteo Croce const char *buf, size_t count) 10432c622ed0SMatteo Croce { 10442c622ed0SMatteo Croce if (!capable(CAP_SYS_BOOT)) 10452c622ed0SMatteo Croce return -EPERM; 10462c622ed0SMatteo Croce 10472c622ed0SMatteo Croce if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR))) 10482c622ed0SMatteo Croce reboot_type = BOOT_TRIPLE; 10492c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR))) 10502c622ed0SMatteo Croce reboot_type = BOOT_KBD; 10512c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR))) 10522c622ed0SMatteo Croce reboot_type = BOOT_BIOS; 10532c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR))) 10542c622ed0SMatteo Croce reboot_type = BOOT_ACPI; 10552c622ed0SMatteo Croce else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR))) 10562c622ed0SMatteo Croce reboot_type = BOOT_EFI; 10570c5c0179SMatteo Croce else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR))) 10582c622ed0SMatteo Croce reboot_type = BOOT_CF9_FORCE; 10592c622ed0SMatteo Croce else 10602c622ed0SMatteo Croce return -EINVAL; 10612c622ed0SMatteo Croce 10621a9d079fSMatteo Croce reboot_default = 0; 10631a9d079fSMatteo Croce 10642c622ed0SMatteo Croce return count; 10652c622ed0SMatteo Croce } 10662c622ed0SMatteo Croce static struct kobj_attribute reboot_type_attr = __ATTR_RW(type); 106740247e55SMatteo Croce #endif 10682c622ed0SMatteo Croce 106940247e55SMatteo Croce #ifdef CONFIG_SMP 10702c622ed0SMatteo Croce static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 10712c622ed0SMatteo Croce { 10722c622ed0SMatteo Croce return sprintf(buf, "%d\n", reboot_cpu); 10732c622ed0SMatteo Croce } 10742c622ed0SMatteo Croce static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr, 10752c622ed0SMatteo Croce const char *buf, size_t count) 10762c622ed0SMatteo Croce { 10772c622ed0SMatteo Croce unsigned int cpunum; 10782c622ed0SMatteo Croce int rc; 10792c622ed0SMatteo Croce 10802c622ed0SMatteo Croce if (!capable(CAP_SYS_BOOT)) 10812c622ed0SMatteo Croce return -EPERM; 10822c622ed0SMatteo Croce 10832c622ed0SMatteo Croce rc = kstrtouint(buf, 0, &cpunum); 10842c622ed0SMatteo Croce 10852c622ed0SMatteo Croce if (rc) 10862c622ed0SMatteo Croce return rc; 10872c622ed0SMatteo Croce 10882c622ed0SMatteo Croce if (cpunum >= num_possible_cpus()) 10892c622ed0SMatteo Croce return -ERANGE; 10902c622ed0SMatteo Croce 10911a9d079fSMatteo Croce reboot_default = 0; 10922c622ed0SMatteo Croce reboot_cpu = cpunum; 10932c622ed0SMatteo Croce 10942c622ed0SMatteo Croce return count; 10952c622ed0SMatteo Croce } 10962c622ed0SMatteo Croce static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu); 109740247e55SMatteo Croce #endif 10982c622ed0SMatteo Croce 10992c622ed0SMatteo Croce static struct attribute *reboot_attrs[] = { 11002c622ed0SMatteo Croce &reboot_mode_attr.attr, 110140247e55SMatteo Croce #ifdef CONFIG_X86 11022c622ed0SMatteo Croce &reboot_force_attr.attr, 110340247e55SMatteo Croce &reboot_type_attr.attr, 110440247e55SMatteo Croce #endif 110540247e55SMatteo Croce #ifdef CONFIG_SMP 110640247e55SMatteo Croce &reboot_cpu_attr.attr, 110740247e55SMatteo Croce #endif 11082c622ed0SMatteo Croce NULL, 11092c622ed0SMatteo Croce }; 11102c622ed0SMatteo Croce 11112c622ed0SMatteo Croce static const struct attribute_group reboot_attr_group = { 11122c622ed0SMatteo Croce .attrs = reboot_attrs, 11132c622ed0SMatteo Croce }; 11142c622ed0SMatteo Croce 11152c622ed0SMatteo Croce static int __init reboot_ksysfs_init(void) 11162c622ed0SMatteo Croce { 11172c622ed0SMatteo Croce struct kobject *reboot_kobj; 11182c622ed0SMatteo Croce int ret; 11192c622ed0SMatteo Croce 11202c622ed0SMatteo Croce reboot_kobj = kobject_create_and_add("reboot", kernel_kobj); 11212c622ed0SMatteo Croce if (!reboot_kobj) 11222c622ed0SMatteo Croce return -ENOMEM; 11232c622ed0SMatteo Croce 11242c622ed0SMatteo Croce ret = sysfs_create_group(reboot_kobj, &reboot_attr_group); 11252c622ed0SMatteo Croce if (ret) { 11262c622ed0SMatteo Croce kobject_put(reboot_kobj); 11272c622ed0SMatteo Croce return ret; 11282c622ed0SMatteo Croce } 11292c622ed0SMatteo Croce 11302c622ed0SMatteo Croce return 0; 11312c622ed0SMatteo Croce } 11322c622ed0SMatteo Croce late_initcall(reboot_ksysfs_init); 11332c622ed0SMatteo Croce 11342c622ed0SMatteo Croce #endif 1135