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