1 // SPDX-License-Identifier: GPL-2.0-only
3 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
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>
12 #define pr_fmt(fmt) "PM: hibernation: " fmt
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>
24 #include <linux/mount.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>
41 static int nocompress;
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;
51 static char hibernate_compressor[CRYPTO_MAX_ALG_NAME] = CONFIG_HIBERNATION_DEF_COMP;
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.
58 char hib_comp_algo[CRYPTO_MAX_ALG_NAME];
68 HIBERNATION_TEST_RESUME,
70 __HIBERNATION_AFTER_LAST
72 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
73 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
75 static int hibernation_mode = HIBERNATION_SHUTDOWN;
77 bool freezer_test_done;
79 static const struct platform_hibernation_ops *hibernation_ops;
81 static atomic_t hibernate_atomic = ATOMIC_INIT(1);
83 bool hibernate_acquire(void)
85 return atomic_add_unless(&hibernate_atomic, -1, 0);
88 void hibernate_release(void)
90 atomic_inc(&hibernate_atomic);
93 bool hibernation_in_progress(void)
95 return !atomic_read(&hibernate_atomic);
98 bool hibernation_available(void)
100 return nohibernate == 0 &&
101 !security_locked_down(LOCKDOWN_HIBERNATION) &&
102 !secretmem_active() && !cxl_mem_active();
106 * hibernation_set_ops - Set the global hibernate operations.
107 * @ops: Hibernation operations to use in subsequent hibernation transitions.
109 void hibernation_set_ops(const struct platform_hibernation_ops *ops)
111 unsigned int sleep_flags;
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)) {
120 sleep_flags = lock_system_sleep();
122 hibernation_ops = ops;
124 hibernation_mode = HIBERNATION_PLATFORM;
125 else if (hibernation_mode == HIBERNATION_PLATFORM)
126 hibernation_mode = HIBERNATION_SHUTDOWN;
128 unlock_system_sleep(sleep_flags);
130 EXPORT_SYMBOL_GPL(hibernation_set_ops);
132 static bool entering_platform_hibernation;
134 bool system_entering_hibernation(void)
136 return entering_platform_hibernation;
138 EXPORT_SYMBOL(system_entering_hibernation);
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)
147 pr_info("hibernation debug: Waiting for %d second(s).\n",
149 mdelay(pm_test_delay * 1000);
152 static int hibernation_test(int level)
154 if (pm_test_level == level) {
155 hibernation_debug_sleep();
160 #else /* !CONFIG_PM_DEBUG */
161 static int hibernation_test(int level) { return 0; }
162 #endif /* !CONFIG_PM_DEBUG */
165 * platform_begin - Call platform to start hibernation.
166 * @platform_mode: Whether or not to use the platform driver.
168 static int platform_begin(int platform_mode)
170 return (platform_mode && hibernation_ops) ?
171 hibernation_ops->begin(PMSG_FREEZE) : 0;
175 * platform_end - Call platform to finish transition to the working state.
176 * @platform_mode: Whether or not to use the platform driver.
178 static void platform_end(int platform_mode)
180 if (platform_mode && hibernation_ops)
181 hibernation_ops->end();
185 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
186 * @platform_mode: Whether or not to use the platform driver.
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.
192 static int platform_pre_snapshot(int platform_mode)
194 return (platform_mode && hibernation_ops) ?
195 hibernation_ops->pre_snapshot() : 0;
199 * platform_leave - Call platform to prepare a transition to the working state.
200 * @platform_mode: Whether or not to use the platform driver.
202 * Use the platform driver prepare to prepare the machine for switching to the
203 * normal mode of operation.
205 * This routine is called on one CPU with interrupts disabled.
207 static void platform_leave(int platform_mode)
209 if (platform_mode && hibernation_ops)
210 hibernation_ops->leave();
214 * platform_finish - Call platform to switch the system to the working state.
215 * @platform_mode: Whether or not to use the platform driver.
217 * Use the platform driver to switch the machine to the normal mode of
220 * This routine must be called after platform_prepare().
222 static void platform_finish(int platform_mode)
224 if (platform_mode && hibernation_ops)
225 hibernation_ops->finish();
229 * platform_pre_restore - Prepare for hibernate image restoration.
230 * @platform_mode: Whether or not to use the platform driver.
232 * Use the platform driver to prepare the system for resume from a hibernation
235 * If the restore fails after this function has been called,
236 * platform_restore_cleanup() must be called.
238 static int platform_pre_restore(int platform_mode)
240 return (platform_mode && hibernation_ops) ?
241 hibernation_ops->pre_restore() : 0;
245 * platform_restore_cleanup - Switch to the working state after failing restore.
246 * @platform_mode: Whether or not to use the platform driver.
248 * Use the platform driver to switch the system to the normal mode of operation
249 * after a failing restore.
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().
255 static void platform_restore_cleanup(int platform_mode)
257 if (platform_mode && hibernation_ops)
258 hibernation_ops->restore_cleanup();
262 * platform_recover - Recover from a failure to suspend devices.
263 * @platform_mode: Whether or not to use the platform driver.
265 static void platform_recover(int platform_mode)
267 if (platform_mode && hibernation_ops && hibernation_ops->recover)
268 hibernation_ops->recover();
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.
278 void swsusp_show_speed(ktime_t start, ktime_t stop,
279 unsigned nr_pages, char *msg)
282 u64 elapsed_centisecs64;
283 unsigned int centisecs;
287 diff = ktime_sub(stop, start);
288 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
289 centisecs = elapsed_centisecs64;
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,
299 __weak int arch_resume_nosmt(void)
305 * create_image - Create a hibernation image.
306 * @platform_mode: Whether or not to use the platform driver.
308 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
309 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
311 * Control reappears in this routine after the subsequent restore.
313 static int create_image(int platform_mode)
317 error = dpm_suspend_end(PMSG_FREEZE);
319 pr_err("Some devices failed to power down, aborting\n");
323 error = platform_pre_snapshot(platform_mode);
324 if (error || hibernation_test(TEST_PLATFORM))
325 goto Platform_finish;
327 error = pm_sleep_disable_secondary_cpus();
328 if (error || hibernation_test(TEST_CPUS))
333 system_state = SYSTEM_SUSPEND;
335 error = syscore_suspend();
337 pr_err("Some system devices failed to power down, aborting\n");
341 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
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);
352 pr_err("Error %d creating image\n", error);
355 events_check_enabled = false;
356 clear_or_poison_free_pages();
359 platform_leave(platform_mode);
365 system_state = SYSTEM_RUNNING;
369 pm_sleep_enable_secondary_cpus();
371 /* Allow architectures to do nosmt-specific post-resume dances */
373 error = arch_resume_nosmt();
376 platform_finish(platform_mode);
378 dpm_resume_start(in_suspend ?
379 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
385 * hibernation_snapshot - Quiesce devices and create a hibernation image.
386 * @platform_mode: If set, use platform driver to prepare for the transition.
388 * This routine must be called with system_transition_mutex held.
390 int hibernation_snapshot(int platform_mode)
395 pm_suspend_clear_flags();
396 error = platform_begin(platform_mode);
400 /* Preallocate image memory before shutting down devices. */
401 error = hibernate_preallocate_memory();
405 error = freeze_kernel_threads();
409 if (hibernation_test(TEST_FREEZER)) {
412 * Indicate to the caller that we are returning due to a
413 * successful freezer test.
415 freezer_test_done = true;
419 error = dpm_prepare(PMSG_FREEZE);
421 dpm_complete(PMSG_RECOVER);
425 console_suspend_all();
427 error = dpm_suspend(PMSG_FREEZE);
429 if (error || hibernation_test(TEST_DEVICES))
430 platform_recover(platform_mode);
432 error = create_image(platform_mode);
435 * In the case that we call create_image() above, the control
436 * returns here (1) after the image has been created or the
437 * image creation has failed and (2) after a successful restore.
440 /* We may need to release the preallocated image pages here. */
441 if (error || !in_suspend)
444 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
447 if (error || !in_suspend)
448 pm_restore_gfp_mask();
450 console_resume_all();
454 platform_end(platform_mode);
458 thaw_kernel_threads();
464 int __weak hibernate_resume_nonboot_cpu_disable(void)
466 return suspend_disable_secondary_cpus();
470 * resume_target_kernel - Restore system state from a hibernation image.
471 * @platform_mode: Whether or not to use the platform driver.
473 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
474 * contents of highmem that have not been restored yet from the image and run
475 * the low-level code that will restore the remaining contents of memory and
476 * switch to the just restored target kernel.
478 static int resume_target_kernel(bool platform_mode)
482 error = dpm_suspend_end(PMSG_QUIESCE);
484 pr_err("Some devices failed to power down, aborting resume\n");
488 error = platform_pre_restore(platform_mode);
494 error = hibernate_resume_nonboot_cpu_disable();
499 system_state = SYSTEM_SUSPEND;
501 error = syscore_suspend();
505 save_processor_state();
506 error = restore_highmem();
508 error = swsusp_arch_resume();
510 * The code below is only ever reached in case of a failure.
511 * Otherwise, execution continues at the place where
512 * swsusp_arch_suspend() was called.
516 * This call to restore_highmem() reverts the changes made by
522 * The only reason why swsusp_arch_resume() can fail is memory being
523 * very tight, so we have to free it as soon as we can to avoid
524 * subsequent failures.
527 restore_processor_state();
528 touch_softlockup_watchdog();
533 system_state = SYSTEM_RUNNING;
537 pm_sleep_enable_secondary_cpus();
540 platform_restore_cleanup(platform_mode);
542 dpm_resume_start(PMSG_RECOVER);
548 * hibernation_restore - Quiesce devices and restore from a hibernation image.
549 * @platform_mode: If set, use platform driver to prepare for the transition.
551 * This routine must be called with system_transition_mutex held. If it is
552 * successful, control reappears in the restored target kernel in
553 * hibernation_snapshot().
555 int hibernation_restore(int platform_mode)
559 pm_prepare_console();
560 console_suspend_all();
561 error = dpm_suspend_start(PMSG_QUIESCE);
563 error = resume_target_kernel(platform_mode);
565 * The above should either succeed and jump to the new kernel,
566 * or return with an error. Otherwise things are just
567 * undefined, so let's be paranoid.
571 dpm_resume_end(PMSG_RECOVER);
572 console_resume_all();
573 pm_restore_console();
578 * hibernation_platform_enter - Power off the system using the platform driver.
580 int hibernation_platform_enter(void)
584 if (!hibernation_ops)
588 * We have cancelled the power transition by running
589 * hibernation_ops->finish() before saving the image, so we should let
590 * the firmware know that we're going to enter the sleep state after all
592 error = hibernation_ops->begin(PMSG_HIBERNATE);
596 entering_platform_hibernation = true;
597 console_suspend_all();
598 error = dpm_suspend_start(PMSG_HIBERNATE);
600 if (hibernation_ops->recover)
601 hibernation_ops->recover();
605 error = dpm_suspend_end(PMSG_HIBERNATE);
609 error = hibernation_ops->prepare();
611 goto Platform_finish;
613 error = pm_sleep_disable_secondary_cpus();
618 system_state = SYSTEM_SUSPEND;
620 error = syscore_suspend();
624 if (pm_wakeup_pending()) {
629 hibernation_ops->enter();
630 /* We should never get here */
636 system_state = SYSTEM_RUNNING;
640 pm_sleep_enable_secondary_cpus();
643 hibernation_ops->finish();
645 dpm_resume_start(PMSG_RESTORE);
648 entering_platform_hibernation = false;
649 dpm_resume_end(PMSG_RESTORE);
650 console_resume_all();
653 hibernation_ops->end();
659 * power_down - Shut the machine down for hibernation.
661 * Use the platform driver, if configured, to put the system into the sleep
662 * state corresponding to hibernation, or try to power it off or reboot,
663 * depending on the value of hibernation_mode.
665 static void power_down(void)
669 #ifdef CONFIG_SUSPEND
670 if (hibernation_mode == HIBERNATION_SUSPEND) {
671 error = suspend_devices_and_enter(mem_sleep_current);
673 hibernation_mode = hibernation_ops ?
674 HIBERNATION_PLATFORM :
675 HIBERNATION_SHUTDOWN;
677 /* Restore swap signature. */
678 error = swsusp_unmark();
680 pr_err("Swap will be unusable! Try swapon -a.\n");
687 switch (hibernation_mode) {
688 case HIBERNATION_REBOOT:
689 kernel_restart(NULL);
691 case HIBERNATION_PLATFORM:
692 error = hibernation_platform_enter();
693 if (error == -EAGAIN || error == -EBUSY) {
695 events_check_enabled = false;
696 pr_info("Wakeup event detected during hibernation, rolling back.\n");
700 case HIBERNATION_SHUTDOWN:
701 if (kernel_can_power_off()) {
702 entering_platform_hibernation = true;
704 entering_platform_hibernation = false;
710 * Valid image is on the disk, if we continue we risk serious data
711 * corruption after resume.
713 pr_crit("Power down manually\n");
718 static int load_image_and_restore(void)
723 pm_pr_dbg("Loading hibernation image.\n");
725 lock_device_hotplug();
726 error = create_basic_memory_bitmaps();
732 error = swsusp_read(&flags);
735 error = hibernation_restore(flags & SF_PLATFORM_MODE);
737 pr_err("Failed to load image, recovering.\n");
739 free_basic_memory_bitmaps();
741 unlock_device_hotplug();
746 #define COMPRESSION_ALGO_LZO "lzo"
747 #define COMPRESSION_ALGO_LZ4 "lz4"
750 * hibernate - Carry out system hibernation, including saving the image.
754 bool snapshot_test = false;
755 unsigned int sleep_flags;
758 if (!hibernation_available()) {
759 pm_pr_dbg("Hibernation not available.\n");
764 * Query for the compression algorithm support if compression is enabled.
767 strscpy(hib_comp_algo, hibernate_compressor);
768 if (!crypto_has_acomp(hib_comp_algo, 0, CRYPTO_ALG_ASYNC)) {
769 pr_err("%s compression is not available\n", hib_comp_algo);
774 sleep_flags = lock_system_sleep();
775 /* The snapshot device should not be opened while we're running */
776 if (!hibernate_acquire()) {
781 pr_info("hibernation entry\n");
782 pm_prepare_console();
783 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
788 if (filesystem_freeze_enabled)
789 filesystems_freeze();
791 error = freeze_processes();
795 lock_device_hotplug();
796 /* Allocate memory management structures */
797 error = create_basic_memory_bitmaps();
801 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
802 if (error || freezer_test_done)
806 unsigned int flags = 0;
808 if (hibernation_mode == HIBERNATION_PLATFORM)
809 flags |= SF_PLATFORM_MODE;
811 flags |= SF_NOCOMPRESS_MODE;
813 flags |= SF_CRC32_MODE;
816 * By default, LZO compression is enabled. Use SF_COMPRESSION_ALG_LZ4
817 * to override this behaviour and use LZ4.
819 * Refer kernel/power/power.h for more details
822 if (!strcmp(hib_comp_algo, COMPRESSION_ALGO_LZ4))
823 flags |= SF_COMPRESSION_ALG_LZ4;
825 flags |= SF_COMPRESSION_ALG_LZO;
828 pm_pr_dbg("Writing hibernation image.\n");
829 error = swsusp_write(flags);
832 if (hibernation_mode == HIBERNATION_TEST_RESUME)
833 snapshot_test = true;
838 pm_restore_gfp_mask();
840 pm_pr_dbg("Hibernation image restored successfully.\n");
844 free_basic_memory_bitmaps();
846 unlock_device_hotplug();
848 pm_pr_dbg("Checking hibernation image\n");
849 error = swsusp_check(false);
851 error = load_image_and_restore();
855 /* Don't bother checking whether freezer_test_done is true */
856 freezer_test_done = false;
859 pm_notifier_call_chain(PM_POST_HIBERNATION);
861 pm_restore_console();
864 unlock_system_sleep(sleep_flags);
865 pr_info("hibernation exit\n");
871 * hibernate_quiet_exec - Execute a function with all devices frozen.
872 * @func: Function to execute.
873 * @data: Data pointer to pass to @func.
875 * Return the @func return value or an error code if it cannot be executed.
877 int hibernate_quiet_exec(int (*func)(void *data), void *data)
879 unsigned int sleep_flags;
882 sleep_flags = lock_system_sleep();
884 if (!hibernate_acquire()) {
889 pm_prepare_console();
891 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
895 if (filesystem_freeze_enabled)
896 filesystems_freeze();
898 error = freeze_processes();
902 lock_device_hotplug();
904 pm_suspend_clear_flags();
906 error = platform_begin(true);
910 error = freeze_kernel_threads();
914 error = dpm_prepare(PMSG_FREEZE);
918 console_suspend_all();
920 error = dpm_suspend(PMSG_FREEZE);
924 error = dpm_suspend_end(PMSG_FREEZE);
928 error = platform_pre_snapshot(true);
935 platform_finish(true);
937 dpm_resume_start(PMSG_THAW);
940 dpm_resume(PMSG_THAW);
942 console_resume_all();
945 dpm_complete(PMSG_THAW);
947 thaw_kernel_threads();
952 unlock_device_hotplug();
958 pm_notifier_call_chain(PM_POST_HIBERNATION);
961 pm_restore_console();
966 unlock_system_sleep(sleep_flags);
970 EXPORT_SYMBOL_GPL(hibernate_quiet_exec);
972 static int __init find_resume_device(void)
974 if (!strlen(resume_file))
977 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
980 pr_info("Waiting %dsec before reading resume device ...\n",
982 ssleep(resume_delay);
985 /* Check if the device is there */
986 if (!early_lookup_bdev(resume_file, &swsusp_resume_device))
990 * Some device discovery might still be in progress; we need to wait for
993 wait_for_device_probe();
995 while (early_lookup_bdev(resume_file, &swsusp_resume_device))
997 async_synchronize_full();
1000 return early_lookup_bdev(resume_file, &swsusp_resume_device);
1003 static int software_resume(void)
1007 pm_pr_dbg("Hibernation image partition %d:%d present\n",
1008 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
1010 pm_pr_dbg("Looking for hibernation image.\n");
1012 mutex_lock(&system_transition_mutex);
1013 error = swsusp_check(true);
1018 * Check if the hibernation image is compressed. If so, query for
1019 * the algorithm support.
1021 if (!(swsusp_header_flags & SF_NOCOMPRESS_MODE)) {
1022 if (swsusp_header_flags & SF_COMPRESSION_ALG_LZ4)
1023 strscpy(hib_comp_algo, COMPRESSION_ALGO_LZ4);
1025 strscpy(hib_comp_algo, COMPRESSION_ALGO_LZO);
1026 if (!crypto_has_acomp(hib_comp_algo, 0, CRYPTO_ALG_ASYNC)) {
1027 pr_err("%s compression is not available\n", hib_comp_algo);
1028 error = -EOPNOTSUPP;
1033 /* The snapshot device should not be opened while we're running */
1034 if (!hibernate_acquire()) {
1040 pr_info("resume from hibernation\n");
1041 pm_prepare_console();
1042 error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE);
1046 if (filesystem_freeze_enabled)
1047 filesystems_freeze();
1049 pm_pr_dbg("Preparing processes for hibernation restore.\n");
1050 error = freeze_processes();
1056 error = freeze_kernel_threads();
1063 error = load_image_and_restore();
1067 pm_notifier_call_chain(PM_POST_RESTORE);
1069 pm_restore_console();
1070 pr_info("resume failed (%d)\n", error);
1071 hibernate_release();
1072 /* For success case, the suspend path will release the lock */
1074 mutex_unlock(&system_transition_mutex);
1075 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
1083 * software_resume_initcall - Resume from a saved hibernation image.
1085 * This routine is called as a late initcall, when all devices have been
1086 * discovered and initialized already.
1088 * The image reading code is called to see if there is a hibernation image
1089 * available for reading. If that is the case, devices are quiesced and the
1090 * contents of memory is restored from the saved image.
1092 * If this is successful, control reappears in the restored target kernel in
1093 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
1094 * attempts to recover gracefully and make the kernel return to the normal mode
1097 static int __init software_resume_initcall(void)
1100 * If the user said "noresume".. bail out early.
1102 if (noresume || !hibernation_available())
1105 if (!swsusp_resume_device) {
1106 int error = find_resume_device();
1112 return software_resume();
1114 late_initcall_sync(software_resume_initcall);
1117 static const char * const hibernation_modes[] = {
1118 [HIBERNATION_PLATFORM] = "platform",
1119 [HIBERNATION_SHUTDOWN] = "shutdown",
1120 [HIBERNATION_REBOOT] = "reboot",
1121 #ifdef CONFIG_SUSPEND
1122 [HIBERNATION_SUSPEND] = "suspend",
1124 [HIBERNATION_TEST_RESUME] = "test_resume",
1128 * /sys/power/disk - Control hibernation mode.
1130 * Hibernation can be handled in several ways. There are a few different ways
1131 * to put the system into the sleep state: using the platform driver (e.g. ACPI
1132 * or other hibernation_ops), powering it off or rebooting it (for testing
1135 * The sysfs file /sys/power/disk provides an interface for selecting the
1136 * hibernation mode to use. Reading from this file causes the available modes
1137 * to be printed. There are 3 modes that can be supported:
1143 * If a platform hibernation driver is in use, 'platform' will be supported
1144 * and will be used by default. Otherwise, 'shutdown' will be used by default.
1145 * The selected option (i.e. the one corresponding to the current value of
1146 * hibernation_mode) is enclosed by a square bracket.
1148 * To select a given hibernation mode it is necessary to write the mode's
1149 * string representation (as returned by reading from /sys/power/disk) back
1150 * into /sys/power/disk.
1153 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
1159 if (!hibernation_available())
1160 return sysfs_emit(buf, "[disabled]\n");
1162 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1163 if (!hibernation_modes[i])
1166 case HIBERNATION_SHUTDOWN:
1167 case HIBERNATION_REBOOT:
1168 #ifdef CONFIG_SUSPEND
1169 case HIBERNATION_SUSPEND:
1171 case HIBERNATION_TEST_RESUME:
1173 case HIBERNATION_PLATFORM:
1174 if (hibernation_ops)
1176 /* not a valid mode, continue with loop */
1179 if (i == hibernation_mode)
1180 count += sysfs_emit_at(buf, count, "[%s] ", hibernation_modes[i]);
1182 count += sysfs_emit_at(buf, count, "%s ", hibernation_modes[i]);
1185 /* Convert the last space to a newline if needed. */
1187 buf[count - 1] = '\n';
1192 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
1193 const char *buf, size_t n)
1195 int mode = HIBERNATION_INVALID;
1196 unsigned int sleep_flags;
1202 if (!hibernation_available())
1205 p = memchr(buf, '\n', n);
1206 len = p ? p - buf : n;
1208 sleep_flags = lock_system_sleep();
1209 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1210 if (len == strlen(hibernation_modes[i])
1211 && !strncmp(buf, hibernation_modes[i], len)) {
1216 if (mode != HIBERNATION_INVALID) {
1218 case HIBERNATION_SHUTDOWN:
1219 case HIBERNATION_REBOOT:
1220 #ifdef CONFIG_SUSPEND
1221 case HIBERNATION_SUSPEND:
1223 case HIBERNATION_TEST_RESUME:
1224 hibernation_mode = mode;
1226 case HIBERNATION_PLATFORM:
1227 if (hibernation_ops)
1228 hibernation_mode = mode;
1236 pm_pr_dbg("Hibernation mode set to '%s'\n",
1237 hibernation_modes[mode]);
1238 unlock_system_sleep(sleep_flags);
1239 return error ? error : n;
1244 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1247 return sysfs_emit(buf, "%d:%d\n", MAJOR(swsusp_resume_device),
1248 MINOR(swsusp_resume_device));
1251 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1252 const char *buf, size_t n)
1254 unsigned int sleep_flags;
1260 if (!hibernation_available())
1263 if (len && buf[len-1] == '\n')
1265 name = kstrndup(buf, len, GFP_KERNEL);
1269 error = lookup_bdev(name, &dev);
1271 unsigned maj, min, offset;
1275 if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
1276 sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset,
1278 dev = MKDEV(maj, min);
1279 if (maj != MAJOR(dev) || min != MINOR(dev))
1282 dev = new_decode_dev(simple_strtoul(name, &p, 16));
1291 sleep_flags = lock_system_sleep();
1292 swsusp_resume_device = dev;
1293 unlock_system_sleep(sleep_flags);
1295 pm_pr_dbg("Configured hibernation resume from disk to %u\n",
1296 swsusp_resume_device);
1304 static ssize_t resume_offset_show(struct kobject *kobj,
1305 struct kobj_attribute *attr, char *buf)
1307 return sysfs_emit(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1310 static ssize_t resume_offset_store(struct kobject *kobj,
1311 struct kobj_attribute *attr, const char *buf,
1314 unsigned long long offset;
1317 rc = kstrtoull(buf, 0, &offset);
1320 swsusp_resume_block = offset;
1325 power_attr(resume_offset);
1327 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1330 return sysfs_emit(buf, "%lu\n", image_size);
1333 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1334 const char *buf, size_t n)
1338 if (sscanf(buf, "%lu", &size) == 1) {
1346 power_attr(image_size);
1348 static ssize_t reserved_size_show(struct kobject *kobj,
1349 struct kobj_attribute *attr, char *buf)
1351 return sysfs_emit(buf, "%lu\n", reserved_size);
1354 static ssize_t reserved_size_store(struct kobject *kobj,
1355 struct kobj_attribute *attr,
1356 const char *buf, size_t n)
1360 if (sscanf(buf, "%lu", &size) == 1) {
1361 reserved_size = size;
1368 power_attr(reserved_size);
1370 static struct attribute *g[] = {
1372 &resume_offset_attr.attr,
1374 &image_size_attr.attr,
1375 &reserved_size_attr.attr,
1380 static const struct attribute_group attr_group = {
1385 static int __init pm_disk_init(void)
1387 return sysfs_create_group(power_kobj, &attr_group);
1390 core_initcall(pm_disk_init);
1393 static int __init resume_setup(char *str)
1398 strscpy(resume_file, str);
1402 static int __init resume_offset_setup(char *str)
1404 unsigned long long offset;
1409 if (sscanf(str, "%llu", &offset) == 1)
1410 swsusp_resume_block = offset;
1415 static int __init hibernate_setup(char *str)
1417 if (!strncmp(str, "noresume", 8)) {
1419 } else if (!strncmp(str, "nocompress", 10)) {
1421 } else if (!strncmp(str, "no", 2)) {
1424 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
1425 && !strncmp(str, "protect_image", 13)) {
1426 enable_restore_image_protection();
1431 static int __init noresume_setup(char *str)
1437 static int __init resumewait_setup(char *str)
1443 static int __init resumedelay_setup(char *str)
1445 int rc = kstrtouint(str, 0, &resume_delay);
1448 pr_warn("resumedelay: bad option string '%s'\n", str);
1452 static int __init nohibernate_setup(char *str)
1459 static const char * const comp_alg_enabled[] = {
1460 #if IS_ENABLED(CONFIG_CRYPTO_LZO)
1461 COMPRESSION_ALGO_LZO,
1463 #if IS_ENABLED(CONFIG_CRYPTO_LZ4)
1464 COMPRESSION_ALGO_LZ4,
1468 static int hibernate_compressor_param_set(const char *compressor,
1469 const struct kernel_param *kp)
1473 if (!mutex_trylock(&system_transition_mutex))
1476 index = sysfs_match_string(comp_alg_enabled, compressor);
1478 ret = param_set_copystring(comp_alg_enabled[index], kp);
1480 strscpy(hib_comp_algo, comp_alg_enabled[index]);
1485 mutex_unlock(&system_transition_mutex);
1488 pr_debug("Cannot set specified compressor %s\n",
1494 static const struct kernel_param_ops hibernate_compressor_param_ops = {
1495 .set = hibernate_compressor_param_set,
1496 .get = param_get_string,
1499 static struct kparam_string hibernate_compressor_param_string = {
1500 .maxlen = sizeof(hibernate_compressor),
1501 .string = hibernate_compressor,
1504 module_param_cb(compressor, &hibernate_compressor_param_ops,
1505 &hibernate_compressor_param_string, 0644);
1506 MODULE_PARM_DESC(compressor,
1507 "Compression algorithm to be used with hibernation");
1509 __setup("noresume", noresume_setup);
1510 __setup("resume_offset=", resume_offset_setup);
1511 __setup("resume=", resume_setup);
1512 __setup("hibernate=", hibernate_setup);
1513 __setup("resumewait", resumewait_setup);
1514 __setup("resumedelay=", resumedelay_setup);
1515 __setup("nohibernate", nohibernate_setup);