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