1 /* 2 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support. 3 * 4 * Copyright (c) 2003 Patrick Mochel 5 * Copyright (c) 2003 Open Source Development Lab 6 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz> 7 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc. 8 * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com> 9 * 10 * This file is released under the GPLv2. 11 */ 12 13 #define pr_fmt(fmt) "PM: " fmt 14 15 #include <linux/export.h> 16 #include <linux/suspend.h> 17 #include <linux/syscalls.h> 18 #include <linux/reboot.h> 19 #include <linux/string.h> 20 #include <linux/device.h> 21 #include <linux/async.h> 22 #include <linux/delay.h> 23 #include <linux/fs.h> 24 #include <linux/mount.h> 25 #include <linux/pm.h> 26 #include <linux/nmi.h> 27 #include <linux/console.h> 28 #include <linux/cpu.h> 29 #include <linux/freezer.h> 30 #include <linux/gfp.h> 31 #include <linux/syscore_ops.h> 32 #include <linux/ctype.h> 33 #include <linux/genhd.h> 34 #include <linux/ktime.h> 35 #include <trace/events/power.h> 36 37 #include "power.h" 38 39 40 static int nocompress; 41 static int noresume; 42 static int nohibernate; 43 static int resume_wait; 44 static unsigned int resume_delay; 45 static char resume_file[256] = CONFIG_PM_STD_PARTITION; 46 dev_t swsusp_resume_device; 47 sector_t swsusp_resume_block; 48 __visible int in_suspend __nosavedata; 49 50 enum { 51 HIBERNATION_INVALID, 52 HIBERNATION_PLATFORM, 53 HIBERNATION_SHUTDOWN, 54 HIBERNATION_REBOOT, 55 #ifdef CONFIG_SUSPEND 56 HIBERNATION_SUSPEND, 57 #endif 58 HIBERNATION_TEST_RESUME, 59 /* keep last */ 60 __HIBERNATION_AFTER_LAST 61 }; 62 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1) 63 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1) 64 65 static int hibernation_mode = HIBERNATION_SHUTDOWN; 66 67 bool freezer_test_done; 68 69 static const struct platform_hibernation_ops *hibernation_ops; 70 71 bool hibernation_available(void) 72 { 73 return (nohibernate == 0); 74 } 75 76 /** 77 * hibernation_set_ops - Set the global hibernate operations. 78 * @ops: Hibernation operations to use in subsequent hibernation transitions. 79 */ 80 void hibernation_set_ops(const struct platform_hibernation_ops *ops) 81 { 82 if (ops && !(ops->begin && ops->end && ops->pre_snapshot 83 && ops->prepare && ops->finish && ops->enter && ops->pre_restore 84 && ops->restore_cleanup && ops->leave)) { 85 WARN_ON(1); 86 return; 87 } 88 lock_system_sleep(); 89 hibernation_ops = ops; 90 if (ops) 91 hibernation_mode = HIBERNATION_PLATFORM; 92 else if (hibernation_mode == HIBERNATION_PLATFORM) 93 hibernation_mode = HIBERNATION_SHUTDOWN; 94 95 unlock_system_sleep(); 96 } 97 EXPORT_SYMBOL_GPL(hibernation_set_ops); 98 99 static bool entering_platform_hibernation; 100 101 bool system_entering_hibernation(void) 102 { 103 return entering_platform_hibernation; 104 } 105 EXPORT_SYMBOL(system_entering_hibernation); 106 107 #ifdef CONFIG_PM_DEBUG 108 static void hibernation_debug_sleep(void) 109 { 110 pr_info("hibernation debug: Waiting for 5 seconds.\n"); 111 mdelay(5000); 112 } 113 114 static int hibernation_test(int level) 115 { 116 if (pm_test_level == level) { 117 hibernation_debug_sleep(); 118 return 1; 119 } 120 return 0; 121 } 122 #else /* !CONFIG_PM_DEBUG */ 123 static int hibernation_test(int level) { return 0; } 124 #endif /* !CONFIG_PM_DEBUG */ 125 126 /** 127 * platform_begin - Call platform to start hibernation. 128 * @platform_mode: Whether or not to use the platform driver. 129 */ 130 static int platform_begin(int platform_mode) 131 { 132 return (platform_mode && hibernation_ops) ? 133 hibernation_ops->begin() : 0; 134 } 135 136 /** 137 * platform_end - Call platform to finish transition to the working state. 138 * @platform_mode: Whether or not to use the platform driver. 139 */ 140 static void platform_end(int platform_mode) 141 { 142 if (platform_mode && hibernation_ops) 143 hibernation_ops->end(); 144 } 145 146 /** 147 * platform_pre_snapshot - Call platform to prepare the machine for hibernation. 148 * @platform_mode: Whether or not to use the platform driver. 149 * 150 * Use the platform driver to prepare the system for creating a hibernate image, 151 * if so configured, and return an error code if that fails. 152 */ 153 154 static int platform_pre_snapshot(int platform_mode) 155 { 156 return (platform_mode && hibernation_ops) ? 157 hibernation_ops->pre_snapshot() : 0; 158 } 159 160 /** 161 * platform_leave - Call platform to prepare a transition to the working state. 162 * @platform_mode: Whether or not to use the platform driver. 163 * 164 * Use the platform driver prepare to prepare the machine for switching to the 165 * normal mode of operation. 166 * 167 * This routine is called on one CPU with interrupts disabled. 168 */ 169 static void platform_leave(int platform_mode) 170 { 171 if (platform_mode && hibernation_ops) 172 hibernation_ops->leave(); 173 } 174 175 /** 176 * platform_finish - Call platform to switch the system to the working state. 177 * @platform_mode: Whether or not to use the platform driver. 178 * 179 * Use the platform driver to switch the machine to the normal mode of 180 * operation. 181 * 182 * This routine must be called after platform_prepare(). 183 */ 184 static void platform_finish(int platform_mode) 185 { 186 if (platform_mode && hibernation_ops) 187 hibernation_ops->finish(); 188 } 189 190 /** 191 * platform_pre_restore - Prepare for hibernate image restoration. 192 * @platform_mode: Whether or not to use the platform driver. 193 * 194 * Use the platform driver to prepare the system for resume from a hibernation 195 * image. 196 * 197 * If the restore fails after this function has been called, 198 * platform_restore_cleanup() must be called. 199 */ 200 static int platform_pre_restore(int platform_mode) 201 { 202 return (platform_mode && hibernation_ops) ? 203 hibernation_ops->pre_restore() : 0; 204 } 205 206 /** 207 * platform_restore_cleanup - Switch to the working state after failing restore. 208 * @platform_mode: Whether or not to use the platform driver. 209 * 210 * Use the platform driver to switch the system to the normal mode of operation 211 * after a failing restore. 212 * 213 * If platform_pre_restore() has been called before the failing restore, this 214 * function must be called too, regardless of the result of 215 * platform_pre_restore(). 216 */ 217 static void platform_restore_cleanup(int platform_mode) 218 { 219 if (platform_mode && hibernation_ops) 220 hibernation_ops->restore_cleanup(); 221 } 222 223 /** 224 * platform_recover - Recover from a failure to suspend devices. 225 * @platform_mode: Whether or not to use the platform driver. 226 */ 227 static void platform_recover(int platform_mode) 228 { 229 if (platform_mode && hibernation_ops && hibernation_ops->recover) 230 hibernation_ops->recover(); 231 } 232 233 /** 234 * swsusp_show_speed - Print time elapsed between two events during hibernation. 235 * @start: Starting event. 236 * @stop: Final event. 237 * @nr_pages: Number of memory pages processed between @start and @stop. 238 * @msg: Additional diagnostic message to print. 239 */ 240 void swsusp_show_speed(ktime_t start, ktime_t stop, 241 unsigned nr_pages, char *msg) 242 { 243 ktime_t diff; 244 u64 elapsed_centisecs64; 245 unsigned int centisecs; 246 unsigned int k; 247 unsigned int kps; 248 249 diff = ktime_sub(stop, start); 250 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC); 251 centisecs = elapsed_centisecs64; 252 if (centisecs == 0) 253 centisecs = 1; /* avoid div-by-zero */ 254 k = nr_pages * (PAGE_SIZE / 1024); 255 kps = (k * 100) / centisecs; 256 pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n", 257 msg, k, centisecs / 100, centisecs % 100, kps / 1000, 258 (kps % 1000) / 10); 259 } 260 261 /** 262 * create_image - Create a hibernation image. 263 * @platform_mode: Whether or not to use the platform driver. 264 * 265 * Execute device drivers' "late" and "noirq" freeze callbacks, create a 266 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks. 267 * 268 * Control reappears in this routine after the subsequent restore. 269 */ 270 static int create_image(int platform_mode) 271 { 272 int error; 273 274 error = dpm_suspend_end(PMSG_FREEZE); 275 if (error) { 276 pr_err("Some devices failed to power down, aborting hibernation\n"); 277 return error; 278 } 279 280 error = platform_pre_snapshot(platform_mode); 281 if (error || hibernation_test(TEST_PLATFORM)) 282 goto Platform_finish; 283 284 error = disable_nonboot_cpus(); 285 if (error || hibernation_test(TEST_CPUS)) 286 goto Enable_cpus; 287 288 local_irq_disable(); 289 290 system_state = SYSTEM_SUSPEND; 291 292 error = syscore_suspend(); 293 if (error) { 294 pr_err("Some system devices failed to power down, aborting hibernation\n"); 295 goto Enable_irqs; 296 } 297 298 if (hibernation_test(TEST_CORE) || pm_wakeup_pending()) 299 goto Power_up; 300 301 in_suspend = 1; 302 save_processor_state(); 303 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true); 304 error = swsusp_arch_suspend(); 305 /* Restore control flow magically appears here */ 306 restore_processor_state(); 307 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false); 308 if (error) 309 pr_err("Error %d creating hibernation image\n", error); 310 311 if (!in_suspend) { 312 events_check_enabled = false; 313 clear_free_pages(); 314 } 315 316 platform_leave(platform_mode); 317 318 Power_up: 319 syscore_resume(); 320 321 Enable_irqs: 322 system_state = SYSTEM_RUNNING; 323 local_irq_enable(); 324 325 Enable_cpus: 326 enable_nonboot_cpus(); 327 328 Platform_finish: 329 platform_finish(platform_mode); 330 331 dpm_resume_start(in_suspend ? 332 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); 333 334 return error; 335 } 336 337 /** 338 * hibernation_snapshot - Quiesce devices and create a hibernation image. 339 * @platform_mode: If set, use platform driver to prepare for the transition. 340 * 341 * This routine must be called with pm_mutex held. 342 */ 343 int hibernation_snapshot(int platform_mode) 344 { 345 pm_message_t msg; 346 int error; 347 348 pm_suspend_clear_flags(); 349 error = platform_begin(platform_mode); 350 if (error) 351 goto Close; 352 353 /* Preallocate image memory before shutting down devices. */ 354 error = hibernate_preallocate_memory(); 355 if (error) 356 goto Close; 357 358 error = freeze_kernel_threads(); 359 if (error) 360 goto Cleanup; 361 362 if (hibernation_test(TEST_FREEZER)) { 363 364 /* 365 * Indicate to the caller that we are returning due to a 366 * successful freezer test. 367 */ 368 freezer_test_done = true; 369 goto Thaw; 370 } 371 372 error = dpm_prepare(PMSG_FREEZE); 373 if (error) { 374 dpm_complete(PMSG_RECOVER); 375 goto Thaw; 376 } 377 378 suspend_console(); 379 pm_restrict_gfp_mask(); 380 381 error = dpm_suspend(PMSG_FREEZE); 382 383 if (error || hibernation_test(TEST_DEVICES)) 384 platform_recover(platform_mode); 385 else 386 error = create_image(platform_mode); 387 388 /* 389 * In the case that we call create_image() above, the control 390 * returns here (1) after the image has been created or the 391 * image creation has failed and (2) after a successful restore. 392 */ 393 394 /* We may need to release the preallocated image pages here. */ 395 if (error || !in_suspend) 396 swsusp_free(); 397 398 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE; 399 dpm_resume(msg); 400 401 if (error || !in_suspend) 402 pm_restore_gfp_mask(); 403 404 resume_console(); 405 dpm_complete(msg); 406 407 Close: 408 platform_end(platform_mode); 409 return error; 410 411 Thaw: 412 thaw_kernel_threads(); 413 Cleanup: 414 swsusp_free(); 415 goto Close; 416 } 417 418 int __weak hibernate_resume_nonboot_cpu_disable(void) 419 { 420 return disable_nonboot_cpus(); 421 } 422 423 /** 424 * resume_target_kernel - Restore system state from a hibernation image. 425 * @platform_mode: Whether or not to use the platform driver. 426 * 427 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the 428 * contents of highmem that have not been restored yet from the image and run 429 * the low-level code that will restore the remaining contents of memory and 430 * switch to the just restored target kernel. 431 */ 432 static int resume_target_kernel(bool platform_mode) 433 { 434 int error; 435 436 error = dpm_suspend_end(PMSG_QUIESCE); 437 if (error) { 438 pr_err("Some devices failed to power down, aborting resume\n"); 439 return error; 440 } 441 442 error = platform_pre_restore(platform_mode); 443 if (error) 444 goto Cleanup; 445 446 error = hibernate_resume_nonboot_cpu_disable(); 447 if (error) 448 goto Enable_cpus; 449 450 local_irq_disable(); 451 system_state = SYSTEM_SUSPEND; 452 453 error = syscore_suspend(); 454 if (error) 455 goto Enable_irqs; 456 457 save_processor_state(); 458 error = restore_highmem(); 459 if (!error) { 460 error = swsusp_arch_resume(); 461 /* 462 * The code below is only ever reached in case of a failure. 463 * Otherwise, execution continues at the place where 464 * swsusp_arch_suspend() was called. 465 */ 466 BUG_ON(!error); 467 /* 468 * This call to restore_highmem() reverts the changes made by 469 * the previous one. 470 */ 471 restore_highmem(); 472 } 473 /* 474 * The only reason why swsusp_arch_resume() can fail is memory being 475 * very tight, so we have to free it as soon as we can to avoid 476 * subsequent failures. 477 */ 478 swsusp_free(); 479 restore_processor_state(); 480 touch_softlockup_watchdog(); 481 482 syscore_resume(); 483 484 Enable_irqs: 485 system_state = SYSTEM_RUNNING; 486 local_irq_enable(); 487 488 Enable_cpus: 489 enable_nonboot_cpus(); 490 491 Cleanup: 492 platform_restore_cleanup(platform_mode); 493 494 dpm_resume_start(PMSG_RECOVER); 495 496 return error; 497 } 498 499 /** 500 * hibernation_restore - Quiesce devices and restore from a hibernation image. 501 * @platform_mode: If set, use platform driver to prepare for the transition. 502 * 503 * This routine must be called with pm_mutex held. If it is successful, control 504 * reappears in the restored target kernel in hibernation_snapshot(). 505 */ 506 int hibernation_restore(int platform_mode) 507 { 508 int error; 509 510 pm_prepare_console(); 511 suspend_console(); 512 pm_restrict_gfp_mask(); 513 error = dpm_suspend_start(PMSG_QUIESCE); 514 if (!error) { 515 error = resume_target_kernel(platform_mode); 516 /* 517 * The above should either succeed and jump to the new kernel, 518 * or return with an error. Otherwise things are just 519 * undefined, so let's be paranoid. 520 */ 521 BUG_ON(!error); 522 } 523 dpm_resume_end(PMSG_RECOVER); 524 pm_restore_gfp_mask(); 525 resume_console(); 526 pm_restore_console(); 527 return error; 528 } 529 530 /** 531 * hibernation_platform_enter - Power off the system using the platform driver. 532 */ 533 int hibernation_platform_enter(void) 534 { 535 int error; 536 537 if (!hibernation_ops) 538 return -ENOSYS; 539 540 /* 541 * We have cancelled the power transition by running 542 * hibernation_ops->finish() before saving the image, so we should let 543 * the firmware know that we're going to enter the sleep state after all 544 */ 545 error = hibernation_ops->begin(); 546 if (error) 547 goto Close; 548 549 entering_platform_hibernation = true; 550 suspend_console(); 551 error = dpm_suspend_start(PMSG_HIBERNATE); 552 if (error) { 553 if (hibernation_ops->recover) 554 hibernation_ops->recover(); 555 goto Resume_devices; 556 } 557 558 error = dpm_suspend_end(PMSG_HIBERNATE); 559 if (error) 560 goto Resume_devices; 561 562 error = hibernation_ops->prepare(); 563 if (error) 564 goto Platform_finish; 565 566 error = disable_nonboot_cpus(); 567 if (error) 568 goto Enable_cpus; 569 570 local_irq_disable(); 571 system_state = SYSTEM_SUSPEND; 572 syscore_suspend(); 573 if (pm_wakeup_pending()) { 574 error = -EAGAIN; 575 goto Power_up; 576 } 577 578 hibernation_ops->enter(); 579 /* We should never get here */ 580 while (1); 581 582 Power_up: 583 syscore_resume(); 584 system_state = SYSTEM_RUNNING; 585 local_irq_enable(); 586 587 Enable_cpus: 588 enable_nonboot_cpus(); 589 590 Platform_finish: 591 hibernation_ops->finish(); 592 593 dpm_resume_start(PMSG_RESTORE); 594 595 Resume_devices: 596 entering_platform_hibernation = false; 597 dpm_resume_end(PMSG_RESTORE); 598 resume_console(); 599 600 Close: 601 hibernation_ops->end(); 602 603 return error; 604 } 605 606 /** 607 * power_down - Shut the machine down for hibernation. 608 * 609 * Use the platform driver, if configured, to put the system into the sleep 610 * state corresponding to hibernation, or try to power it off or reboot, 611 * depending on the value of hibernation_mode. 612 */ 613 static void power_down(void) 614 { 615 #ifdef CONFIG_SUSPEND 616 int error; 617 618 if (hibernation_mode == HIBERNATION_SUSPEND) { 619 error = suspend_devices_and_enter(PM_SUSPEND_MEM); 620 if (error) { 621 hibernation_mode = hibernation_ops ? 622 HIBERNATION_PLATFORM : 623 HIBERNATION_SHUTDOWN; 624 } else { 625 /* Restore swap signature. */ 626 error = swsusp_unmark(); 627 if (error) 628 pr_err("Swap will be unusable! Try swapon -a.\n"); 629 630 return; 631 } 632 } 633 #endif 634 635 switch (hibernation_mode) { 636 case HIBERNATION_REBOOT: 637 kernel_restart(NULL); 638 break; 639 case HIBERNATION_PLATFORM: 640 hibernation_platform_enter(); 641 case HIBERNATION_SHUTDOWN: 642 if (pm_power_off) 643 kernel_power_off(); 644 break; 645 } 646 kernel_halt(); 647 /* 648 * Valid image is on the disk, if we continue we risk serious data 649 * corruption after resume. 650 */ 651 pr_crit("Power down manually\n"); 652 while (1) 653 cpu_relax(); 654 } 655 656 static int load_image_and_restore(void) 657 { 658 int error; 659 unsigned int flags; 660 661 pm_pr_dbg("Loading hibernation image.\n"); 662 663 lock_device_hotplug(); 664 error = create_basic_memory_bitmaps(); 665 if (error) 666 goto Unlock; 667 668 error = swsusp_read(&flags); 669 swsusp_close(FMODE_READ); 670 if (!error) 671 hibernation_restore(flags & SF_PLATFORM_MODE); 672 673 pr_err("Failed to load hibernation image, recovering.\n"); 674 swsusp_free(); 675 free_basic_memory_bitmaps(); 676 Unlock: 677 unlock_device_hotplug(); 678 679 return error; 680 } 681 682 /** 683 * hibernate - Carry out system hibernation, including saving the image. 684 */ 685 int hibernate(void) 686 { 687 int error, nr_calls = 0; 688 bool snapshot_test = false; 689 690 if (!hibernation_available()) { 691 pm_pr_dbg("Hibernation not available.\n"); 692 return -EPERM; 693 } 694 695 lock_system_sleep(); 696 /* The snapshot device should not be opened while we're running */ 697 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { 698 error = -EBUSY; 699 goto Unlock; 700 } 701 702 pr_info("hibernation entry\n"); 703 pm_prepare_console(); 704 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls); 705 if (error) { 706 nr_calls--; 707 goto Exit; 708 } 709 710 pr_info("Syncing filesystems ... \n"); 711 ksys_sync(); 712 pr_info("done.\n"); 713 714 error = freeze_processes(); 715 if (error) 716 goto Exit; 717 718 lock_device_hotplug(); 719 /* Allocate memory management structures */ 720 error = create_basic_memory_bitmaps(); 721 if (error) 722 goto Thaw; 723 724 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); 725 if (error || freezer_test_done) 726 goto Free_bitmaps; 727 728 if (in_suspend) { 729 unsigned int flags = 0; 730 731 if (hibernation_mode == HIBERNATION_PLATFORM) 732 flags |= SF_PLATFORM_MODE; 733 if (nocompress) 734 flags |= SF_NOCOMPRESS_MODE; 735 else 736 flags |= SF_CRC32_MODE; 737 738 pm_pr_dbg("Writing image.\n"); 739 error = swsusp_write(flags); 740 swsusp_free(); 741 if (!error) { 742 if (hibernation_mode == HIBERNATION_TEST_RESUME) 743 snapshot_test = true; 744 else 745 power_down(); 746 } 747 in_suspend = 0; 748 pm_restore_gfp_mask(); 749 } else { 750 pm_pr_dbg("Image restored successfully.\n"); 751 } 752 753 Free_bitmaps: 754 free_basic_memory_bitmaps(); 755 Thaw: 756 unlock_device_hotplug(); 757 if (snapshot_test) { 758 pm_pr_dbg("Checking hibernation image\n"); 759 error = swsusp_check(); 760 if (!error) 761 error = load_image_and_restore(); 762 } 763 thaw_processes(); 764 765 /* Don't bother checking whether freezer_test_done is true */ 766 freezer_test_done = false; 767 Exit: 768 __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL); 769 pm_restore_console(); 770 atomic_inc(&snapshot_device_available); 771 Unlock: 772 unlock_system_sleep(); 773 pr_info("hibernation exit\n"); 774 775 return error; 776 } 777 778 779 /** 780 * software_resume - Resume from a saved hibernation image. 781 * 782 * This routine is called as a late initcall, when all devices have been 783 * discovered and initialized already. 784 * 785 * The image reading code is called to see if there is a hibernation image 786 * available for reading. If that is the case, devices are quiesced and the 787 * contents of memory is restored from the saved image. 788 * 789 * If this is successful, control reappears in the restored target kernel in 790 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine 791 * attempts to recover gracefully and make the kernel return to the normal mode 792 * of operation. 793 */ 794 static int software_resume(void) 795 { 796 int error, nr_calls = 0; 797 798 /* 799 * If the user said "noresume".. bail out early. 800 */ 801 if (noresume || !hibernation_available()) 802 return 0; 803 804 /* 805 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs 806 * is configured into the kernel. Since the regular hibernate 807 * trigger path is via sysfs which takes a buffer mutex before 808 * calling hibernate functions (which take pm_mutex) this can 809 * cause lockdep to complain about a possible ABBA deadlock 810 * which cannot happen since we're in the boot code here and 811 * sysfs can't be invoked yet. Therefore, we use a subclass 812 * here to avoid lockdep complaining. 813 */ 814 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING); 815 816 if (swsusp_resume_device) 817 goto Check_image; 818 819 if (!strlen(resume_file)) { 820 error = -ENOENT; 821 goto Unlock; 822 } 823 824 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file); 825 826 if (resume_delay) { 827 pr_info("Waiting %dsec before reading resume device ...\n", 828 resume_delay); 829 ssleep(resume_delay); 830 } 831 832 /* Check if the device is there */ 833 swsusp_resume_device = name_to_dev_t(resume_file); 834 835 /* 836 * name_to_dev_t is ineffective to verify parition if resume_file is in 837 * integer format. (e.g. major:minor) 838 */ 839 if (isdigit(resume_file[0]) && resume_wait) { 840 int partno; 841 while (!get_gendisk(swsusp_resume_device, &partno)) 842 msleep(10); 843 } 844 845 if (!swsusp_resume_device) { 846 /* 847 * Some device discovery might still be in progress; we need 848 * to wait for this to finish. 849 */ 850 wait_for_device_probe(); 851 852 if (resume_wait) { 853 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0) 854 msleep(10); 855 async_synchronize_full(); 856 } 857 858 swsusp_resume_device = name_to_dev_t(resume_file); 859 if (!swsusp_resume_device) { 860 error = -ENODEV; 861 goto Unlock; 862 } 863 } 864 865 Check_image: 866 pm_pr_dbg("Hibernation image partition %d:%d present\n", 867 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); 868 869 pm_pr_dbg("Looking for hibernation image.\n"); 870 error = swsusp_check(); 871 if (error) 872 goto Unlock; 873 874 /* The snapshot device should not be opened while we're running */ 875 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { 876 error = -EBUSY; 877 swsusp_close(FMODE_READ); 878 goto Unlock; 879 } 880 881 pr_info("resume from hibernation\n"); 882 pm_prepare_console(); 883 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls); 884 if (error) { 885 nr_calls--; 886 goto Close_Finish; 887 } 888 889 pm_pr_dbg("Preparing processes for restore.\n"); 890 error = freeze_processes(); 891 if (error) 892 goto Close_Finish; 893 error = load_image_and_restore(); 894 thaw_processes(); 895 Finish: 896 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL); 897 pm_restore_console(); 898 pr_info("resume from hibernation failed (%d)\n", error); 899 atomic_inc(&snapshot_device_available); 900 /* For success case, the suspend path will release the lock */ 901 Unlock: 902 mutex_unlock(&pm_mutex); 903 pm_pr_dbg("Hibernation image not present or could not be loaded.\n"); 904 return error; 905 Close_Finish: 906 swsusp_close(FMODE_READ); 907 goto Finish; 908 } 909 910 late_initcall_sync(software_resume); 911 912 913 static const char * const hibernation_modes[] = { 914 [HIBERNATION_PLATFORM] = "platform", 915 [HIBERNATION_SHUTDOWN] = "shutdown", 916 [HIBERNATION_REBOOT] = "reboot", 917 #ifdef CONFIG_SUSPEND 918 [HIBERNATION_SUSPEND] = "suspend", 919 #endif 920 [HIBERNATION_TEST_RESUME] = "test_resume", 921 }; 922 923 /* 924 * /sys/power/disk - Control hibernation mode. 925 * 926 * Hibernation can be handled in several ways. There are a few different ways 927 * to put the system into the sleep state: using the platform driver (e.g. ACPI 928 * or other hibernation_ops), powering it off or rebooting it (for testing 929 * mostly). 930 * 931 * The sysfs file /sys/power/disk provides an interface for selecting the 932 * hibernation mode to use. Reading from this file causes the available modes 933 * to be printed. There are 3 modes that can be supported: 934 * 935 * 'platform' 936 * 'shutdown' 937 * 'reboot' 938 * 939 * If a platform hibernation driver is in use, 'platform' will be supported 940 * and will be used by default. Otherwise, 'shutdown' will be used by default. 941 * The selected option (i.e. the one corresponding to the current value of 942 * hibernation_mode) is enclosed by a square bracket. 943 * 944 * To select a given hibernation mode it is necessary to write the mode's 945 * string representation (as returned by reading from /sys/power/disk) back 946 * into /sys/power/disk. 947 */ 948 949 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr, 950 char *buf) 951 { 952 int i; 953 char *start = buf; 954 955 if (!hibernation_available()) 956 return sprintf(buf, "[disabled]\n"); 957 958 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { 959 if (!hibernation_modes[i]) 960 continue; 961 switch (i) { 962 case HIBERNATION_SHUTDOWN: 963 case HIBERNATION_REBOOT: 964 #ifdef CONFIG_SUSPEND 965 case HIBERNATION_SUSPEND: 966 #endif 967 case HIBERNATION_TEST_RESUME: 968 break; 969 case HIBERNATION_PLATFORM: 970 if (hibernation_ops) 971 break; 972 /* not a valid mode, continue with loop */ 973 continue; 974 } 975 if (i == hibernation_mode) 976 buf += sprintf(buf, "[%s] ", hibernation_modes[i]); 977 else 978 buf += sprintf(buf, "%s ", hibernation_modes[i]); 979 } 980 buf += sprintf(buf, "\n"); 981 return buf-start; 982 } 983 984 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, 985 const char *buf, size_t n) 986 { 987 int error = 0; 988 int i; 989 int len; 990 char *p; 991 int mode = HIBERNATION_INVALID; 992 993 if (!hibernation_available()) 994 return -EPERM; 995 996 p = memchr(buf, '\n', n); 997 len = p ? p - buf : n; 998 999 lock_system_sleep(); 1000 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { 1001 if (len == strlen(hibernation_modes[i]) 1002 && !strncmp(buf, hibernation_modes[i], len)) { 1003 mode = i; 1004 break; 1005 } 1006 } 1007 if (mode != HIBERNATION_INVALID) { 1008 switch (mode) { 1009 case HIBERNATION_SHUTDOWN: 1010 case HIBERNATION_REBOOT: 1011 #ifdef CONFIG_SUSPEND 1012 case HIBERNATION_SUSPEND: 1013 #endif 1014 case HIBERNATION_TEST_RESUME: 1015 hibernation_mode = mode; 1016 break; 1017 case HIBERNATION_PLATFORM: 1018 if (hibernation_ops) 1019 hibernation_mode = mode; 1020 else 1021 error = -EINVAL; 1022 } 1023 } else 1024 error = -EINVAL; 1025 1026 if (!error) 1027 pm_pr_dbg("Hibernation mode set to '%s'\n", 1028 hibernation_modes[mode]); 1029 unlock_system_sleep(); 1030 return error ? error : n; 1031 } 1032 1033 power_attr(disk); 1034 1035 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr, 1036 char *buf) 1037 { 1038 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), 1039 MINOR(swsusp_resume_device)); 1040 } 1041 1042 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr, 1043 const char *buf, size_t n) 1044 { 1045 dev_t res; 1046 int len = n; 1047 char *name; 1048 1049 if (len && buf[len-1] == '\n') 1050 len--; 1051 name = kstrndup(buf, len, GFP_KERNEL); 1052 if (!name) 1053 return -ENOMEM; 1054 1055 res = name_to_dev_t(name); 1056 kfree(name); 1057 if (!res) 1058 return -EINVAL; 1059 1060 lock_system_sleep(); 1061 swsusp_resume_device = res; 1062 unlock_system_sleep(); 1063 pm_pr_dbg("Configured resume from disk to %u\n", swsusp_resume_device); 1064 noresume = 0; 1065 software_resume(); 1066 return n; 1067 } 1068 1069 power_attr(resume); 1070 1071 static ssize_t resume_offset_show(struct kobject *kobj, 1072 struct kobj_attribute *attr, char *buf) 1073 { 1074 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block); 1075 } 1076 1077 static ssize_t resume_offset_store(struct kobject *kobj, 1078 struct kobj_attribute *attr, const char *buf, 1079 size_t n) 1080 { 1081 unsigned long long offset; 1082 int rc; 1083 1084 rc = kstrtoull(buf, 0, &offset); 1085 if (rc) 1086 return rc; 1087 swsusp_resume_block = offset; 1088 1089 return n; 1090 } 1091 1092 power_attr(resume_offset); 1093 1094 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr, 1095 char *buf) 1096 { 1097 return sprintf(buf, "%lu\n", image_size); 1098 } 1099 1100 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr, 1101 const char *buf, size_t n) 1102 { 1103 unsigned long size; 1104 1105 if (sscanf(buf, "%lu", &size) == 1) { 1106 image_size = size; 1107 return n; 1108 } 1109 1110 return -EINVAL; 1111 } 1112 1113 power_attr(image_size); 1114 1115 static ssize_t reserved_size_show(struct kobject *kobj, 1116 struct kobj_attribute *attr, char *buf) 1117 { 1118 return sprintf(buf, "%lu\n", reserved_size); 1119 } 1120 1121 static ssize_t reserved_size_store(struct kobject *kobj, 1122 struct kobj_attribute *attr, 1123 const char *buf, size_t n) 1124 { 1125 unsigned long size; 1126 1127 if (sscanf(buf, "%lu", &size) == 1) { 1128 reserved_size = size; 1129 return n; 1130 } 1131 1132 return -EINVAL; 1133 } 1134 1135 power_attr(reserved_size); 1136 1137 static struct attribute * g[] = { 1138 &disk_attr.attr, 1139 &resume_offset_attr.attr, 1140 &resume_attr.attr, 1141 &image_size_attr.attr, 1142 &reserved_size_attr.attr, 1143 NULL, 1144 }; 1145 1146 1147 static const struct attribute_group attr_group = { 1148 .attrs = g, 1149 }; 1150 1151 1152 static int __init pm_disk_init(void) 1153 { 1154 return sysfs_create_group(power_kobj, &attr_group); 1155 } 1156 1157 core_initcall(pm_disk_init); 1158 1159 1160 static int __init resume_setup(char *str) 1161 { 1162 if (noresume) 1163 return 1; 1164 1165 strncpy( resume_file, str, 255 ); 1166 return 1; 1167 } 1168 1169 static int __init resume_offset_setup(char *str) 1170 { 1171 unsigned long long offset; 1172 1173 if (noresume) 1174 return 1; 1175 1176 if (sscanf(str, "%llu", &offset) == 1) 1177 swsusp_resume_block = offset; 1178 1179 return 1; 1180 } 1181 1182 static int __init hibernate_setup(char *str) 1183 { 1184 if (!strncmp(str, "noresume", 8)) { 1185 noresume = 1; 1186 } else if (!strncmp(str, "nocompress", 10)) { 1187 nocompress = 1; 1188 } else if (!strncmp(str, "no", 2)) { 1189 noresume = 1; 1190 nohibernate = 1; 1191 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) 1192 && !strncmp(str, "protect_image", 13)) { 1193 enable_restore_image_protection(); 1194 } 1195 return 1; 1196 } 1197 1198 static int __init noresume_setup(char *str) 1199 { 1200 noresume = 1; 1201 return 1; 1202 } 1203 1204 static int __init resumewait_setup(char *str) 1205 { 1206 resume_wait = 1; 1207 return 1; 1208 } 1209 1210 static int __init resumedelay_setup(char *str) 1211 { 1212 int rc = kstrtouint(str, 0, &resume_delay); 1213 1214 if (rc) 1215 return rc; 1216 return 1; 1217 } 1218 1219 static int __init nohibernate_setup(char *str) 1220 { 1221 noresume = 1; 1222 nohibernate = 1; 1223 return 1; 1224 } 1225 1226 __setup("noresume", noresume_setup); 1227 __setup("resume_offset=", resume_offset_setup); 1228 __setup("resume=", resume_setup); 1229 __setup("hibernate=", hibernate_setup); 1230 __setup("resumewait", resumewait_setup); 1231 __setup("resumedelay=", resumedelay_setup); 1232 __setup("nohibernate", nohibernate_setup); 1233