1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/kernel/reboot.c 4 * 5 * Copyright (C) 2013 Linus Torvalds 6 */ 7 8 #define pr_fmt(fmt) "reboot: " fmt 9 10 #include <linux/atomic.h> 11 #include <linux/console.h> 12 #include <linux/ctype.h> 13 #include <linux/export.h> 14 #include <linux/kexec.h> 15 #include <linux/kmod.h> 16 #include <linux/kmsg_dump.h> 17 #include <linux/rcupdate.h> 18 #include <linux/reboot.h> 19 #include <linux/sched/signal.h> 20 #include <linux/suspend.h> 21 #include <linux/syscalls.h> 22 #include <linux/syscore_ops.h> 23 #include <linux/uaccess.h> 24 25 /* 26 * this indicates whether you can reboot with ctrl-alt-del: the default is yes 27 */ 28 29 static int C_A_D = 1; 30 struct pid __rcu *cad_pid; 31 32 #if defined(CONFIG_ARM) 33 #define DEFAULT_REBOOT_MODE = REBOOT_HARD 34 #else 35 #define DEFAULT_REBOOT_MODE 36 #endif 37 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE; 38 EXPORT_SYMBOL_GPL(reboot_mode); 39 enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED; 40 41 static enum hw_protection_action hw_protection_action = HWPROT_ACT_SHUTDOWN; 42 43 /* 44 * This variable is used privately to keep track of whether or not 45 * reboot_type is still set to its default value (i.e., reboot= hasn't 46 * been set on the command line). This is needed so that we can 47 * suppress DMI scanning for reboot quirks. Without it, it's 48 * impossible to override a faulty reboot quirk without recompiling. 49 */ 50 int reboot_default = 1; 51 int reboot_cpu; 52 enum reboot_type reboot_type = BOOT_ACPI; 53 int reboot_force; 54 55 struct sys_off_handler { 56 struct notifier_block nb; 57 int (*sys_off_cb)(struct sys_off_data *data); 58 void *cb_data; 59 enum sys_off_mode mode; 60 bool blocking; 61 void *list; 62 struct device *dev; 63 }; 64 65 /* 66 * This variable is used to indicate if a halt was initiated instead of a 67 * reboot when the reboot call was invoked with LINUX_REBOOT_CMD_POWER_OFF, but 68 * the system cannot be powered off. This allowes kernel_halt() to notify users 69 * of that. 70 */ 71 static bool poweroff_fallback_to_halt; 72 73 /* 74 * Temporary stub that prevents linkage failure while we're in process 75 * of removing all uses of legacy pm_power_off() around the kernel. 76 */ 77 void __weak (*pm_power_off)(void); 78 79 /* 80 * Notifier list for kernel code which wants to be called 81 * at shutdown. This is used to stop any idling DMA operations 82 * and the like. 83 */ 84 static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list); 85 86 /** 87 * emergency_restart - reboot the system 88 * 89 * Without shutting down any hardware or taking any locks 90 * reboot the system. This is called when we know we are in 91 * trouble so this is our best effort to reboot. This is 92 * safe to call in interrupt context. 93 */ 94 void emergency_restart(void) 95 { 96 kmsg_dump(KMSG_DUMP_EMERG); 97 system_state = SYSTEM_RESTART; 98 99 /* 100 * NBCON kthreads won't be flushed and stopped properly in 101 * the emergency restart. Move them to the atomic mode. 102 */ 103 nbcon_cpu_emergency_enter(); 104 printk_trigger_flush(); 105 106 machine_emergency_restart(); 107 } 108 EXPORT_SYMBOL_GPL(emergency_restart); 109 110 void kernel_restart_prepare(char *cmd) 111 { 112 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); 113 system_state = SYSTEM_RESTART; 114 usermodehelper_disable(); 115 device_shutdown(); 116 } 117 118 /** 119 * register_reboot_notifier - Register function to be called at reboot time 120 * @nb: Info about notifier function to be called 121 * 122 * Registers a function with the list of functions 123 * to be called at reboot time. 124 * 125 * Currently always returns zero, as blocking_notifier_chain_register() 126 * always returns zero. 127 */ 128 int register_reboot_notifier(struct notifier_block *nb) 129 { 130 return blocking_notifier_chain_register(&reboot_notifier_list, nb); 131 } 132 EXPORT_SYMBOL(register_reboot_notifier); 133 134 /** 135 * unregister_reboot_notifier - Unregister previously registered reboot notifier 136 * @nb: Hook to be unregistered 137 * 138 * Unregisters a previously registered reboot 139 * notifier function. 140 * 141 * Returns zero on success, or %-ENOENT on failure. 142 */ 143 int unregister_reboot_notifier(struct notifier_block *nb) 144 { 145 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb); 146 } 147 EXPORT_SYMBOL(unregister_reboot_notifier); 148 149 static void devm_unregister_reboot_notifier(struct device *dev, void *res) 150 { 151 WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res)); 152 } 153 154 int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb) 155 { 156 struct notifier_block **rcnb; 157 int ret; 158 159 rcnb = devres_alloc(devm_unregister_reboot_notifier, 160 sizeof(*rcnb), GFP_KERNEL); 161 if (!rcnb) 162 return -ENOMEM; 163 164 ret = register_reboot_notifier(nb); 165 if (!ret) { 166 *rcnb = nb; 167 devres_add(dev, rcnb); 168 } else { 169 devres_free(rcnb); 170 } 171 172 return ret; 173 } 174 EXPORT_SYMBOL(devm_register_reboot_notifier); 175 176 /* 177 * Notifier list for kernel code which wants to be called 178 * to restart the system. 179 */ 180 static ATOMIC_NOTIFIER_HEAD(restart_handler_list); 181 182 /** 183 * register_restart_handler - Register function to be called to reset 184 * the system 185 * @nb: Info about handler function to be called 186 * @nb->priority: Handler priority. Handlers should follow the 187 * following guidelines for setting priorities. 188 * 0: Restart handler of last resort, 189 * with limited restart capabilities 190 * 128: Default restart handler; use if no other 191 * restart handler is expected to be available, 192 * and/or if restart functionality is 193 * sufficient to restart the entire system 194 * 255: Highest priority restart handler, will 195 * preempt all other restart handlers 196 * 197 * Registers a function with code to be called to restart the 198 * system. 199 * 200 * Registered functions will be called from machine_restart as last 201 * step of the restart sequence (if the architecture specific 202 * machine_restart function calls do_kernel_restart - see below 203 * for details). 204 * Registered functions are expected to restart the system immediately. 205 * If more than one function is registered, the restart handler priority 206 * selects which function will be called first. 207 * 208 * Restart handlers are expected to be registered from non-architecture 209 * code, typically from drivers. A typical use case would be a system 210 * where restart functionality is provided through a watchdog. Multiple 211 * restart handlers may exist; for example, one restart handler might 212 * restart the entire system, while another only restarts the CPU. 213 * In such cases, the restart handler which only restarts part of the 214 * hardware is expected to register with low priority to ensure that 215 * it only runs if no other means to restart the system is available. 216 * 217 * Currently always returns zero, as atomic_notifier_chain_register() 218 * always returns zero. 219 */ 220 int register_restart_handler(struct notifier_block *nb) 221 { 222 return atomic_notifier_chain_register(&restart_handler_list, nb); 223 } 224 EXPORT_SYMBOL(register_restart_handler); 225 226 /** 227 * unregister_restart_handler - Unregister previously registered 228 * restart handler 229 * @nb: Hook to be unregistered 230 * 231 * Unregisters a previously registered restart handler function. 232 * 233 * Returns zero on success, or %-ENOENT on failure. 234 */ 235 int unregister_restart_handler(struct notifier_block *nb) 236 { 237 return atomic_notifier_chain_unregister(&restart_handler_list, nb); 238 } 239 EXPORT_SYMBOL(unregister_restart_handler); 240 241 /** 242 * do_kernel_restart - Execute kernel restart handler call chain 243 * 244 * @cmd: pointer to buffer containing command to execute for restart 245 * or %NULL 246 * 247 * Calls functions registered with register_restart_handler. 248 * 249 * Expected to be called from machine_restart as last step of the restart 250 * sequence. 251 * 252 * Restarts the system immediately if a restart handler function has been 253 * registered. Otherwise does nothing. 254 */ 255 void do_kernel_restart(char *cmd) 256 { 257 atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd); 258 } 259 260 void migrate_to_reboot_cpu(void) 261 { 262 /* The boot cpu is always logical cpu 0 */ 263 int cpu = reboot_cpu; 264 265 cpu_hotplug_disable(); 266 267 /* Make certain the cpu I'm about to reboot on is online */ 268 if (!cpu_online(cpu)) 269 cpu = cpumask_first(cpu_online_mask); 270 271 /* Prevent races with other tasks migrating this task */ 272 current->flags |= PF_NO_SETAFFINITY; 273 274 /* Make certain I only run on the appropriate processor */ 275 set_cpus_allowed_ptr(current, cpumask_of(cpu)); 276 } 277 278 /* 279 * Notifier list for kernel code which wants to be called 280 * to prepare system for restart. 281 */ 282 static BLOCKING_NOTIFIER_HEAD(restart_prep_handler_list); 283 284 static void do_kernel_restart_prepare(void) 285 { 286 blocking_notifier_call_chain(&restart_prep_handler_list, 0, NULL); 287 } 288 289 /** 290 * kernel_restart - reboot the system 291 * @cmd: pointer to buffer containing command to execute for restart 292 * or %NULL 293 * 294 * Shutdown everything and perform a clean reboot. 295 * This is not safe to call in interrupt context. 296 */ 297 void kernel_restart(char *cmd) 298 { 299 kernel_restart_prepare(cmd); 300 do_kernel_restart_prepare(); 301 migrate_to_reboot_cpu(); 302 syscore_shutdown(); 303 if (!cmd) 304 pr_emerg("Restarting system\n"); 305 else 306 pr_emerg("Restarting system with command '%s'\n", cmd); 307 kmsg_dump(KMSG_DUMP_SHUTDOWN); 308 machine_restart(cmd); 309 } 310 EXPORT_SYMBOL_GPL(kernel_restart); 311 312 static void kernel_shutdown_prepare(enum system_states state) 313 { 314 blocking_notifier_call_chain(&reboot_notifier_list, 315 (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL); 316 system_state = state; 317 usermodehelper_disable(); 318 device_shutdown(); 319 } 320 /** 321 * kernel_halt - halt the system 322 * 323 * Shutdown everything and perform a clean system halt. 324 */ 325 void kernel_halt(void) 326 { 327 kernel_shutdown_prepare(SYSTEM_HALT); 328 migrate_to_reboot_cpu(); 329 syscore_shutdown(); 330 if (poweroff_fallback_to_halt) 331 pr_emerg("Power off not available: System halted instead\n"); 332 else 333 pr_emerg("System halted\n"); 334 kmsg_dump(KMSG_DUMP_SHUTDOWN); 335 machine_halt(); 336 } 337 EXPORT_SYMBOL_GPL(kernel_halt); 338 339 /* 340 * Notifier list for kernel code which wants to be called 341 * to prepare system for power off. 342 */ 343 static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list); 344 345 /* 346 * Notifier list for kernel code which wants to be called 347 * to power off system. 348 */ 349 static ATOMIC_NOTIFIER_HEAD(power_off_handler_list); 350 351 static int sys_off_notify(struct notifier_block *nb, 352 unsigned long mode, void *cmd) 353 { 354 struct sys_off_handler *handler; 355 struct sys_off_data data = {}; 356 357 handler = container_of(nb, struct sys_off_handler, nb); 358 data.cb_data = handler->cb_data; 359 data.mode = mode; 360 data.cmd = cmd; 361 data.dev = handler->dev; 362 363 return handler->sys_off_cb(&data); 364 } 365 366 static struct sys_off_handler platform_sys_off_handler; 367 368 static struct sys_off_handler *alloc_sys_off_handler(int priority) 369 { 370 struct sys_off_handler *handler; 371 gfp_t flags; 372 373 /* 374 * Platforms like m68k can't allocate sys_off handler dynamically 375 * at the early boot time because memory allocator isn't available yet. 376 */ 377 if (priority == SYS_OFF_PRIO_PLATFORM) { 378 handler = &platform_sys_off_handler; 379 if (handler->cb_data) 380 return ERR_PTR(-EBUSY); 381 } else { 382 if (system_state > SYSTEM_RUNNING) 383 flags = GFP_ATOMIC; 384 else 385 flags = GFP_KERNEL; 386 387 handler = kzalloc_obj(*handler, flags); 388 if (!handler) 389 return ERR_PTR(-ENOMEM); 390 } 391 392 return handler; 393 } 394 395 static void free_sys_off_handler(struct sys_off_handler *handler) 396 { 397 if (handler == &platform_sys_off_handler) 398 memset(handler, 0, sizeof(*handler)); 399 else 400 kfree(handler); 401 } 402 403 /** 404 * register_sys_off_handler - Register sys-off handler 405 * @mode: Sys-off mode 406 * @priority: Handler priority 407 * @callback: Callback function 408 * @cb_data: Callback argument 409 * 410 * Registers system power-off or restart handler that will be invoked 411 * at the step corresponding to the given sys-off mode. Handler's callback 412 * should return NOTIFY_DONE to permit execution of the next handler in 413 * the call chain or NOTIFY_STOP to break the chain (in error case for 414 * example). 415 * 416 * Multiple handlers can be registered at the default priority level. 417 * 418 * Only one handler can be registered at the non-default priority level, 419 * otherwise ERR_PTR(-EBUSY) is returned. 420 * 421 * Returns a new instance of struct sys_off_handler on success, or 422 * an ERR_PTR()-encoded error code otherwise. 423 */ 424 struct sys_off_handler * 425 register_sys_off_handler(enum sys_off_mode mode, 426 int priority, 427 int (*callback)(struct sys_off_data *data), 428 void *cb_data) 429 { 430 struct sys_off_handler *handler; 431 int err; 432 433 handler = alloc_sys_off_handler(priority); 434 if (IS_ERR(handler)) 435 return handler; 436 437 switch (mode) { 438 case SYS_OFF_MODE_POWER_OFF_PREPARE: 439 handler->list = &power_off_prep_handler_list; 440 handler->blocking = true; 441 break; 442 443 case SYS_OFF_MODE_POWER_OFF: 444 handler->list = &power_off_handler_list; 445 break; 446 447 case SYS_OFF_MODE_RESTART_PREPARE: 448 handler->list = &restart_prep_handler_list; 449 handler->blocking = true; 450 break; 451 452 case SYS_OFF_MODE_RESTART: 453 handler->list = &restart_handler_list; 454 break; 455 456 default: 457 free_sys_off_handler(handler); 458 return ERR_PTR(-EINVAL); 459 } 460 461 handler->nb.notifier_call = sys_off_notify; 462 handler->nb.priority = priority; 463 handler->sys_off_cb = callback; 464 handler->cb_data = cb_data; 465 handler->mode = mode; 466 467 if (handler->blocking) { 468 if (priority == SYS_OFF_PRIO_DEFAULT) 469 err = blocking_notifier_chain_register(handler->list, 470 &handler->nb); 471 else 472 err = blocking_notifier_chain_register_unique_prio(handler->list, 473 &handler->nb); 474 } else { 475 if (priority == SYS_OFF_PRIO_DEFAULT) 476 err = atomic_notifier_chain_register(handler->list, 477 &handler->nb); 478 else 479 err = atomic_notifier_chain_register_unique_prio(handler->list, 480 &handler->nb); 481 } 482 483 if (err) { 484 free_sys_off_handler(handler); 485 return ERR_PTR(err); 486 } 487 488 return handler; 489 } 490 EXPORT_SYMBOL_GPL(register_sys_off_handler); 491 492 /** 493 * unregister_sys_off_handler - Unregister sys-off handler 494 * @handler: Sys-off handler 495 * 496 * Unregisters given sys-off handler. 497 */ 498 void unregister_sys_off_handler(struct sys_off_handler *handler) 499 { 500 int err; 501 502 if (IS_ERR_OR_NULL(handler)) 503 return; 504 505 if (handler->blocking) 506 err = blocking_notifier_chain_unregister(handler->list, 507 &handler->nb); 508 else 509 err = atomic_notifier_chain_unregister(handler->list, 510 &handler->nb); 511 512 /* sanity check, shall never happen */ 513 WARN_ON(err); 514 515 free_sys_off_handler(handler); 516 } 517 EXPORT_SYMBOL_GPL(unregister_sys_off_handler); 518 519 static void devm_unregister_sys_off_handler(void *data) 520 { 521 struct sys_off_handler *handler = data; 522 523 unregister_sys_off_handler(handler); 524 } 525 526 /** 527 * devm_register_sys_off_handler - Register sys-off handler 528 * @dev: Device that registers handler 529 * @mode: Sys-off mode 530 * @priority: Handler priority 531 * @callback: Callback function 532 * @cb_data: Callback argument 533 * 534 * Registers resource-managed sys-off handler. 535 * 536 * Returns zero on success, or error code on failure. 537 */ 538 int devm_register_sys_off_handler(struct device *dev, 539 enum sys_off_mode mode, 540 int priority, 541 int (*callback)(struct sys_off_data *data), 542 void *cb_data) 543 { 544 struct sys_off_handler *handler; 545 546 handler = register_sys_off_handler(mode, priority, callback, cb_data); 547 if (IS_ERR(handler)) 548 return PTR_ERR(handler); 549 handler->dev = dev; 550 551 return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler, 552 handler); 553 } 554 EXPORT_SYMBOL_GPL(devm_register_sys_off_handler); 555 556 /** 557 * devm_register_power_off_handler - Register power-off handler 558 * @dev: Device that registers callback 559 * @callback: Callback function 560 * @cb_data: Callback's argument 561 * 562 * Registers resource-managed sys-off handler with a default priority 563 * and using power-off mode. 564 * 565 * Returns zero on success, or error code on failure. 566 */ 567 int devm_register_power_off_handler(struct device *dev, 568 int (*callback)(struct sys_off_data *data), 569 void *cb_data) 570 { 571 return devm_register_sys_off_handler(dev, 572 SYS_OFF_MODE_POWER_OFF, 573 SYS_OFF_PRIO_DEFAULT, 574 callback, cb_data); 575 } 576 EXPORT_SYMBOL_GPL(devm_register_power_off_handler); 577 578 /** 579 * devm_register_restart_handler - Register restart handler 580 * @dev: Device that registers callback 581 * @callback: Callback function 582 * @cb_data: Callback's argument 583 * 584 * Registers resource-managed sys-off handler with a default priority 585 * and using restart mode. 586 * 587 * Returns zero on success, or error code on failure. 588 */ 589 int devm_register_restart_handler(struct device *dev, 590 int (*callback)(struct sys_off_data *data), 591 void *cb_data) 592 { 593 return devm_register_sys_off_handler(dev, 594 SYS_OFF_MODE_RESTART, 595 SYS_OFF_PRIO_DEFAULT, 596 callback, cb_data); 597 } 598 EXPORT_SYMBOL_GPL(devm_register_restart_handler); 599 600 static struct sys_off_handler *platform_power_off_handler; 601 602 static int platform_power_off_notify(struct sys_off_data *data) 603 { 604 void (*platform_power_power_off_cb)(void) = data->cb_data; 605 606 platform_power_power_off_cb(); 607 608 return NOTIFY_DONE; 609 } 610 611 /** 612 * register_platform_power_off - Register platform-level power-off callback 613 * @power_off: Power-off callback 614 * 615 * Registers power-off callback that will be called as last step 616 * of the power-off sequence. This callback is expected to be invoked 617 * for the last resort. Only one platform power-off callback is allowed 618 * to be registered at a time. 619 * 620 * Returns zero on success, or error code on failure. 621 */ 622 int register_platform_power_off(void (*power_off)(void)) 623 { 624 struct sys_off_handler *handler; 625 626 handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, 627 SYS_OFF_PRIO_PLATFORM, 628 platform_power_off_notify, 629 power_off); 630 if (IS_ERR(handler)) 631 return PTR_ERR(handler); 632 633 platform_power_off_handler = handler; 634 635 return 0; 636 } 637 EXPORT_SYMBOL_GPL(register_platform_power_off); 638 639 /** 640 * unregister_platform_power_off - Unregister platform-level power-off callback 641 * @power_off: Power-off callback 642 * 643 * Unregisters previously registered platform power-off callback. 644 */ 645 void unregister_platform_power_off(void (*power_off)(void)) 646 { 647 if (platform_power_off_handler && 648 platform_power_off_handler->cb_data == power_off) { 649 unregister_sys_off_handler(platform_power_off_handler); 650 platform_power_off_handler = NULL; 651 } 652 } 653 EXPORT_SYMBOL_GPL(unregister_platform_power_off); 654 655 static int legacy_pm_power_off(struct sys_off_data *data) 656 { 657 if (pm_power_off) 658 pm_power_off(); 659 660 return NOTIFY_DONE; 661 } 662 663 static void do_kernel_power_off_prepare(void) 664 { 665 blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL); 666 } 667 668 /** 669 * do_kernel_power_off - Execute kernel power-off handler call chain 670 * 671 * Expected to be called as last step of the power-off sequence. 672 * 673 * Powers off the system immediately if a power-off handler function has 674 * been registered. Otherwise does nothing. 675 */ 676 void do_kernel_power_off(void) 677 { 678 struct sys_off_handler *sys_off = NULL; 679 680 /* 681 * Register sys-off handlers for legacy PM callback. This allows 682 * legacy PM callbacks temporary co-exist with the new sys-off API. 683 * 684 * TODO: Remove legacy handlers once all legacy PM users will be 685 * switched to the sys-off based APIs. 686 */ 687 if (pm_power_off) 688 sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, 689 SYS_OFF_PRIO_DEFAULT, 690 legacy_pm_power_off, NULL); 691 692 atomic_notifier_call_chain(&power_off_handler_list, 0, NULL); 693 694 unregister_sys_off_handler(sys_off); 695 } 696 697 /** 698 * kernel_can_power_off - check whether system can be powered off 699 * 700 * Returns true if power-off handler is registered and system can be 701 * powered off, false otherwise. 702 */ 703 bool kernel_can_power_off(void) 704 { 705 return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) || 706 pm_power_off; 707 } 708 EXPORT_SYMBOL_GPL(kernel_can_power_off); 709 710 /** 711 * kernel_power_off - power_off the system 712 * 713 * Shutdown everything and perform a clean system power_off. 714 */ 715 void kernel_power_off(void) 716 { 717 kernel_shutdown_prepare(SYSTEM_POWER_OFF); 718 do_kernel_power_off_prepare(); 719 migrate_to_reboot_cpu(); 720 syscore_shutdown(); 721 pr_emerg("Power down\n"); 722 pr_flush(1000, true); 723 kmsg_dump(KMSG_DUMP_SHUTDOWN); 724 machine_power_off(); 725 } 726 EXPORT_SYMBOL_GPL(kernel_power_off); 727 728 DEFINE_MUTEX(system_transition_mutex); 729 730 /* 731 * Reboot system call: for obvious reasons only root may call it, 732 * and even root needs to set up some magic numbers in the registers 733 * so that some mistake won't make this reboot the whole machine. 734 * You can also set the meaning of the ctrl-alt-del-key here. 735 * 736 * reboot doesn't sync: do that yourself before calling this. 737 */ 738 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd, 739 void __user *, arg) 740 { 741 struct pid_namespace *pid_ns = task_active_pid_ns(current); 742 char buffer[256]; 743 int ret = 0; 744 745 /* We only trust the superuser with rebooting the system. */ 746 if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT)) 747 return -EPERM; 748 749 /* For safety, we require "magic" arguments. */ 750 if (magic1 != LINUX_REBOOT_MAGIC1 || 751 (magic2 != LINUX_REBOOT_MAGIC2 && 752 magic2 != LINUX_REBOOT_MAGIC2A && 753 magic2 != LINUX_REBOOT_MAGIC2B && 754 magic2 != LINUX_REBOOT_MAGIC2C)) 755 return -EINVAL; 756 757 /* 758 * If pid namespaces are enabled and the current task is in a child 759 * pid_namespace, the command is handled by reboot_pid_ns() which will 760 * call do_exit(). 761 */ 762 ret = reboot_pid_ns(pid_ns, cmd); 763 if (ret) 764 return ret; 765 766 /* Instead of trying to make the power_off code look like 767 * halt when pm_power_off is not set do it the easy way. 768 */ 769 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off()) { 770 poweroff_fallback_to_halt = true; 771 cmd = LINUX_REBOOT_CMD_HALT; 772 } 773 774 mutex_lock(&system_transition_mutex); 775 switch (cmd) { 776 case LINUX_REBOOT_CMD_RESTART: 777 kernel_restart(NULL); 778 break; 779 780 case LINUX_REBOOT_CMD_CAD_ON: 781 C_A_D = 1; 782 break; 783 784 case LINUX_REBOOT_CMD_CAD_OFF: 785 C_A_D = 0; 786 break; 787 788 case LINUX_REBOOT_CMD_HALT: 789 kernel_halt(); 790 do_exit(0); 791 792 case LINUX_REBOOT_CMD_POWER_OFF: 793 kernel_power_off(); 794 do_exit(0); 795 break; 796 797 case LINUX_REBOOT_CMD_RESTART2: 798 ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1); 799 if (ret < 0) { 800 ret = -EFAULT; 801 break; 802 } 803 buffer[sizeof(buffer) - 1] = '\0'; 804 805 kernel_restart(buffer); 806 break; 807 808 #ifdef CONFIG_KEXEC_CORE 809 case LINUX_REBOOT_CMD_KEXEC: 810 ret = kernel_kexec(); 811 break; 812 #endif 813 814 #ifdef CONFIG_HIBERNATION 815 case LINUX_REBOOT_CMD_SW_SUSPEND: 816 ret = hibernate(); 817 break; 818 #endif 819 820 default: 821 ret = -EINVAL; 822 break; 823 } 824 mutex_unlock(&system_transition_mutex); 825 return ret; 826 } 827 828 static void deferred_cad(struct work_struct *dummy) 829 { 830 kernel_restart(NULL); 831 } 832 833 /* 834 * This function gets called by ctrl-alt-del - ie the keyboard interrupt. 835 * As it's called within an interrupt, it may NOT sync: the only choice 836 * is whether to reboot at once, or just ignore the ctrl-alt-del. 837 */ 838 void ctrl_alt_del(void) 839 { 840 static DECLARE_WORK(cad_work, deferred_cad); 841 842 if (C_A_D) 843 schedule_work(&cad_work); 844 else 845 kill_cad_pid(SIGINT, 1); 846 } 847 848 #define POWEROFF_CMD_PATH_LEN 256 849 static char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff"; 850 static const char reboot_cmd[] = "/sbin/reboot"; 851 852 static int run_cmd(const char *cmd) 853 { 854 char **argv; 855 static char *envp[] = { 856 "HOME=/", 857 "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 858 NULL 859 }; 860 int ret; 861 argv = argv_split(GFP_KERNEL, cmd, NULL); 862 if (argv) { 863 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); 864 argv_free(argv); 865 } else { 866 ret = -ENOMEM; 867 } 868 869 return ret; 870 } 871 872 static int __orderly_reboot(void) 873 { 874 int ret; 875 876 ret = run_cmd(reboot_cmd); 877 878 if (ret) { 879 pr_warn("Failed to start orderly reboot: forcing the issue\n"); 880 emergency_sync(); 881 kernel_restart(NULL); 882 } 883 884 return ret; 885 } 886 887 static int __orderly_poweroff(bool force) 888 { 889 int ret; 890 891 ret = run_cmd(poweroff_cmd); 892 893 if (ret && force) { 894 pr_warn("Failed to start orderly shutdown: forcing the issue\n"); 895 896 /* 897 * I guess this should try to kick off some daemon to sync and 898 * poweroff asap. Or not even bother syncing if we're doing an 899 * emergency shutdown? 900 */ 901 emergency_sync(); 902 kernel_power_off(); 903 } 904 905 return ret; 906 } 907 908 static bool poweroff_force; 909 910 static void poweroff_work_func(struct work_struct *work) 911 { 912 __orderly_poweroff(poweroff_force); 913 } 914 915 static DECLARE_WORK(poweroff_work, poweroff_work_func); 916 917 /** 918 * orderly_poweroff - Trigger an orderly system poweroff 919 * @force: force poweroff if command execution fails 920 * 921 * This may be called from any context to trigger a system shutdown. 922 * If the orderly shutdown fails, it will force an immediate shutdown. 923 */ 924 void orderly_poweroff(bool force) 925 { 926 if (force) /* do not override the pending "true" */ 927 poweroff_force = true; 928 schedule_work(&poweroff_work); 929 } 930 EXPORT_SYMBOL_GPL(orderly_poweroff); 931 932 static void reboot_work_func(struct work_struct *work) 933 { 934 __orderly_reboot(); 935 } 936 937 static DECLARE_WORK(reboot_work, reboot_work_func); 938 939 /** 940 * orderly_reboot - Trigger an orderly system reboot 941 * 942 * This may be called from any context to trigger a system reboot. 943 * If the orderly reboot fails, it will force an immediate reboot. 944 */ 945 void orderly_reboot(void) 946 { 947 schedule_work(&reboot_work); 948 } 949 EXPORT_SYMBOL_GPL(orderly_reboot); 950 951 static const char *hw_protection_action_str(enum hw_protection_action action) 952 { 953 switch (action) { 954 case HWPROT_ACT_SHUTDOWN: 955 return "shutdown"; 956 case HWPROT_ACT_REBOOT: 957 return "reboot"; 958 default: 959 return "undefined"; 960 } 961 } 962 963 static enum hw_protection_action hw_failure_emergency_action; 964 965 /** 966 * hw_failure_emergency_action_func - emergency action work after a known delay 967 * @work: work_struct associated with the emergency action function 968 * 969 * This function is called in very critical situations to force 970 * a kernel poweroff or reboot after a configurable timeout value. 971 */ 972 static void hw_failure_emergency_action_func(struct work_struct *work) 973 { 974 const char *action_str = hw_protection_action_str(hw_failure_emergency_action); 975 976 pr_emerg("Hardware protection timed-out. Trying forced %s\n", 977 action_str); 978 979 /* 980 * We have reached here after the emergency action waiting period has 981 * expired. This means orderly_poweroff/reboot has not been able to 982 * shut off the system for some reason. 983 * 984 * Try to shut off the system immediately if possible 985 */ 986 987 if (hw_failure_emergency_action == HWPROT_ACT_REBOOT) 988 kernel_restart(NULL); 989 else 990 kernel_power_off(); 991 992 /* 993 * Worst of the worst case trigger emergency restart 994 */ 995 pr_emerg("Hardware protection %s failed. Trying emergency restart\n", 996 action_str); 997 emergency_restart(); 998 } 999 1000 static DECLARE_DELAYED_WORK(hw_failure_emergency_action_work, 1001 hw_failure_emergency_action_func); 1002 1003 /** 1004 * hw_failure_emergency_schedule - Schedule an emergency system shutdown or reboot 1005 * 1006 * @action: The hardware protection action to be taken 1007 * @action_delay_ms: Time in milliseconds to elapse before triggering action 1008 * 1009 * This may be called from any critical situation to trigger a system shutdown 1010 * or reboot after a given period of time. 1011 * If time is negative this is not scheduled. 1012 */ 1013 static void hw_failure_emergency_schedule(enum hw_protection_action action, 1014 int action_delay_ms) 1015 { 1016 if (action_delay_ms <= 0) 1017 return; 1018 hw_failure_emergency_action = action; 1019 schedule_delayed_work(&hw_failure_emergency_action_work, 1020 msecs_to_jiffies(action_delay_ms)); 1021 } 1022 1023 /** 1024 * __hw_protection_trigger - Trigger an emergency system shutdown or reboot 1025 * 1026 * @reason: Reason of emergency shutdown or reboot to be printed. 1027 * @ms_until_forced: Time to wait for orderly shutdown or reboot before 1028 * triggering it. Negative value disables the forced 1029 * shutdown or reboot. 1030 * @action: The hardware protection action to be taken. 1031 * 1032 * Initiate an emergency system shutdown or reboot in order to protect 1033 * hardware from further damage. Usage examples include a thermal protection. 1034 * NOTE: The request is ignored if protection shutdown or reboot is already 1035 * pending even if the previous request has given a large timeout for forced 1036 * shutdown/reboot. 1037 */ 1038 void __hw_protection_trigger(const char *reason, int ms_until_forced, 1039 enum hw_protection_action action) 1040 { 1041 static atomic_t allow_proceed = ATOMIC_INIT(1); 1042 1043 if (action == HWPROT_ACT_DEFAULT) 1044 action = hw_protection_action; 1045 1046 pr_emerg("HARDWARE PROTECTION %s (%s)\n", 1047 hw_protection_action_str(action), reason); 1048 1049 /* Shutdown should be initiated only once. */ 1050 if (!atomic_dec_and_test(&allow_proceed)) 1051 return; 1052 1053 /* 1054 * Queue a backup emergency shutdown in the event of 1055 * orderly_poweroff failure 1056 */ 1057 hw_failure_emergency_schedule(action, ms_until_forced); 1058 if (action == HWPROT_ACT_REBOOT) 1059 orderly_reboot(); 1060 else 1061 orderly_poweroff(true); 1062 } 1063 EXPORT_SYMBOL_GPL(__hw_protection_trigger); 1064 1065 static bool hw_protection_action_parse(const char *str, 1066 enum hw_protection_action *action) 1067 { 1068 if (sysfs_streq(str, "shutdown")) 1069 *action = HWPROT_ACT_SHUTDOWN; 1070 else if (sysfs_streq(str, "reboot")) 1071 *action = HWPROT_ACT_REBOOT; 1072 else 1073 return false; 1074 1075 return true; 1076 } 1077 1078 static int __init hw_protection_setup(char *str) 1079 { 1080 hw_protection_action_parse(str, &hw_protection_action); 1081 return 1; 1082 } 1083 __setup("hw_protection=", hw_protection_setup); 1084 1085 #ifdef CONFIG_SYSFS 1086 static ssize_t hw_protection_show(struct kobject *kobj, 1087 struct kobj_attribute *attr, char *buf) 1088 { 1089 return sysfs_emit(buf, "%s\n", 1090 hw_protection_action_str(hw_protection_action)); 1091 } 1092 static ssize_t hw_protection_store(struct kobject *kobj, 1093 struct kobj_attribute *attr, const char *buf, 1094 size_t count) 1095 { 1096 if (!capable(CAP_SYS_ADMIN)) 1097 return -EPERM; 1098 1099 if (!hw_protection_action_parse(buf, &hw_protection_action)) 1100 return -EINVAL; 1101 1102 return count; 1103 } 1104 static struct kobj_attribute hw_protection_attr = __ATTR_RW(hw_protection); 1105 #endif 1106 1107 static int __init reboot_setup(char *str) 1108 { 1109 for (;;) { 1110 enum reboot_mode *mode; 1111 1112 /* 1113 * Having anything passed on the command line via 1114 * reboot= will cause us to disable DMI checking 1115 * below. 1116 */ 1117 reboot_default = 0; 1118 1119 if (!strncmp(str, "panic_", 6)) { 1120 mode = &panic_reboot_mode; 1121 str += 6; 1122 } else { 1123 mode = &reboot_mode; 1124 } 1125 1126 switch (*str) { 1127 case 'w': 1128 *mode = REBOOT_WARM; 1129 break; 1130 1131 case 'c': 1132 *mode = REBOOT_COLD; 1133 break; 1134 1135 case 'h': 1136 *mode = REBOOT_HARD; 1137 break; 1138 1139 case 's': 1140 /* 1141 * reboot_cpu is s[mp]#### with #### being the processor 1142 * to be used for rebooting. Skip 's' or 'smp' prefix. 1143 */ 1144 str += str[1] == 'm' && str[2] == 'p' ? 3 : 1; 1145 1146 if (isdigit(str[0])) { 1147 int cpu = simple_strtoul(str, NULL, 0); 1148 1149 if (cpu >= num_possible_cpus()) { 1150 pr_err("Ignoring the CPU number in reboot= option. " 1151 "CPU %d exceeds possible cpu number %d\n", 1152 cpu, num_possible_cpus()); 1153 break; 1154 } 1155 reboot_cpu = cpu; 1156 } else 1157 *mode = REBOOT_SOFT; 1158 break; 1159 1160 case 'g': 1161 *mode = REBOOT_GPIO; 1162 break; 1163 1164 case 'b': 1165 case 'a': 1166 case 'k': 1167 case 't': 1168 case 'e': 1169 case 'p': 1170 reboot_type = *str; 1171 break; 1172 1173 case 'f': 1174 reboot_force = 1; 1175 break; 1176 } 1177 1178 str = strchr(str, ','); 1179 if (str) 1180 str++; 1181 else 1182 break; 1183 } 1184 return 1; 1185 } 1186 __setup("reboot=", reboot_setup); 1187 1188 #ifdef CONFIG_SYSFS 1189 1190 #define REBOOT_COLD_STR "cold" 1191 #define REBOOT_WARM_STR "warm" 1192 #define REBOOT_HARD_STR "hard" 1193 #define REBOOT_SOFT_STR "soft" 1194 #define REBOOT_GPIO_STR "gpio" 1195 #define REBOOT_UNDEFINED_STR "undefined" 1196 1197 #define BOOT_TRIPLE_STR "triple" 1198 #define BOOT_KBD_STR "kbd" 1199 #define BOOT_BIOS_STR "bios" 1200 #define BOOT_ACPI_STR "acpi" 1201 #define BOOT_EFI_STR "efi" 1202 #define BOOT_PCI_STR "pci" 1203 1204 static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 1205 { 1206 const char *val; 1207 1208 switch (reboot_mode) { 1209 case REBOOT_COLD: 1210 val = REBOOT_COLD_STR; 1211 break; 1212 case REBOOT_WARM: 1213 val = REBOOT_WARM_STR; 1214 break; 1215 case REBOOT_HARD: 1216 val = REBOOT_HARD_STR; 1217 break; 1218 case REBOOT_SOFT: 1219 val = REBOOT_SOFT_STR; 1220 break; 1221 case REBOOT_GPIO: 1222 val = REBOOT_GPIO_STR; 1223 break; 1224 default: 1225 val = REBOOT_UNDEFINED_STR; 1226 } 1227 1228 return sysfs_emit(buf, "%s\n", val); 1229 } 1230 static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr, 1231 const char *buf, size_t count) 1232 { 1233 if (!capable(CAP_SYS_BOOT)) 1234 return -EPERM; 1235 1236 if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR))) 1237 reboot_mode = REBOOT_COLD; 1238 else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR))) 1239 reboot_mode = REBOOT_WARM; 1240 else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR))) 1241 reboot_mode = REBOOT_HARD; 1242 else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR))) 1243 reboot_mode = REBOOT_SOFT; 1244 else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR))) 1245 reboot_mode = REBOOT_GPIO; 1246 else 1247 return -EINVAL; 1248 1249 reboot_default = 0; 1250 1251 return count; 1252 } 1253 static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode); 1254 1255 #ifdef CONFIG_X86 1256 static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 1257 { 1258 return sysfs_emit(buf, "%d\n", reboot_force); 1259 } 1260 static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr, 1261 const char *buf, size_t count) 1262 { 1263 bool res; 1264 1265 if (!capable(CAP_SYS_BOOT)) 1266 return -EPERM; 1267 1268 if (kstrtobool(buf, &res)) 1269 return -EINVAL; 1270 1271 reboot_default = 0; 1272 reboot_force = res; 1273 1274 return count; 1275 } 1276 static struct kobj_attribute reboot_force_attr = __ATTR_RW(force); 1277 1278 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 1279 { 1280 const char *val; 1281 1282 switch (reboot_type) { 1283 case BOOT_TRIPLE: 1284 val = BOOT_TRIPLE_STR; 1285 break; 1286 case BOOT_KBD: 1287 val = BOOT_KBD_STR; 1288 break; 1289 case BOOT_BIOS: 1290 val = BOOT_BIOS_STR; 1291 break; 1292 case BOOT_ACPI: 1293 val = BOOT_ACPI_STR; 1294 break; 1295 case BOOT_EFI: 1296 val = BOOT_EFI_STR; 1297 break; 1298 case BOOT_CF9_FORCE: 1299 val = BOOT_PCI_STR; 1300 break; 1301 default: 1302 val = REBOOT_UNDEFINED_STR; 1303 } 1304 1305 return sysfs_emit(buf, "%s\n", val); 1306 } 1307 static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr, 1308 const char *buf, size_t count) 1309 { 1310 if (!capable(CAP_SYS_BOOT)) 1311 return -EPERM; 1312 1313 if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR))) 1314 reboot_type = BOOT_TRIPLE; 1315 else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR))) 1316 reboot_type = BOOT_KBD; 1317 else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR))) 1318 reboot_type = BOOT_BIOS; 1319 else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR))) 1320 reboot_type = BOOT_ACPI; 1321 else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR))) 1322 reboot_type = BOOT_EFI; 1323 else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR))) 1324 reboot_type = BOOT_CF9_FORCE; 1325 else 1326 return -EINVAL; 1327 1328 reboot_default = 0; 1329 1330 return count; 1331 } 1332 static struct kobj_attribute reboot_type_attr = __ATTR_RW(type); 1333 #endif 1334 1335 #ifdef CONFIG_SMP 1336 static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 1337 { 1338 return sysfs_emit(buf, "%d\n", reboot_cpu); 1339 } 1340 static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr, 1341 const char *buf, size_t count) 1342 { 1343 unsigned int cpunum; 1344 int rc; 1345 1346 if (!capable(CAP_SYS_BOOT)) 1347 return -EPERM; 1348 1349 rc = kstrtouint(buf, 0, &cpunum); 1350 1351 if (rc) 1352 return rc; 1353 1354 if (cpunum >= num_possible_cpus()) 1355 return -ERANGE; 1356 1357 reboot_default = 0; 1358 reboot_cpu = cpunum; 1359 1360 return count; 1361 } 1362 static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu); 1363 #endif 1364 1365 static struct attribute *reboot_attrs[] = { 1366 &hw_protection_attr.attr, 1367 &reboot_mode_attr.attr, 1368 #ifdef CONFIG_X86 1369 &reboot_force_attr.attr, 1370 &reboot_type_attr.attr, 1371 #endif 1372 #ifdef CONFIG_SMP 1373 &reboot_cpu_attr.attr, 1374 #endif 1375 NULL, 1376 }; 1377 1378 #ifdef CONFIG_SYSCTL 1379 static int proc_do_cad_pid(const struct ctl_table *table, int write, void *buffer, 1380 size_t *lenp, loff_t *ppos) 1381 { 1382 struct ctl_table tmp_table = *table; 1383 struct pid *new_pid; 1384 struct pid *old_pid; 1385 pid_t tmp_pid; 1386 int r; 1387 1388 rcu_read_lock(); 1389 tmp_pid = pid_vnr(rcu_dereference(cad_pid)); 1390 rcu_read_unlock(); 1391 1392 tmp_table.data = &tmp_pid; 1393 1394 r = proc_dointvec(&tmp_table, write, buffer, lenp, ppos); 1395 if (r || !write) 1396 return r; 1397 1398 new_pid = find_get_pid(tmp_pid); 1399 if (!new_pid) 1400 return -ESRCH; 1401 1402 old_pid = unrcu_pointer(xchg(&cad_pid, RCU_INITIALIZER(new_pid))); 1403 /* 1404 * Wait for cad_pid readers before put_pid(). We cannot use 1405 * call_rcu() here because free_pid() already owns pid->rcu. 1406 */ 1407 synchronize_rcu(); 1408 put_pid(old_pid); 1409 return 0; 1410 } 1411 1412 static const struct ctl_table kern_reboot_table[] = { 1413 { 1414 .procname = "poweroff_cmd", 1415 .data = &poweroff_cmd, 1416 .maxlen = POWEROFF_CMD_PATH_LEN, 1417 .mode = 0644, 1418 .proc_handler = proc_dostring, 1419 }, 1420 { 1421 .procname = "ctrl-alt-del", 1422 .data = &C_A_D, 1423 .maxlen = sizeof(int), 1424 .mode = 0644, 1425 .proc_handler = proc_dointvec, 1426 }, 1427 { 1428 .procname = "cad_pid", 1429 .maxlen = sizeof(int), 1430 .mode = 0600, 1431 .proc_handler = proc_do_cad_pid, 1432 }, 1433 }; 1434 1435 static void __init kernel_reboot_sysctls_init(void) 1436 { 1437 register_sysctl_init("kernel", kern_reboot_table); 1438 } 1439 #else 1440 #define kernel_reboot_sysctls_init() do { } while (0) 1441 #endif /* CONFIG_SYSCTL */ 1442 1443 static const struct attribute_group reboot_attr_group = { 1444 .attrs = reboot_attrs, 1445 }; 1446 1447 static int __init reboot_ksysfs_init(void) 1448 { 1449 struct kobject *reboot_kobj; 1450 int ret; 1451 1452 reboot_kobj = kobject_create_and_add("reboot", kernel_kobj); 1453 if (!reboot_kobj) 1454 return -ENOMEM; 1455 1456 ret = sysfs_create_group(reboot_kobj, &reboot_attr_group); 1457 if (ret) { 1458 kobject_put(reboot_kobj); 1459 return ret; 1460 } 1461 1462 kernel_reboot_sysctls_init(); 1463 1464 return 0; 1465 } 1466 late_initcall(reboot_ksysfs_init); 1467 1468 #endif 1469