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 <linux/blkdev.h>
15 #include <linux/export.h>
16 #include <linux/suspend.h>
17 #include <linux/reboot.h>
18 #include <linux/string.h>
19 #include <linux/device.h>
20 #include <linux/async.h>
21 #include <linux/delay.h>
23 #include <linux/mount.h>
25 #include <linux/nmi.h>
26 #include <linux/console.h>
27 #include <linux/cpu.h>
28 #include <linux/freezer.h>
29 #include <linux/gfp.h>
30 #include <linux/syscore_ops.h>
31 #include <linux/ctype.h>
32 #include <linux/ktime.h>
33 #include <linux/security.h>
34 #include <linux/secretmem.h>
35 #include <trace/events/power.h>
40 static int nocompress;
42 static int nohibernate;
43 static int resume_wait;
44 static unsigned int resume_delay;
45 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
46 dev_t swsusp_resume_device;
47 sector_t swsusp_resume_block;
48 __visible int in_suspend __nosavedata;
58 HIBERNATION_TEST_RESUME,
60 __HIBERNATION_AFTER_LAST
62 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
63 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
65 static int hibernation_mode = HIBERNATION_SHUTDOWN;
67 bool freezer_test_done;
69 static const struct platform_hibernation_ops *hibernation_ops;
71 static atomic_t hibernate_atomic = ATOMIC_INIT(1);
73 bool hibernate_acquire(void)
75 return atomic_add_unless(&hibernate_atomic, -1, 0);
78 void hibernate_release(void)
80 atomic_inc(&hibernate_atomic);
83 bool hibernation_available(void)
85 return nohibernate == 0 &&
86 !security_locked_down(LOCKDOWN_HIBERNATION) &&
87 !secretmem_active() && !cxl_mem_active();
91 * hibernation_set_ops - Set the global hibernate operations.
92 * @ops: Hibernation operations to use in subsequent hibernation transitions.
94 void hibernation_set_ops(const struct platform_hibernation_ops *ops)
96 unsigned int sleep_flags;
98 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
99 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
100 && ops->restore_cleanup && ops->leave)) {
105 sleep_flags = lock_system_sleep();
107 hibernation_ops = ops;
109 hibernation_mode = HIBERNATION_PLATFORM;
110 else if (hibernation_mode == HIBERNATION_PLATFORM)
111 hibernation_mode = HIBERNATION_SHUTDOWN;
113 unlock_system_sleep(sleep_flags);
115 EXPORT_SYMBOL_GPL(hibernation_set_ops);
117 static bool entering_platform_hibernation;
119 bool system_entering_hibernation(void)
121 return entering_platform_hibernation;
123 EXPORT_SYMBOL(system_entering_hibernation);
125 #ifdef CONFIG_PM_DEBUG
126 static void hibernation_debug_sleep(void)
128 pr_info("debug: Waiting for 5 seconds.\n");
132 static int hibernation_test(int level)
134 if (pm_test_level == level) {
135 hibernation_debug_sleep();
140 #else /* !CONFIG_PM_DEBUG */
141 static int hibernation_test(int level) { return 0; }
142 #endif /* !CONFIG_PM_DEBUG */
145 * platform_begin - Call platform to start hibernation.
146 * @platform_mode: Whether or not to use the platform driver.
148 static int platform_begin(int platform_mode)
150 return (platform_mode && hibernation_ops) ?
151 hibernation_ops->begin(PMSG_FREEZE) : 0;
155 * platform_end - Call platform to finish transition to the working state.
156 * @platform_mode: Whether or not to use the platform driver.
158 static void platform_end(int platform_mode)
160 if (platform_mode && hibernation_ops)
161 hibernation_ops->end();
165 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
166 * @platform_mode: Whether or not to use the platform driver.
168 * Use the platform driver to prepare the system for creating a hibernate image,
169 * if so configured, and return an error code if that fails.
172 static int platform_pre_snapshot(int platform_mode)
174 return (platform_mode && hibernation_ops) ?
175 hibernation_ops->pre_snapshot() : 0;
179 * platform_leave - Call platform to prepare a transition to the working state.
180 * @platform_mode: Whether or not to use the platform driver.
182 * Use the platform driver prepare to prepare the machine for switching to the
183 * normal mode of operation.
185 * This routine is called on one CPU with interrupts disabled.
187 static void platform_leave(int platform_mode)
189 if (platform_mode && hibernation_ops)
190 hibernation_ops->leave();
194 * platform_finish - Call platform to switch the system to the working state.
195 * @platform_mode: Whether or not to use the platform driver.
197 * Use the platform driver to switch the machine to the normal mode of
200 * This routine must be called after platform_prepare().
202 static void platform_finish(int platform_mode)
204 if (platform_mode && hibernation_ops)
205 hibernation_ops->finish();
209 * platform_pre_restore - Prepare for hibernate image restoration.
210 * @platform_mode: Whether or not to use the platform driver.
212 * Use the platform driver to prepare the system for resume from a hibernation
215 * If the restore fails after this function has been called,
216 * platform_restore_cleanup() must be called.
218 static int platform_pre_restore(int platform_mode)
220 return (platform_mode && hibernation_ops) ?
221 hibernation_ops->pre_restore() : 0;
225 * platform_restore_cleanup - Switch to the working state after failing restore.
226 * @platform_mode: Whether or not to use the platform driver.
228 * Use the platform driver to switch the system to the normal mode of operation
229 * after a failing restore.
231 * If platform_pre_restore() has been called before the failing restore, this
232 * function must be called too, regardless of the result of
233 * platform_pre_restore().
235 static void platform_restore_cleanup(int platform_mode)
237 if (platform_mode && hibernation_ops)
238 hibernation_ops->restore_cleanup();
242 * platform_recover - Recover from a failure to suspend devices.
243 * @platform_mode: Whether or not to use the platform driver.
245 static void platform_recover(int platform_mode)
247 if (platform_mode && hibernation_ops && hibernation_ops->recover)
248 hibernation_ops->recover();
252 * swsusp_show_speed - Print time elapsed between two events during hibernation.
253 * @start: Starting event.
254 * @stop: Final event.
255 * @nr_pages: Number of memory pages processed between @start and @stop.
256 * @msg: Additional diagnostic message to print.
258 void swsusp_show_speed(ktime_t start, ktime_t stop,
259 unsigned nr_pages, char *msg)
262 u64 elapsed_centisecs64;
263 unsigned int centisecs;
267 diff = ktime_sub(stop, start);
268 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
269 centisecs = elapsed_centisecs64;
271 centisecs = 1; /* avoid div-by-zero */
272 k = nr_pages * (PAGE_SIZE / 1024);
273 kps = (k * 100) / centisecs;
274 pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
275 msg, k, centisecs / 100, centisecs % 100, kps / 1000,
279 __weak int arch_resume_nosmt(void)
285 * create_image - Create a hibernation image.
286 * @platform_mode: Whether or not to use the platform driver.
288 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
289 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
291 * Control reappears in this routine after the subsequent restore.
293 static int create_image(int platform_mode)
297 error = dpm_suspend_end(PMSG_FREEZE);
299 pr_err("Some devices failed to power down, aborting\n");
303 error = platform_pre_snapshot(platform_mode);
304 if (error || hibernation_test(TEST_PLATFORM))
305 goto Platform_finish;
307 error = pm_sleep_disable_secondary_cpus();
308 if (error || hibernation_test(TEST_CPUS))
313 system_state = SYSTEM_SUSPEND;
315 error = syscore_suspend();
317 pr_err("Some system devices failed to power down, aborting\n");
321 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
325 save_processor_state();
326 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
327 error = swsusp_arch_suspend();
328 /* Restore control flow magically appears here */
329 restore_processor_state();
330 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
332 pr_err("Error %d creating image\n", error);
335 events_check_enabled = false;
336 clear_or_poison_free_pages();
339 platform_leave(platform_mode);
345 system_state = SYSTEM_RUNNING;
349 pm_sleep_enable_secondary_cpus();
351 /* Allow architectures to do nosmt-specific post-resume dances */
353 error = arch_resume_nosmt();
356 platform_finish(platform_mode);
358 dpm_resume_start(in_suspend ?
359 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
365 * hibernation_snapshot - Quiesce devices and create a hibernation image.
366 * @platform_mode: If set, use platform driver to prepare for the transition.
368 * This routine must be called with system_transition_mutex held.
370 int hibernation_snapshot(int platform_mode)
375 pm_suspend_clear_flags();
376 error = platform_begin(platform_mode);
380 /* Preallocate image memory before shutting down devices. */
381 error = hibernate_preallocate_memory();
385 error = freeze_kernel_threads();
389 if (hibernation_test(TEST_FREEZER)) {
392 * Indicate to the caller that we are returning due to a
393 * successful freezer test.
395 freezer_test_done = true;
399 error = dpm_prepare(PMSG_FREEZE);
401 dpm_complete(PMSG_RECOVER);
406 pm_restrict_gfp_mask();
408 error = dpm_suspend(PMSG_FREEZE);
410 if (error || hibernation_test(TEST_DEVICES))
411 platform_recover(platform_mode);
413 error = create_image(platform_mode);
416 * In the case that we call create_image() above, the control
417 * returns here (1) after the image has been created or the
418 * image creation has failed and (2) after a successful restore.
421 /* We may need to release the preallocated image pages here. */
422 if (error || !in_suspend)
425 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
428 if (error || !in_suspend)
429 pm_restore_gfp_mask();
435 platform_end(platform_mode);
439 thaw_kernel_threads();
445 int __weak hibernate_resume_nonboot_cpu_disable(void)
447 return suspend_disable_secondary_cpus();
451 * resume_target_kernel - Restore system state from a hibernation image.
452 * @platform_mode: Whether or not to use the platform driver.
454 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
455 * contents of highmem that have not been restored yet from the image and run
456 * the low-level code that will restore the remaining contents of memory and
457 * switch to the just restored target kernel.
459 static int resume_target_kernel(bool platform_mode)
463 error = dpm_suspend_end(PMSG_QUIESCE);
465 pr_err("Some devices failed to power down, aborting resume\n");
469 error = platform_pre_restore(platform_mode);
475 error = hibernate_resume_nonboot_cpu_disable();
480 system_state = SYSTEM_SUSPEND;
482 error = syscore_suspend();
486 save_processor_state();
487 error = restore_highmem();
489 error = swsusp_arch_resume();
491 * The code below is only ever reached in case of a failure.
492 * Otherwise, execution continues at the place where
493 * swsusp_arch_suspend() was called.
497 * This call to restore_highmem() reverts the changes made by
503 * The only reason why swsusp_arch_resume() can fail is memory being
504 * very tight, so we have to free it as soon as we can to avoid
505 * subsequent failures.
508 restore_processor_state();
509 touch_softlockup_watchdog();
514 system_state = SYSTEM_RUNNING;
518 pm_sleep_enable_secondary_cpus();
521 platform_restore_cleanup(platform_mode);
523 dpm_resume_start(PMSG_RECOVER);
529 * hibernation_restore - Quiesce devices and restore from a hibernation image.
530 * @platform_mode: If set, use platform driver to prepare for the transition.
532 * This routine must be called with system_transition_mutex held. If it is
533 * successful, control reappears in the restored target kernel in
534 * hibernation_snapshot().
536 int hibernation_restore(int platform_mode)
540 pm_prepare_console();
542 pm_restrict_gfp_mask();
543 error = dpm_suspend_start(PMSG_QUIESCE);
545 error = resume_target_kernel(platform_mode);
547 * The above should either succeed and jump to the new kernel,
548 * or return with an error. Otherwise things are just
549 * undefined, so let's be paranoid.
553 dpm_resume_end(PMSG_RECOVER);
554 pm_restore_gfp_mask();
556 pm_restore_console();
561 * hibernation_platform_enter - Power off the system using the platform driver.
563 int hibernation_platform_enter(void)
567 if (!hibernation_ops)
571 * We have cancelled the power transition by running
572 * hibernation_ops->finish() before saving the image, so we should let
573 * the firmware know that we're going to enter the sleep state after all
575 error = hibernation_ops->begin(PMSG_HIBERNATE);
579 entering_platform_hibernation = true;
581 error = dpm_suspend_start(PMSG_HIBERNATE);
583 if (hibernation_ops->recover)
584 hibernation_ops->recover();
588 error = dpm_suspend_end(PMSG_HIBERNATE);
592 error = hibernation_ops->prepare();
594 goto Platform_finish;
596 error = pm_sleep_disable_secondary_cpus();
601 system_state = SYSTEM_SUSPEND;
603 if (pm_wakeup_pending()) {
608 hibernation_ops->enter();
609 /* We should never get here */
614 system_state = SYSTEM_RUNNING;
618 pm_sleep_enable_secondary_cpus();
621 hibernation_ops->finish();
623 dpm_resume_start(PMSG_RESTORE);
626 entering_platform_hibernation = false;
627 dpm_resume_end(PMSG_RESTORE);
631 hibernation_ops->end();
637 * power_down - Shut the machine down for hibernation.
639 * Use the platform driver, if configured, to put the system into the sleep
640 * state corresponding to hibernation, or try to power it off or reboot,
641 * depending on the value of hibernation_mode.
643 static void power_down(void)
645 #ifdef CONFIG_SUSPEND
648 if (hibernation_mode == HIBERNATION_SUSPEND) {
649 error = suspend_devices_and_enter(mem_sleep_current);
651 hibernation_mode = hibernation_ops ?
652 HIBERNATION_PLATFORM :
653 HIBERNATION_SHUTDOWN;
655 /* Restore swap signature. */
656 error = swsusp_unmark();
658 pr_err("Swap will be unusable! Try swapon -a.\n");
665 switch (hibernation_mode) {
666 case HIBERNATION_REBOOT:
667 kernel_restart(NULL);
669 case HIBERNATION_PLATFORM:
670 hibernation_platform_enter();
672 case HIBERNATION_SHUTDOWN:
673 if (kernel_can_power_off())
679 * Valid image is on the disk, if we continue we risk serious data
680 * corruption after resume.
682 pr_crit("Power down manually\n");
687 static int load_image_and_restore(bool snapshot_test)
692 pm_pr_dbg("Loading hibernation image.\n");
694 lock_device_hotplug();
695 error = create_basic_memory_bitmaps();
697 swsusp_close(snapshot_test);
701 error = swsusp_read(&flags);
702 swsusp_close(snapshot_test);
704 error = hibernation_restore(flags & SF_PLATFORM_MODE);
706 pr_err("Failed to load image, recovering.\n");
708 free_basic_memory_bitmaps();
710 unlock_device_hotplug();
716 * hibernate - Carry out system hibernation, including saving the image.
720 bool snapshot_test = false;
721 unsigned int sleep_flags;
724 if (!hibernation_available()) {
725 pm_pr_dbg("Hibernation not available.\n");
729 sleep_flags = lock_system_sleep();
730 /* The snapshot device should not be opened while we're running */
731 if (!hibernate_acquire()) {
736 pr_info("hibernation entry\n");
737 pm_prepare_console();
738 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
744 error = freeze_processes();
748 lock_device_hotplug();
749 /* Allocate memory management structures */
750 error = create_basic_memory_bitmaps();
754 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
755 if (error || freezer_test_done)
759 unsigned int flags = 0;
761 if (hibernation_mode == HIBERNATION_PLATFORM)
762 flags |= SF_PLATFORM_MODE;
764 flags |= SF_NOCOMPRESS_MODE;
766 flags |= SF_CRC32_MODE;
768 pm_pr_dbg("Writing hibernation image.\n");
769 error = swsusp_write(flags);
772 if (hibernation_mode == HIBERNATION_TEST_RESUME)
773 snapshot_test = true;
778 pm_restore_gfp_mask();
780 pm_pr_dbg("Hibernation image restored successfully.\n");
784 free_basic_memory_bitmaps();
786 unlock_device_hotplug();
788 pm_pr_dbg("Checking hibernation image\n");
789 error = swsusp_check(false);
791 error = load_image_and_restore(false);
795 /* Don't bother checking whether freezer_test_done is true */
796 freezer_test_done = false;
798 pm_notifier_call_chain(PM_POST_HIBERNATION);
800 pm_restore_console();
803 unlock_system_sleep(sleep_flags);
804 pr_info("hibernation exit\n");
810 * hibernate_quiet_exec - Execute a function with all devices frozen.
811 * @func: Function to execute.
812 * @data: Data pointer to pass to @func.
814 * Return the @func return value or an error code if it cannot be executed.
816 int hibernate_quiet_exec(int (*func)(void *data), void *data)
818 unsigned int sleep_flags;
821 sleep_flags = lock_system_sleep();
823 if (!hibernate_acquire()) {
828 pm_prepare_console();
830 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
834 error = freeze_processes();
838 lock_device_hotplug();
840 pm_suspend_clear_flags();
842 error = platform_begin(true);
846 error = freeze_kernel_threads();
850 error = dpm_prepare(PMSG_FREEZE);
856 error = dpm_suspend(PMSG_FREEZE);
860 error = dpm_suspend_end(PMSG_FREEZE);
864 error = platform_pre_snapshot(true);
871 platform_finish(true);
873 dpm_resume_start(PMSG_THAW);
876 dpm_resume(PMSG_THAW);
881 dpm_complete(PMSG_THAW);
883 thaw_kernel_threads();
888 unlock_device_hotplug();
893 pm_notifier_call_chain(PM_POST_HIBERNATION);
896 pm_restore_console();
901 unlock_system_sleep(sleep_flags);
905 EXPORT_SYMBOL_GPL(hibernate_quiet_exec);
907 static int __init find_resume_device(void)
909 if (!strlen(resume_file))
912 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
915 pr_info("Waiting %dsec before reading resume device ...\n",
917 ssleep(resume_delay);
920 /* Check if the device is there */
921 if (!early_lookup_bdev(resume_file, &swsusp_resume_device))
925 * Some device discovery might still be in progress; we need to wait for
928 wait_for_device_probe();
930 while (early_lookup_bdev(resume_file, &swsusp_resume_device))
932 async_synchronize_full();
935 return early_lookup_bdev(resume_file, &swsusp_resume_device);
938 static int software_resume(void)
942 pm_pr_dbg("Hibernation image partition %d:%d present\n",
943 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
945 pm_pr_dbg("Looking for hibernation image.\n");
947 mutex_lock(&system_transition_mutex);
948 error = swsusp_check(true);
952 /* The snapshot device should not be opened while we're running */
953 if (!hibernate_acquire()) {
959 pr_info("resume from hibernation\n");
960 pm_prepare_console();
961 error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE);
965 pm_pr_dbg("Preparing processes for hibernation restore.\n");
966 error = freeze_processes();
970 error = freeze_kernel_threads();
976 error = load_image_and_restore(true);
979 pm_notifier_call_chain(PM_POST_RESTORE);
981 pm_restore_console();
982 pr_info("resume failed (%d)\n", error);
984 /* For success case, the suspend path will release the lock */
986 mutex_unlock(&system_transition_mutex);
987 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
995 * software_resume_initcall - Resume from a saved hibernation image.
997 * This routine is called as a late initcall, when all devices have been
998 * discovered and initialized already.
1000 * The image reading code is called to see if there is a hibernation image
1001 * available for reading. If that is the case, devices are quiesced and the
1002 * contents of memory is restored from the saved image.
1004 * If this is successful, control reappears in the restored target kernel in
1005 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
1006 * attempts to recover gracefully and make the kernel return to the normal mode
1009 static int __init software_resume_initcall(void)
1012 * If the user said "noresume".. bail out early.
1014 if (noresume || !hibernation_available())
1017 if (!swsusp_resume_device) {
1018 int error = find_resume_device();
1024 return software_resume();
1026 late_initcall_sync(software_resume_initcall);
1029 static const char * const hibernation_modes[] = {
1030 [HIBERNATION_PLATFORM] = "platform",
1031 [HIBERNATION_SHUTDOWN] = "shutdown",
1032 [HIBERNATION_REBOOT] = "reboot",
1033 #ifdef CONFIG_SUSPEND
1034 [HIBERNATION_SUSPEND] = "suspend",
1036 [HIBERNATION_TEST_RESUME] = "test_resume",
1040 * /sys/power/disk - Control hibernation mode.
1042 * Hibernation can be handled in several ways. There are a few different ways
1043 * to put the system into the sleep state: using the platform driver (e.g. ACPI
1044 * or other hibernation_ops), powering it off or rebooting it (for testing
1047 * The sysfs file /sys/power/disk provides an interface for selecting the
1048 * hibernation mode to use. Reading from this file causes the available modes
1049 * to be printed. There are 3 modes that can be supported:
1055 * If a platform hibernation driver is in use, 'platform' will be supported
1056 * and will be used by default. Otherwise, 'shutdown' will be used by default.
1057 * The selected option (i.e. the one corresponding to the current value of
1058 * hibernation_mode) is enclosed by a square bracket.
1060 * To select a given hibernation mode it is necessary to write the mode's
1061 * string representation (as returned by reading from /sys/power/disk) back
1062 * into /sys/power/disk.
1065 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
1071 if (!hibernation_available())
1072 return sprintf(buf, "[disabled]\n");
1074 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1075 if (!hibernation_modes[i])
1078 case HIBERNATION_SHUTDOWN:
1079 case HIBERNATION_REBOOT:
1080 #ifdef CONFIG_SUSPEND
1081 case HIBERNATION_SUSPEND:
1083 case HIBERNATION_TEST_RESUME:
1085 case HIBERNATION_PLATFORM:
1086 if (hibernation_ops)
1088 /* not a valid mode, continue with loop */
1091 if (i == hibernation_mode)
1092 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
1094 buf += sprintf(buf, "%s ", hibernation_modes[i]);
1096 buf += sprintf(buf, "\n");
1100 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
1101 const char *buf, size_t n)
1103 int mode = HIBERNATION_INVALID;
1104 unsigned int sleep_flags;
1110 if (!hibernation_available())
1113 p = memchr(buf, '\n', n);
1114 len = p ? p - buf : n;
1116 sleep_flags = lock_system_sleep();
1117 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1118 if (len == strlen(hibernation_modes[i])
1119 && !strncmp(buf, hibernation_modes[i], len)) {
1124 if (mode != HIBERNATION_INVALID) {
1126 case HIBERNATION_SHUTDOWN:
1127 case HIBERNATION_REBOOT:
1128 #ifdef CONFIG_SUSPEND
1129 case HIBERNATION_SUSPEND:
1131 case HIBERNATION_TEST_RESUME:
1132 hibernation_mode = mode;
1134 case HIBERNATION_PLATFORM:
1135 if (hibernation_ops)
1136 hibernation_mode = mode;
1144 pm_pr_dbg("Hibernation mode set to '%s'\n",
1145 hibernation_modes[mode]);
1146 unlock_system_sleep(sleep_flags);
1147 return error ? error : n;
1152 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1155 return sprintf(buf, "%d:%d\n", MAJOR(swsusp_resume_device),
1156 MINOR(swsusp_resume_device));
1159 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1160 const char *buf, size_t n)
1162 unsigned int sleep_flags;
1168 if (!hibernation_available())
1171 if (len && buf[len-1] == '\n')
1173 name = kstrndup(buf, len, GFP_KERNEL);
1177 error = lookup_bdev(name, &dev);
1179 unsigned maj, min, offset;
1183 if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
1184 sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset,
1186 dev = MKDEV(maj, min);
1187 if (maj != MAJOR(dev) || min != MINOR(dev))
1190 dev = new_decode_dev(simple_strtoul(name, &p, 16));
1199 sleep_flags = lock_system_sleep();
1200 swsusp_resume_device = dev;
1201 unlock_system_sleep(sleep_flags);
1203 pm_pr_dbg("Configured hibernation resume from disk to %u\n",
1204 swsusp_resume_device);
1212 static ssize_t resume_offset_show(struct kobject *kobj,
1213 struct kobj_attribute *attr, char *buf)
1215 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1218 static ssize_t resume_offset_store(struct kobject *kobj,
1219 struct kobj_attribute *attr, const char *buf,
1222 unsigned long long offset;
1225 rc = kstrtoull(buf, 0, &offset);
1228 swsusp_resume_block = offset;
1233 power_attr(resume_offset);
1235 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1238 return sprintf(buf, "%lu\n", image_size);
1241 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1242 const char *buf, size_t n)
1246 if (sscanf(buf, "%lu", &size) == 1) {
1254 power_attr(image_size);
1256 static ssize_t reserved_size_show(struct kobject *kobj,
1257 struct kobj_attribute *attr, char *buf)
1259 return sprintf(buf, "%lu\n", reserved_size);
1262 static ssize_t reserved_size_store(struct kobject *kobj,
1263 struct kobj_attribute *attr,
1264 const char *buf, size_t n)
1268 if (sscanf(buf, "%lu", &size) == 1) {
1269 reserved_size = size;
1276 power_attr(reserved_size);
1278 static struct attribute *g[] = {
1280 &resume_offset_attr.attr,
1282 &image_size_attr.attr,
1283 &reserved_size_attr.attr,
1288 static const struct attribute_group attr_group = {
1293 static int __init pm_disk_init(void)
1295 return sysfs_create_group(power_kobj, &attr_group);
1298 core_initcall(pm_disk_init);
1301 static int __init resume_setup(char *str)
1306 strncpy(resume_file, str, 255);
1310 static int __init resume_offset_setup(char *str)
1312 unsigned long long offset;
1317 if (sscanf(str, "%llu", &offset) == 1)
1318 swsusp_resume_block = offset;
1323 static int __init hibernate_setup(char *str)
1325 if (!strncmp(str, "noresume", 8)) {
1327 } else if (!strncmp(str, "nocompress", 10)) {
1329 } else if (!strncmp(str, "no", 2)) {
1332 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
1333 && !strncmp(str, "protect_image", 13)) {
1334 enable_restore_image_protection();
1339 static int __init noresume_setup(char *str)
1345 static int __init resumewait_setup(char *str)
1351 static int __init resumedelay_setup(char *str)
1353 int rc = kstrtouint(str, 0, &resume_delay);
1356 pr_warn("resumedelay: bad option string '%s'\n", str);
1360 static int __init nohibernate_setup(char *str)
1367 __setup("noresume", noresume_setup);
1368 __setup("resume_offset=", resume_offset_setup);
1369 __setup("resume=", resume_setup);
1370 __setup("hibernate=", hibernate_setup);
1371 __setup("resumewait", resumewait_setup);
1372 __setup("resumedelay=", resumedelay_setup);
1373 __setup("nohibernate", nohibernate_setup);