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