1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support. 4 * 5 * Copyright (c) 2003 Patrick Mochel 6 * Copyright (c) 2003 Open Source Development Lab 7 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz> 8 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc. 9 * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com> 10 */ 11 12 #define pr_fmt(fmt) "PM: hibernation: " fmt 13 14 #include <crypto/acompress.h> 15 #include <linux/blkdev.h> 16 #include <linux/export.h> 17 #include <linux/suspend.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/ktime.h> 34 #include <linux/security.h> 35 #include <linux/secretmem.h> 36 #include <trace/events/power.h> 37 38 #include "power.h" 39 40 41 static int nocompress; 42 static int noresume; 43 static int nohibernate; 44 static int resume_wait; 45 static unsigned int resume_delay; 46 static char resume_file[256] = CONFIG_PM_STD_PARTITION; 47 dev_t swsusp_resume_device; 48 sector_t swsusp_resume_block; 49 __visible int in_suspend __nosavedata; 50 51 static char hibernate_compressor[CRYPTO_MAX_ALG_NAME] = CONFIG_HIBERNATION_DEF_COMP; 52 53 /* 54 * Compression/decompression algorithm to be used while saving/loading 55 * image to/from disk. This would later be used in 'kernel/power/swap.c' 56 * to allocate comp streams. 57 */ 58 char hib_comp_algo[CRYPTO_MAX_ALG_NAME]; 59 60 enum { 61 HIBERNATION_INVALID, 62 HIBERNATION_PLATFORM, 63 HIBERNATION_SHUTDOWN, 64 HIBERNATION_REBOOT, 65 #ifdef CONFIG_SUSPEND 66 HIBERNATION_SUSPEND, 67 #endif 68 HIBERNATION_TEST_RESUME, 69 /* keep last */ 70 __HIBERNATION_AFTER_LAST 71 }; 72 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1) 73 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1) 74 75 static int hibernation_mode = HIBERNATION_SHUTDOWN; 76 77 bool freezer_test_done; 78 79 static const struct platform_hibernation_ops *hibernation_ops; 80 81 static atomic_t hibernate_atomic = ATOMIC_INIT(1); 82 83 #ifdef CONFIG_SUSPEND 84 /** 85 * pm_hibernation_mode_is_suspend - Check if hibernation has been set to suspend 86 */ 87 bool pm_hibernation_mode_is_suspend(void) 88 { 89 return hibernation_mode == HIBERNATION_SUSPEND; 90 } 91 EXPORT_SYMBOL_GPL(pm_hibernation_mode_is_suspend); 92 #endif 93 94 bool hibernate_acquire(void) 95 { 96 return atomic_add_unless(&hibernate_atomic, -1, 0); 97 } 98 99 void hibernate_release(void) 100 { 101 atomic_inc(&hibernate_atomic); 102 } 103 104 bool hibernation_in_progress(void) 105 { 106 return !atomic_read(&hibernate_atomic); 107 } 108 109 bool hibernation_available(void) 110 { 111 return nohibernate == 0 && 112 !security_locked_down(LOCKDOWN_HIBERNATION) && 113 !secretmem_active() && !cxl_mem_active(); 114 } 115 116 /** 117 * hibernation_set_ops - Set the global hibernate operations. 118 * @ops: Hibernation operations to use in subsequent hibernation transitions. 119 */ 120 void hibernation_set_ops(const struct platform_hibernation_ops *ops) 121 { 122 unsigned int sleep_flags; 123 124 if (ops && !(ops->begin && ops->end && ops->pre_snapshot 125 && ops->prepare && ops->finish && ops->enter && ops->pre_restore 126 && ops->restore_cleanup && ops->leave)) { 127 WARN_ON(1); 128 return; 129 } 130 131 sleep_flags = lock_system_sleep(); 132 133 hibernation_ops = ops; 134 if (ops) 135 hibernation_mode = HIBERNATION_PLATFORM; 136 else if (hibernation_mode == HIBERNATION_PLATFORM) 137 hibernation_mode = HIBERNATION_SHUTDOWN; 138 139 unlock_system_sleep(sleep_flags); 140 } 141 EXPORT_SYMBOL_GPL(hibernation_set_ops); 142 143 static bool entering_platform_hibernation; 144 145 bool system_entering_hibernation(void) 146 { 147 return entering_platform_hibernation; 148 } 149 EXPORT_SYMBOL(system_entering_hibernation); 150 151 #ifdef CONFIG_PM_DEBUG 152 static unsigned int pm_test_delay = 5; 153 module_param(pm_test_delay, uint, 0644); 154 MODULE_PARM_DESC(pm_test_delay, 155 "Number of seconds to wait before resuming from hibernation test"); 156 static void hibernation_debug_sleep(void) 157 { 158 pr_info("hibernation debug: Waiting for %d second(s).\n", 159 pm_test_delay); 160 mdelay(pm_test_delay * 1000); 161 } 162 163 static int hibernation_test(int level) 164 { 165 if (pm_test_level == level) { 166 hibernation_debug_sleep(); 167 return 1; 168 } 169 return 0; 170 } 171 #else /* !CONFIG_PM_DEBUG */ 172 static int hibernation_test(int level) { return 0; } 173 #endif /* !CONFIG_PM_DEBUG */ 174 175 /** 176 * platform_begin - Call platform to start hibernation. 177 * @platform_mode: Whether or not to use the platform driver. 178 */ 179 static int platform_begin(int platform_mode) 180 { 181 return (platform_mode && hibernation_ops) ? 182 hibernation_ops->begin(PMSG_FREEZE) : 0; 183 } 184 185 /** 186 * platform_end - Call platform to finish transition to the working state. 187 * @platform_mode: Whether or not to use the platform driver. 188 */ 189 static void platform_end(int platform_mode) 190 { 191 if (platform_mode && hibernation_ops) 192 hibernation_ops->end(); 193 } 194 195 /** 196 * platform_pre_snapshot - Call platform to prepare the machine for hibernation. 197 * @platform_mode: Whether or not to use the platform driver. 198 * 199 * Use the platform driver to prepare the system for creating a hibernate image, 200 * if so configured, and return an error code if that fails. 201 */ 202 203 static int platform_pre_snapshot(int platform_mode) 204 { 205 return (platform_mode && hibernation_ops) ? 206 hibernation_ops->pre_snapshot() : 0; 207 } 208 209 /** 210 * platform_leave - Call platform to prepare a transition to the working state. 211 * @platform_mode: Whether or not to use the platform driver. 212 * 213 * Use the platform driver prepare to prepare the machine for switching to the 214 * normal mode of operation. 215 * 216 * This routine is called on one CPU with interrupts disabled. 217 */ 218 static void platform_leave(int platform_mode) 219 { 220 if (platform_mode && hibernation_ops) 221 hibernation_ops->leave(); 222 } 223 224 /** 225 * platform_finish - Call platform to switch the system to the working state. 226 * @platform_mode: Whether or not to use the platform driver. 227 * 228 * Use the platform driver to switch the machine to the normal mode of 229 * operation. 230 * 231 * This routine must be called after platform_prepare(). 232 */ 233 static void platform_finish(int platform_mode) 234 { 235 if (platform_mode && hibernation_ops) 236 hibernation_ops->finish(); 237 } 238 239 /** 240 * platform_pre_restore - Prepare for hibernate image restoration. 241 * @platform_mode: Whether or not to use the platform driver. 242 * 243 * Use the platform driver to prepare the system for resume from a hibernation 244 * image. 245 * 246 * If the restore fails after this function has been called, 247 * platform_restore_cleanup() must be called. 248 */ 249 static int platform_pre_restore(int platform_mode) 250 { 251 return (platform_mode && hibernation_ops) ? 252 hibernation_ops->pre_restore() : 0; 253 } 254 255 /** 256 * platform_restore_cleanup - Switch to the working state after failing restore. 257 * @platform_mode: Whether or not to use the platform driver. 258 * 259 * Use the platform driver to switch the system to the normal mode of operation 260 * after a failing restore. 261 * 262 * If platform_pre_restore() has been called before the failing restore, this 263 * function must be called too, regardless of the result of 264 * platform_pre_restore(). 265 */ 266 static void platform_restore_cleanup(int platform_mode) 267 { 268 if (platform_mode && hibernation_ops) 269 hibernation_ops->restore_cleanup(); 270 } 271 272 /** 273 * platform_recover - Recover from a failure to suspend devices. 274 * @platform_mode: Whether or not to use the platform driver. 275 */ 276 static void platform_recover(int platform_mode) 277 { 278 if (platform_mode && hibernation_ops && hibernation_ops->recover) 279 hibernation_ops->recover(); 280 } 281 282 /** 283 * swsusp_show_speed - Print time elapsed between two events during hibernation. 284 * @start: Starting event. 285 * @stop: Final event. 286 * @nr_pages: Number of memory pages processed between @start and @stop. 287 * @msg: Additional diagnostic message to print. 288 */ 289 void swsusp_show_speed(ktime_t start, ktime_t stop, 290 unsigned nr_pages, char *msg) 291 { 292 ktime_t diff; 293 u64 elapsed_centisecs64; 294 unsigned int centisecs; 295 unsigned int k; 296 unsigned int kps; 297 298 diff = ktime_sub(stop, start); 299 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC); 300 centisecs = elapsed_centisecs64; 301 if (centisecs == 0) 302 centisecs = 1; /* avoid div-by-zero */ 303 k = nr_pages * (PAGE_SIZE / 1024); 304 kps = (k * 100) / centisecs; 305 pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n", 306 msg, k, centisecs / 100, centisecs % 100, kps / 1000, 307 (kps % 1000) / 10); 308 } 309 310 __weak int arch_resume_nosmt(void) 311 { 312 return 0; 313 } 314 315 /** 316 * create_image - Create a hibernation image. 317 * @platform_mode: Whether or not to use the platform driver. 318 * 319 * Execute device drivers' "late" and "noirq" freeze callbacks, create a 320 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks. 321 * 322 * Control reappears in this routine after the subsequent restore. 323 */ 324 static int create_image(int platform_mode) 325 { 326 int error; 327 328 error = dpm_suspend_end(PMSG_FREEZE); 329 if (error) { 330 pr_err("Some devices failed to power down, aborting\n"); 331 return error; 332 } 333 334 error = platform_pre_snapshot(platform_mode); 335 if (error || hibernation_test(TEST_PLATFORM)) 336 goto Platform_finish; 337 338 error = pm_sleep_disable_secondary_cpus(); 339 if (error || hibernation_test(TEST_CPUS)) 340 goto Enable_cpus; 341 342 local_irq_disable(); 343 344 system_state = SYSTEM_SUSPEND; 345 346 error = syscore_suspend(); 347 if (error) { 348 pr_err("Some system devices failed to power down, aborting\n"); 349 goto Enable_irqs; 350 } 351 352 if (hibernation_test(TEST_CORE) || pm_wakeup_pending()) 353 goto Power_up; 354 355 in_suspend = 1; 356 save_processor_state(); 357 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true); 358 error = swsusp_arch_suspend(); 359 /* Restore control flow magically appears here */ 360 restore_processor_state(); 361 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false); 362 if (error) 363 pr_err("Error %d creating image\n", error); 364 365 if (!in_suspend) { 366 events_check_enabled = false; 367 clear_or_poison_free_pages(); 368 } 369 370 platform_leave(platform_mode); 371 372 Power_up: 373 syscore_resume(); 374 375 Enable_irqs: 376 system_state = SYSTEM_RUNNING; 377 local_irq_enable(); 378 379 Enable_cpus: 380 pm_sleep_enable_secondary_cpus(); 381 382 /* Allow architectures to do nosmt-specific post-resume dances */ 383 if (!in_suspend) 384 error = arch_resume_nosmt(); 385 386 Platform_finish: 387 platform_finish(platform_mode); 388 389 dpm_resume_start(in_suspend ? 390 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); 391 392 return error; 393 } 394 395 /** 396 * hibernation_snapshot - Quiesce devices and create a hibernation image. 397 * @platform_mode: If set, use platform driver to prepare for the transition. 398 * 399 * This routine must be called with system_transition_mutex held. 400 */ 401 int hibernation_snapshot(int platform_mode) 402 { 403 pm_message_t msg; 404 int error; 405 406 pm_suspend_clear_flags(); 407 error = platform_begin(platform_mode); 408 if (error) 409 goto Close; 410 411 error = freeze_kernel_threads(); 412 if (error) 413 goto Close; 414 415 if (hibernation_test(TEST_FREEZER)) { 416 417 /* 418 * Indicate to the caller that we are returning due to a 419 * successful freezer test. 420 */ 421 freezer_test_done = true; 422 goto Thaw; 423 } 424 425 error = dpm_prepare(PMSG_FREEZE); 426 if (error) 427 goto Complete; 428 429 /* Preallocate image memory before shutting down devices. */ 430 error = hibernate_preallocate_memory(); 431 if (error) 432 goto Complete; 433 434 console_suspend_all(); 435 pm_restrict_gfp_mask(); 436 437 error = dpm_suspend(PMSG_FREEZE); 438 439 if (error || hibernation_test(TEST_DEVICES)) 440 platform_recover(platform_mode); 441 else 442 error = create_image(platform_mode); 443 444 /* 445 * In the case that we call create_image() above, the control 446 * returns here (1) after the image has been created or the 447 * image creation has failed and (2) after a successful restore. 448 */ 449 450 /* We may need to release the preallocated image pages here. */ 451 if (error || !in_suspend) 452 swsusp_free(); 453 454 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE; 455 dpm_resume(msg); 456 457 if (error || !in_suspend) 458 pm_restore_gfp_mask(); 459 460 console_resume_all(); 461 dpm_complete(msg); 462 463 Close: 464 platform_end(platform_mode); 465 return error; 466 467 Complete: 468 dpm_complete(PMSG_RECOVER); 469 Thaw: 470 thaw_kernel_threads(); 471 goto Close; 472 } 473 474 int __weak hibernate_resume_nonboot_cpu_disable(void) 475 { 476 return suspend_disable_secondary_cpus(); 477 } 478 479 /** 480 * resume_target_kernel - Restore system state from a hibernation image. 481 * @platform_mode: Whether or not to use the platform driver. 482 * 483 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the 484 * contents of highmem that have not been restored yet from the image and run 485 * the low-level code that will restore the remaining contents of memory and 486 * switch to the just restored target kernel. 487 */ 488 static int resume_target_kernel(bool platform_mode) 489 { 490 int error; 491 492 error = dpm_suspend_end(PMSG_QUIESCE); 493 if (error) { 494 pr_err("Some devices failed to power down, aborting resume\n"); 495 return error; 496 } 497 498 error = platform_pre_restore(platform_mode); 499 if (error) 500 goto Cleanup; 501 502 cpuidle_pause(); 503 504 error = hibernate_resume_nonboot_cpu_disable(); 505 if (error) 506 goto Enable_cpus; 507 508 local_irq_disable(); 509 system_state = SYSTEM_SUSPEND; 510 511 error = syscore_suspend(); 512 if (error) 513 goto Enable_irqs; 514 515 save_processor_state(); 516 error = restore_highmem(); 517 if (!error) { 518 error = swsusp_arch_resume(); 519 /* 520 * The code below is only ever reached in case of a failure. 521 * Otherwise, execution continues at the place where 522 * swsusp_arch_suspend() was called. 523 */ 524 BUG_ON(!error); 525 /* 526 * This call to restore_highmem() reverts the changes made by 527 * the previous one. 528 */ 529 restore_highmem(); 530 } 531 /* 532 * The only reason why swsusp_arch_resume() can fail is memory being 533 * very tight, so we have to free it as soon as we can to avoid 534 * subsequent failures. 535 */ 536 swsusp_free(); 537 restore_processor_state(); 538 touch_softlockup_watchdog(); 539 540 syscore_resume(); 541 542 Enable_irqs: 543 system_state = SYSTEM_RUNNING; 544 local_irq_enable(); 545 546 Enable_cpus: 547 pm_sleep_enable_secondary_cpus(); 548 549 Cleanup: 550 platform_restore_cleanup(platform_mode); 551 552 dpm_resume_start(PMSG_RECOVER); 553 554 return error; 555 } 556 557 /** 558 * hibernation_restore - Quiesce devices and restore from a hibernation image. 559 * @platform_mode: If set, use platform driver to prepare for the transition. 560 * 561 * This routine must be called with system_transition_mutex held. If it is 562 * successful, control reappears in the restored target kernel in 563 * hibernation_snapshot(). 564 */ 565 int hibernation_restore(int platform_mode) 566 { 567 int error; 568 569 pm_prepare_console(); 570 console_suspend_all(); 571 error = dpm_suspend_start(PMSG_QUIESCE); 572 if (!error) { 573 error = resume_target_kernel(platform_mode); 574 /* 575 * The above should either succeed and jump to the new kernel, 576 * or return with an error. Otherwise things are just 577 * undefined, so let's be paranoid. 578 */ 579 BUG_ON(!error); 580 } 581 dpm_resume_end(PMSG_RECOVER); 582 console_resume_all(); 583 pm_restore_console(); 584 return error; 585 } 586 587 /** 588 * hibernation_platform_enter - Power off the system using the platform driver. 589 */ 590 int hibernation_platform_enter(void) 591 { 592 int error; 593 594 if (!hibernation_ops) 595 return -ENOSYS; 596 597 /* 598 * We have cancelled the power transition by running 599 * hibernation_ops->finish() before saving the image, so we should let 600 * the firmware know that we're going to enter the sleep state after all 601 */ 602 error = hibernation_ops->begin(PMSG_HIBERNATE); 603 if (error) 604 goto Close; 605 606 entering_platform_hibernation = true; 607 console_suspend_all(); 608 error = dpm_suspend_start(PMSG_HIBERNATE); 609 if (error) { 610 if (hibernation_ops->recover) 611 hibernation_ops->recover(); 612 goto Resume_devices; 613 } 614 615 error = dpm_suspend_end(PMSG_HIBERNATE); 616 if (error) 617 goto Resume_devices; 618 619 error = hibernation_ops->prepare(); 620 if (error) 621 goto Platform_finish; 622 623 error = pm_sleep_disable_secondary_cpus(); 624 if (error) 625 goto Enable_cpus; 626 627 local_irq_disable(); 628 system_state = SYSTEM_SUSPEND; 629 630 error = syscore_suspend(); 631 if (error) 632 goto Enable_irqs; 633 634 if (pm_wakeup_pending()) { 635 error = -EAGAIN; 636 goto Power_up; 637 } 638 639 hibernation_ops->enter(); 640 /* We should never get here */ 641 while (1); 642 643 Power_up: 644 syscore_resume(); 645 Enable_irqs: 646 system_state = SYSTEM_RUNNING; 647 local_irq_enable(); 648 649 Enable_cpus: 650 pm_sleep_enable_secondary_cpus(); 651 652 Platform_finish: 653 hibernation_ops->finish(); 654 655 dpm_resume_start(PMSG_RESTORE); 656 657 Resume_devices: 658 entering_platform_hibernation = false; 659 dpm_resume_end(PMSG_RESTORE); 660 console_resume_all(); 661 662 Close: 663 hibernation_ops->end(); 664 665 return error; 666 } 667 668 /** 669 * power_down - Shut the machine down for hibernation. 670 * 671 * Use the platform driver, if configured, to put the system into the sleep 672 * state corresponding to hibernation, or try to power it off or reboot, 673 * depending on the value of hibernation_mode. 674 */ 675 static void power_down(void) 676 { 677 int error; 678 679 #ifdef CONFIG_SUSPEND 680 if (hibernation_mode == HIBERNATION_SUSPEND) { 681 error = suspend_devices_and_enter(mem_sleep_current); 682 if (!error) 683 goto exit; 684 685 hibernation_mode = hibernation_ops ? HIBERNATION_PLATFORM : 686 HIBERNATION_SHUTDOWN; 687 } 688 #endif 689 690 switch (hibernation_mode) { 691 case HIBERNATION_REBOOT: 692 kernel_restart(NULL); 693 break; 694 case HIBERNATION_PLATFORM: 695 error = hibernation_platform_enter(); 696 if (error == -EAGAIN || error == -EBUSY) { 697 events_check_enabled = false; 698 pr_info("Wakeup event detected during hibernation, rolling back.\n"); 699 goto exit; 700 } 701 fallthrough; 702 case HIBERNATION_SHUTDOWN: 703 if (kernel_can_power_off()) { 704 entering_platform_hibernation = true; 705 kernel_power_off(); 706 entering_platform_hibernation = false; 707 } 708 break; 709 } 710 kernel_halt(); 711 /* 712 * Valid image is on the disk, if we continue we risk serious data 713 * corruption after resume. 714 */ 715 pr_crit("Power down manually\n"); 716 while (1) 717 cpu_relax(); 718 719 exit: 720 /* Restore swap signature. */ 721 error = swsusp_unmark(); 722 if (error) 723 pr_err("Swap will be unusable! Try swapon -a.\n"); 724 } 725 726 static int load_image_and_restore(void) 727 { 728 int error; 729 unsigned int flags; 730 731 pm_pr_dbg("Loading hibernation image.\n"); 732 733 lock_device_hotplug(); 734 error = create_basic_memory_bitmaps(); 735 if (error) { 736 swsusp_close(); 737 goto Unlock; 738 } 739 740 error = swsusp_read(&flags); 741 swsusp_close(); 742 if (!error) 743 error = hibernation_restore(flags & SF_PLATFORM_MODE); 744 745 pr_err("Failed to load image, recovering.\n"); 746 swsusp_free(); 747 free_basic_memory_bitmaps(); 748 Unlock: 749 unlock_device_hotplug(); 750 751 return error; 752 } 753 754 #define COMPRESSION_ALGO_LZO "lzo" 755 #define COMPRESSION_ALGO_LZ4 "lz4" 756 757 /** 758 * hibernate - Carry out system hibernation, including saving the image. 759 */ 760 int hibernate(void) 761 { 762 bool snapshot_test = false; 763 unsigned int sleep_flags; 764 int error; 765 766 if (!hibernation_available()) { 767 pm_pr_dbg("Hibernation not available.\n"); 768 return -EPERM; 769 } 770 771 /* 772 * Query for the compression algorithm support if compression is enabled. 773 */ 774 if (!nocompress) { 775 strscpy(hib_comp_algo, hibernate_compressor); 776 if (!crypto_has_acomp(hib_comp_algo, 0, CRYPTO_ALG_ASYNC)) { 777 pr_err("%s compression is not available\n", hib_comp_algo); 778 return -EOPNOTSUPP; 779 } 780 } 781 782 sleep_flags = lock_system_sleep(); 783 /* The snapshot device should not be opened while we're running */ 784 if (!hibernate_acquire()) { 785 error = -EBUSY; 786 goto Unlock; 787 } 788 789 pr_info("hibernation entry\n"); 790 pm_prepare_console(); 791 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION); 792 if (error) 793 goto Restore; 794 795 error = pm_sleep_fs_sync(); 796 if (error) 797 goto Notify; 798 799 filesystems_freeze(filesystem_freeze_enabled); 800 801 error = freeze_processes(); 802 if (error) 803 goto Exit; 804 805 lock_device_hotplug(); 806 /* Allocate memory management structures */ 807 error = create_basic_memory_bitmaps(); 808 if (error) 809 goto Thaw; 810 811 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); 812 if (error || freezer_test_done) 813 goto Free_bitmaps; 814 815 if (in_suspend) { 816 unsigned int flags = 0; 817 818 if (hibernation_mode == HIBERNATION_PLATFORM) 819 flags |= SF_PLATFORM_MODE; 820 if (nocompress) { 821 flags |= SF_NOCOMPRESS_MODE; 822 } else { 823 flags |= SF_CRC32_MODE; 824 825 /* 826 * By default, LZO compression is enabled. Use SF_COMPRESSION_ALG_LZ4 827 * to override this behaviour and use LZ4. 828 * 829 * Refer kernel/power/power.h for more details 830 */ 831 832 if (!strcmp(hib_comp_algo, COMPRESSION_ALGO_LZ4)) 833 flags |= SF_COMPRESSION_ALG_LZ4; 834 else 835 flags |= SF_COMPRESSION_ALG_LZO; 836 } 837 838 pm_pr_dbg("Writing hibernation image.\n"); 839 error = swsusp_write(flags); 840 swsusp_free(); 841 if (!error) { 842 if (hibernation_mode == HIBERNATION_TEST_RESUME) 843 snapshot_test = true; 844 else 845 power_down(); 846 } 847 in_suspend = 0; 848 pm_restore_gfp_mask(); 849 } else { 850 pm_pr_dbg("Hibernation image restored successfully.\n"); 851 } 852 853 Free_bitmaps: 854 free_basic_memory_bitmaps(); 855 Thaw: 856 unlock_device_hotplug(); 857 if (snapshot_test) { 858 pm_pr_dbg("Checking hibernation image\n"); 859 error = swsusp_check(false); 860 if (!error) 861 error = load_image_and_restore(); 862 } 863 thaw_processes(); 864 865 /* Don't bother checking whether freezer_test_done is true */ 866 freezer_test_done = false; 867 Exit: 868 filesystems_thaw(); 869 Notify: 870 pm_notifier_call_chain(PM_POST_HIBERNATION); 871 Restore: 872 pm_restore_console(); 873 hibernate_release(); 874 Unlock: 875 unlock_system_sleep(sleep_flags); 876 pr_info("hibernation exit\n"); 877 878 return error; 879 } 880 881 /** 882 * hibernate_quiet_exec - Execute a function with all devices frozen. 883 * @func: Function to execute. 884 * @data: Data pointer to pass to @func. 885 * 886 * Return the @func return value or an error code if it cannot be executed. 887 */ 888 int hibernate_quiet_exec(int (*func)(void *data), void *data) 889 { 890 unsigned int sleep_flags; 891 int error; 892 893 sleep_flags = lock_system_sleep(); 894 895 if (!hibernate_acquire()) { 896 error = -EBUSY; 897 goto unlock; 898 } 899 900 pm_prepare_console(); 901 902 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION); 903 if (error) 904 goto restore; 905 906 filesystems_freeze(filesystem_freeze_enabled); 907 908 error = freeze_processes(); 909 if (error) 910 goto exit; 911 912 lock_device_hotplug(); 913 914 pm_suspend_clear_flags(); 915 916 error = platform_begin(true); 917 if (error) 918 goto thaw; 919 920 error = freeze_kernel_threads(); 921 if (error) 922 goto thaw; 923 924 error = dpm_prepare(PMSG_FREEZE); 925 if (error) 926 goto dpm_complete; 927 928 console_suspend_all(); 929 930 error = dpm_suspend(PMSG_FREEZE); 931 if (error) 932 goto dpm_resume; 933 934 error = dpm_suspend_end(PMSG_FREEZE); 935 if (error) 936 goto dpm_resume; 937 938 error = platform_pre_snapshot(true); 939 if (error) 940 goto skip; 941 942 error = func(data); 943 944 skip: 945 platform_finish(true); 946 947 dpm_resume_start(PMSG_THAW); 948 949 dpm_resume: 950 dpm_resume(PMSG_THAW); 951 952 console_resume_all(); 953 954 dpm_complete: 955 dpm_complete(PMSG_THAW); 956 957 thaw_kernel_threads(); 958 959 thaw: 960 platform_end(true); 961 962 unlock_device_hotplug(); 963 964 thaw_processes(); 965 966 exit: 967 filesystems_thaw(); 968 pm_notifier_call_chain(PM_POST_HIBERNATION); 969 970 restore: 971 pm_restore_console(); 972 973 hibernate_release(); 974 975 unlock: 976 unlock_system_sleep(sleep_flags); 977 978 return error; 979 } 980 EXPORT_SYMBOL_GPL(hibernate_quiet_exec); 981 982 static int __init find_resume_device(void) 983 { 984 if (!strlen(resume_file)) 985 return -ENOENT; 986 987 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file); 988 989 if (resume_delay) { 990 pr_info("Waiting %dsec before reading resume device ...\n", 991 resume_delay); 992 ssleep(resume_delay); 993 } 994 995 /* Check if the device is there */ 996 if (!early_lookup_bdev(resume_file, &swsusp_resume_device)) 997 return 0; 998 999 /* 1000 * Some device discovery might still be in progress; we need to wait for 1001 * this to finish. 1002 */ 1003 wait_for_device_probe(); 1004 if (resume_wait) { 1005 while (early_lookup_bdev(resume_file, &swsusp_resume_device)) 1006 msleep(10); 1007 async_synchronize_full(); 1008 } 1009 1010 return early_lookup_bdev(resume_file, &swsusp_resume_device); 1011 } 1012 1013 static int software_resume(void) 1014 { 1015 int error; 1016 1017 pm_pr_dbg("Hibernation image partition %d:%d present\n", 1018 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); 1019 1020 pm_pr_dbg("Looking for hibernation image.\n"); 1021 1022 mutex_lock(&system_transition_mutex); 1023 error = swsusp_check(true); 1024 if (error) 1025 goto Unlock; 1026 1027 /* 1028 * Check if the hibernation image is compressed. If so, query for 1029 * the algorithm support. 1030 */ 1031 if (!(swsusp_header_flags & SF_NOCOMPRESS_MODE)) { 1032 if (swsusp_header_flags & SF_COMPRESSION_ALG_LZ4) 1033 strscpy(hib_comp_algo, COMPRESSION_ALGO_LZ4); 1034 else 1035 strscpy(hib_comp_algo, COMPRESSION_ALGO_LZO); 1036 if (!crypto_has_acomp(hib_comp_algo, 0, CRYPTO_ALG_ASYNC)) { 1037 pr_err("%s compression is not available\n", hib_comp_algo); 1038 error = -EOPNOTSUPP; 1039 goto Unlock; 1040 } 1041 } 1042 1043 /* The snapshot device should not be opened while we're running */ 1044 if (!hibernate_acquire()) { 1045 error = -EBUSY; 1046 swsusp_close(); 1047 goto Unlock; 1048 } 1049 1050 pr_info("resume from hibernation\n"); 1051 pm_prepare_console(); 1052 error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE); 1053 if (error) 1054 goto Restore; 1055 1056 filesystems_freeze(filesystem_freeze_enabled); 1057 1058 pm_pr_dbg("Preparing processes for hibernation restore.\n"); 1059 error = freeze_processes(); 1060 if (error) { 1061 filesystems_thaw(); 1062 goto Close_Finish; 1063 } 1064 1065 error = freeze_kernel_threads(); 1066 if (error) { 1067 thaw_processes(); 1068 filesystems_thaw(); 1069 goto Close_Finish; 1070 } 1071 1072 error = load_image_and_restore(); 1073 thaw_processes(); 1074 filesystems_thaw(); 1075 Finish: 1076 pm_notifier_call_chain(PM_POST_RESTORE); 1077 Restore: 1078 pm_restore_console(); 1079 pr_info("resume failed (%d)\n", error); 1080 hibernate_release(); 1081 /* For success case, the suspend path will release the lock */ 1082 Unlock: 1083 mutex_unlock(&system_transition_mutex); 1084 pm_pr_dbg("Hibernation image not present or could not be loaded.\n"); 1085 return error; 1086 Close_Finish: 1087 swsusp_close(); 1088 goto Finish; 1089 } 1090 1091 /** 1092 * software_resume_initcall - Resume from a saved hibernation image. 1093 * 1094 * This routine is called as a late initcall, when all devices have been 1095 * discovered and initialized already. 1096 * 1097 * The image reading code is called to see if there is a hibernation image 1098 * available for reading. If that is the case, devices are quiesced and the 1099 * contents of memory is restored from the saved image. 1100 * 1101 * If this is successful, control reappears in the restored target kernel in 1102 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine 1103 * attempts to recover gracefully and make the kernel return to the normal mode 1104 * of operation. 1105 */ 1106 static int __init software_resume_initcall(void) 1107 { 1108 /* 1109 * If the user said "noresume".. bail out early. 1110 */ 1111 if (noresume || !hibernation_available()) 1112 return 0; 1113 1114 if (!swsusp_resume_device) { 1115 int error = find_resume_device(); 1116 1117 if (error) 1118 return error; 1119 } 1120 1121 return software_resume(); 1122 } 1123 late_initcall_sync(software_resume_initcall); 1124 1125 1126 static const char * const hibernation_modes[] = { 1127 [HIBERNATION_PLATFORM] = "platform", 1128 [HIBERNATION_SHUTDOWN] = "shutdown", 1129 [HIBERNATION_REBOOT] = "reboot", 1130 #ifdef CONFIG_SUSPEND 1131 [HIBERNATION_SUSPEND] = "suspend", 1132 #endif 1133 [HIBERNATION_TEST_RESUME] = "test_resume", 1134 }; 1135 1136 /* 1137 * /sys/power/disk - Control hibernation mode. 1138 * 1139 * Hibernation can be handled in several ways. There are a few different ways 1140 * to put the system into the sleep state: using the platform driver (e.g. ACPI 1141 * or other hibernation_ops), powering it off or rebooting it (for testing 1142 * mostly). 1143 * 1144 * The sysfs file /sys/power/disk provides an interface for selecting the 1145 * hibernation mode to use. Reading from this file causes the available modes 1146 * to be printed. There are 3 modes that can be supported: 1147 * 1148 * 'platform' 1149 * 'shutdown' 1150 * 'reboot' 1151 * 1152 * If a platform hibernation driver is in use, 'platform' will be supported 1153 * and will be used by default. Otherwise, 'shutdown' will be used by default. 1154 * The selected option (i.e. the one corresponding to the current value of 1155 * hibernation_mode) is enclosed by a square bracket. 1156 * 1157 * To select a given hibernation mode it is necessary to write the mode's 1158 * string representation (as returned by reading from /sys/power/disk) back 1159 * into /sys/power/disk. 1160 */ 1161 1162 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr, 1163 char *buf) 1164 { 1165 ssize_t count = 0; 1166 int i; 1167 1168 if (!hibernation_available()) 1169 return sysfs_emit(buf, "[disabled]\n"); 1170 1171 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { 1172 if (!hibernation_modes[i]) 1173 continue; 1174 switch (i) { 1175 case HIBERNATION_SHUTDOWN: 1176 case HIBERNATION_REBOOT: 1177 #ifdef CONFIG_SUSPEND 1178 case HIBERNATION_SUSPEND: 1179 #endif 1180 case HIBERNATION_TEST_RESUME: 1181 break; 1182 case HIBERNATION_PLATFORM: 1183 if (hibernation_ops) 1184 break; 1185 /* not a valid mode, continue with loop */ 1186 continue; 1187 } 1188 if (i == hibernation_mode) 1189 count += sysfs_emit_at(buf, count, "[%s] ", hibernation_modes[i]); 1190 else 1191 count += sysfs_emit_at(buf, count, "%s ", hibernation_modes[i]); 1192 } 1193 1194 /* Convert the last space to a newline if needed. */ 1195 if (count > 0) 1196 buf[count - 1] = '\n'; 1197 1198 return count; 1199 } 1200 1201 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, 1202 const char *buf, size_t n) 1203 { 1204 int mode = HIBERNATION_INVALID; 1205 unsigned int sleep_flags; 1206 int error = 0; 1207 int len; 1208 char *p; 1209 int i; 1210 1211 if (!hibernation_available()) 1212 return -EPERM; 1213 1214 p = memchr(buf, '\n', n); 1215 len = p ? p - buf : n; 1216 1217 sleep_flags = lock_system_sleep(); 1218 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { 1219 if (len == strlen(hibernation_modes[i]) 1220 && !strncmp(buf, hibernation_modes[i], len)) { 1221 mode = i; 1222 break; 1223 } 1224 } 1225 if (mode != HIBERNATION_INVALID) { 1226 switch (mode) { 1227 case HIBERNATION_SHUTDOWN: 1228 case HIBERNATION_REBOOT: 1229 #ifdef CONFIG_SUSPEND 1230 case HIBERNATION_SUSPEND: 1231 #endif 1232 case HIBERNATION_TEST_RESUME: 1233 hibernation_mode = mode; 1234 break; 1235 case HIBERNATION_PLATFORM: 1236 if (hibernation_ops) 1237 hibernation_mode = mode; 1238 else 1239 error = -EINVAL; 1240 } 1241 } else 1242 error = -EINVAL; 1243 1244 if (!error) 1245 pm_pr_dbg("Hibernation mode set to '%s'\n", 1246 hibernation_modes[mode]); 1247 unlock_system_sleep(sleep_flags); 1248 return error ? error : n; 1249 } 1250 1251 power_attr(disk); 1252 1253 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr, 1254 char *buf) 1255 { 1256 return sysfs_emit(buf, "%d:%d\n", MAJOR(swsusp_resume_device), 1257 MINOR(swsusp_resume_device)); 1258 } 1259 1260 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr, 1261 const char *buf, size_t n) 1262 { 1263 unsigned int sleep_flags; 1264 int len = n; 1265 char *name; 1266 dev_t dev; 1267 int error; 1268 1269 if (!hibernation_available()) 1270 return n; 1271 1272 if (len && buf[len-1] == '\n') 1273 len--; 1274 name = kstrndup(buf, len, GFP_KERNEL); 1275 if (!name) 1276 return -ENOMEM; 1277 1278 error = lookup_bdev(name, &dev); 1279 if (error) { 1280 unsigned maj, min, offset; 1281 char *p, dummy; 1282 1283 error = 0; 1284 if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 || 1285 sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, 1286 &dummy) == 3) { 1287 dev = MKDEV(maj, min); 1288 if (maj != MAJOR(dev) || min != MINOR(dev)) 1289 error = -EINVAL; 1290 } else { 1291 dev = new_decode_dev(simple_strtoul(name, &p, 16)); 1292 if (*p) 1293 error = -EINVAL; 1294 } 1295 } 1296 kfree(name); 1297 if (error) 1298 return error; 1299 1300 sleep_flags = lock_system_sleep(); 1301 swsusp_resume_device = dev; 1302 unlock_system_sleep(sleep_flags); 1303 1304 pm_pr_dbg("Configured hibernation resume from disk to %u\n", 1305 swsusp_resume_device); 1306 noresume = 0; 1307 software_resume(); 1308 return n; 1309 } 1310 1311 power_attr(resume); 1312 1313 static ssize_t resume_offset_show(struct kobject *kobj, 1314 struct kobj_attribute *attr, char *buf) 1315 { 1316 return sysfs_emit(buf, "%llu\n", (unsigned long long)swsusp_resume_block); 1317 } 1318 1319 static ssize_t resume_offset_store(struct kobject *kobj, 1320 struct kobj_attribute *attr, const char *buf, 1321 size_t n) 1322 { 1323 unsigned long long offset; 1324 int rc; 1325 1326 rc = kstrtoull(buf, 0, &offset); 1327 if (rc) 1328 return rc; 1329 swsusp_resume_block = offset; 1330 1331 return n; 1332 } 1333 1334 power_attr(resume_offset); 1335 1336 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr, 1337 char *buf) 1338 { 1339 return sysfs_emit(buf, "%lu\n", image_size); 1340 } 1341 1342 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr, 1343 const char *buf, size_t n) 1344 { 1345 unsigned long size; 1346 1347 if (sscanf(buf, "%lu", &size) == 1) { 1348 image_size = size; 1349 return n; 1350 } 1351 1352 return -EINVAL; 1353 } 1354 1355 power_attr(image_size); 1356 1357 static ssize_t reserved_size_show(struct kobject *kobj, 1358 struct kobj_attribute *attr, char *buf) 1359 { 1360 return sysfs_emit(buf, "%lu\n", reserved_size); 1361 } 1362 1363 static ssize_t reserved_size_store(struct kobject *kobj, 1364 struct kobj_attribute *attr, 1365 const char *buf, size_t n) 1366 { 1367 unsigned long size; 1368 1369 if (sscanf(buf, "%lu", &size) == 1) { 1370 reserved_size = size; 1371 return n; 1372 } 1373 1374 return -EINVAL; 1375 } 1376 1377 power_attr(reserved_size); 1378 1379 static struct attribute *g[] = { 1380 &disk_attr.attr, 1381 &resume_offset_attr.attr, 1382 &resume_attr.attr, 1383 &image_size_attr.attr, 1384 &reserved_size_attr.attr, 1385 NULL, 1386 }; 1387 1388 1389 static const struct attribute_group attr_group = { 1390 .attrs = g, 1391 }; 1392 1393 1394 static int __init pm_disk_init(void) 1395 { 1396 return sysfs_create_group(power_kobj, &attr_group); 1397 } 1398 1399 core_initcall(pm_disk_init); 1400 1401 1402 static int __init resume_setup(char *str) 1403 { 1404 if (noresume) 1405 return 1; 1406 1407 strscpy(resume_file, str); 1408 return 1; 1409 } 1410 1411 static int __init resume_offset_setup(char *str) 1412 { 1413 unsigned long long offset; 1414 1415 if (noresume) 1416 return 1; 1417 1418 if (sscanf(str, "%llu", &offset) == 1) 1419 swsusp_resume_block = offset; 1420 1421 return 1; 1422 } 1423 1424 static int __init hibernate_setup(char *str) 1425 { 1426 if (!strncmp(str, "noresume", 8)) { 1427 noresume = 1; 1428 } else if (!strncmp(str, "nocompress", 10)) { 1429 nocompress = 1; 1430 } else if (!strncmp(str, "no", 2)) { 1431 noresume = 1; 1432 nohibernate = 1; 1433 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) 1434 && !strncmp(str, "protect_image", 13)) { 1435 enable_restore_image_protection(); 1436 } 1437 return 1; 1438 } 1439 1440 static int __init noresume_setup(char *str) 1441 { 1442 noresume = 1; 1443 return 1; 1444 } 1445 1446 static int __init resumewait_setup(char *str) 1447 { 1448 resume_wait = 1; 1449 return 1; 1450 } 1451 1452 static int __init resumedelay_setup(char *str) 1453 { 1454 int rc = kstrtouint(str, 0, &resume_delay); 1455 1456 if (rc) 1457 pr_warn("resumedelay: bad option string '%s'\n", str); 1458 return 1; 1459 } 1460 1461 static int __init nohibernate_setup(char *str) 1462 { 1463 noresume = 1; 1464 nohibernate = 1; 1465 return 1; 1466 } 1467 1468 static const char * const comp_alg_enabled[] = { 1469 #if IS_ENABLED(CONFIG_CRYPTO_LZO) 1470 COMPRESSION_ALGO_LZO, 1471 #endif 1472 #if IS_ENABLED(CONFIG_CRYPTO_LZ4) 1473 COMPRESSION_ALGO_LZ4, 1474 #endif 1475 }; 1476 1477 static int hibernate_compressor_param_set(const char *compressor, 1478 const struct kernel_param *kp) 1479 { 1480 int index, ret; 1481 1482 if (!mutex_trylock(&system_transition_mutex)) 1483 return -EBUSY; 1484 1485 index = sysfs_match_string(comp_alg_enabled, compressor); 1486 if (index >= 0) { 1487 ret = param_set_copystring(comp_alg_enabled[index], kp); 1488 if (!ret) 1489 strscpy(hib_comp_algo, comp_alg_enabled[index]); 1490 } else { 1491 ret = index; 1492 } 1493 1494 mutex_unlock(&system_transition_mutex); 1495 1496 if (ret) 1497 pr_debug("Cannot set specified compressor %s\n", 1498 compressor); 1499 1500 return ret; 1501 } 1502 1503 static const struct kernel_param_ops hibernate_compressor_param_ops = { 1504 .set = hibernate_compressor_param_set, 1505 .get = param_get_string, 1506 }; 1507 1508 static struct kparam_string hibernate_compressor_param_string = { 1509 .maxlen = sizeof(hibernate_compressor), 1510 .string = hibernate_compressor, 1511 }; 1512 1513 module_param_cb(compressor, &hibernate_compressor_param_ops, 1514 &hibernate_compressor_param_string, 0644); 1515 MODULE_PARM_DESC(compressor, 1516 "Compression algorithm to be used with hibernation"); 1517 1518 __setup("noresume", noresume_setup); 1519 __setup("resume_offset=", resume_offset_setup); 1520 __setup("resume=", resume_setup); 1521 __setup("hibernate=", hibernate_setup); 1522 __setup("resumewait", resumewait_setup); 1523 __setup("resumedelay=", resumedelay_setup); 1524 __setup("nohibernate", nohibernate_setup); 1525