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)
647 #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 error = hibernation_platform_enter();
671 if (error == -EAGAIN || error == -EBUSY) {
673 events_check_enabled = false;
674 pr_info("Wakeup event detected during hibernation, rolling back.\n");
678 case HIBERNATION_SHUTDOWN:
679 if (kernel_can_power_off())
685 * Valid image is on the disk, if we continue we risk serious data
686 * corruption after resume.
688 pr_crit("Power down manually\n");
693 static int load_image_and_restore(void)
698 pm_pr_dbg("Loading hibernation image.\n");
700 lock_device_hotplug();
701 error = create_basic_memory_bitmaps();
707 error = swsusp_read(&flags);
710 error = hibernation_restore(flags & SF_PLATFORM_MODE);
712 pr_err("Failed to load image, recovering.\n");
714 free_basic_memory_bitmaps();
716 unlock_device_hotplug();
722 * hibernate - Carry out system hibernation, including saving the image.
726 bool snapshot_test = false;
727 unsigned int sleep_flags;
730 if (!hibernation_available()) {
731 pm_pr_dbg("Hibernation not available.\n");
735 sleep_flags = lock_system_sleep();
736 /* The snapshot device should not be opened while we're running */
737 if (!hibernate_acquire()) {
742 pr_info("hibernation entry\n");
743 pm_prepare_console();
744 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
750 error = freeze_processes();
754 lock_device_hotplug();
755 /* Allocate memory management structures */
756 error = create_basic_memory_bitmaps();
760 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
761 if (error || freezer_test_done)
765 unsigned int flags = 0;
767 if (hibernation_mode == HIBERNATION_PLATFORM)
768 flags |= SF_PLATFORM_MODE;
770 flags |= SF_NOCOMPRESS_MODE;
772 flags |= SF_CRC32_MODE;
774 pm_pr_dbg("Writing hibernation image.\n");
775 error = swsusp_write(flags);
778 if (hibernation_mode == HIBERNATION_TEST_RESUME)
779 snapshot_test = true;
784 pm_restore_gfp_mask();
786 pm_pr_dbg("Hibernation image restored successfully.\n");
790 free_basic_memory_bitmaps();
792 unlock_device_hotplug();
794 pm_pr_dbg("Checking hibernation image\n");
795 error = swsusp_check(false);
797 error = load_image_and_restore();
801 /* Don't bother checking whether freezer_test_done is true */
802 freezer_test_done = false;
804 pm_notifier_call_chain(PM_POST_HIBERNATION);
806 pm_restore_console();
809 unlock_system_sleep(sleep_flags);
810 pr_info("hibernation exit\n");
816 * hibernate_quiet_exec - Execute a function with all devices frozen.
817 * @func: Function to execute.
818 * @data: Data pointer to pass to @func.
820 * Return the @func return value or an error code if it cannot be executed.
822 int hibernate_quiet_exec(int (*func)(void *data), void *data)
824 unsigned int sleep_flags;
827 sleep_flags = lock_system_sleep();
829 if (!hibernate_acquire()) {
834 pm_prepare_console();
836 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
840 error = freeze_processes();
844 lock_device_hotplug();
846 pm_suspend_clear_flags();
848 error = platform_begin(true);
852 error = freeze_kernel_threads();
856 error = dpm_prepare(PMSG_FREEZE);
862 error = dpm_suspend(PMSG_FREEZE);
866 error = dpm_suspend_end(PMSG_FREEZE);
870 error = platform_pre_snapshot(true);
877 platform_finish(true);
879 dpm_resume_start(PMSG_THAW);
882 dpm_resume(PMSG_THAW);
887 dpm_complete(PMSG_THAW);
889 thaw_kernel_threads();
894 unlock_device_hotplug();
899 pm_notifier_call_chain(PM_POST_HIBERNATION);
902 pm_restore_console();
907 unlock_system_sleep(sleep_flags);
911 EXPORT_SYMBOL_GPL(hibernate_quiet_exec);
913 static int __init find_resume_device(void)
915 if (!strlen(resume_file))
918 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
921 pr_info("Waiting %dsec before reading resume device ...\n",
923 ssleep(resume_delay);
926 /* Check if the device is there */
927 if (!early_lookup_bdev(resume_file, &swsusp_resume_device))
931 * Some device discovery might still be in progress; we need to wait for
934 wait_for_device_probe();
936 while (early_lookup_bdev(resume_file, &swsusp_resume_device))
938 async_synchronize_full();
941 return early_lookup_bdev(resume_file, &swsusp_resume_device);
944 static int software_resume(void)
948 pm_pr_dbg("Hibernation image partition %d:%d present\n",
949 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
951 pm_pr_dbg("Looking for hibernation image.\n");
953 mutex_lock(&system_transition_mutex);
954 error = swsusp_check(true);
958 /* The snapshot device should not be opened while we're running */
959 if (!hibernate_acquire()) {
965 pr_info("resume from hibernation\n");
966 pm_prepare_console();
967 error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE);
971 pm_pr_dbg("Preparing processes for hibernation restore.\n");
972 error = freeze_processes();
976 error = freeze_kernel_threads();
982 error = load_image_and_restore();
985 pm_notifier_call_chain(PM_POST_RESTORE);
987 pm_restore_console();
988 pr_info("resume failed (%d)\n", error);
990 /* For success case, the suspend path will release the lock */
992 mutex_unlock(&system_transition_mutex);
993 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
1001 * software_resume_initcall - Resume from a saved hibernation image.
1003 * This routine is called as a late initcall, when all devices have been
1004 * discovered and initialized already.
1006 * The image reading code is called to see if there is a hibernation image
1007 * available for reading. If that is the case, devices are quiesced and the
1008 * contents of memory is restored from the saved image.
1010 * If this is successful, control reappears in the restored target kernel in
1011 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
1012 * attempts to recover gracefully and make the kernel return to the normal mode
1015 static int __init software_resume_initcall(void)
1018 * If the user said "noresume".. bail out early.
1020 if (noresume || !hibernation_available())
1023 if (!swsusp_resume_device) {
1024 int error = find_resume_device();
1030 return software_resume();
1032 late_initcall_sync(software_resume_initcall);
1035 static const char * const hibernation_modes[] = {
1036 [HIBERNATION_PLATFORM] = "platform",
1037 [HIBERNATION_SHUTDOWN] = "shutdown",
1038 [HIBERNATION_REBOOT] = "reboot",
1039 #ifdef CONFIG_SUSPEND
1040 [HIBERNATION_SUSPEND] = "suspend",
1042 [HIBERNATION_TEST_RESUME] = "test_resume",
1046 * /sys/power/disk - Control hibernation mode.
1048 * Hibernation can be handled in several ways. There are a few different ways
1049 * to put the system into the sleep state: using the platform driver (e.g. ACPI
1050 * or other hibernation_ops), powering it off or rebooting it (for testing
1053 * The sysfs file /sys/power/disk provides an interface for selecting the
1054 * hibernation mode to use. Reading from this file causes the available modes
1055 * to be printed. There are 3 modes that can be supported:
1061 * If a platform hibernation driver is in use, 'platform' will be supported
1062 * and will be used by default. Otherwise, 'shutdown' will be used by default.
1063 * The selected option (i.e. the one corresponding to the current value of
1064 * hibernation_mode) is enclosed by a square bracket.
1066 * To select a given hibernation mode it is necessary to write the mode's
1067 * string representation (as returned by reading from /sys/power/disk) back
1068 * into /sys/power/disk.
1071 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
1077 if (!hibernation_available())
1078 return sprintf(buf, "[disabled]\n");
1080 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1081 if (!hibernation_modes[i])
1084 case HIBERNATION_SHUTDOWN:
1085 case HIBERNATION_REBOOT:
1086 #ifdef CONFIG_SUSPEND
1087 case HIBERNATION_SUSPEND:
1089 case HIBERNATION_TEST_RESUME:
1091 case HIBERNATION_PLATFORM:
1092 if (hibernation_ops)
1094 /* not a valid mode, continue with loop */
1097 if (i == hibernation_mode)
1098 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
1100 buf += sprintf(buf, "%s ", hibernation_modes[i]);
1102 buf += sprintf(buf, "\n");
1106 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
1107 const char *buf, size_t n)
1109 int mode = HIBERNATION_INVALID;
1110 unsigned int sleep_flags;
1116 if (!hibernation_available())
1119 p = memchr(buf, '\n', n);
1120 len = p ? p - buf : n;
1122 sleep_flags = lock_system_sleep();
1123 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1124 if (len == strlen(hibernation_modes[i])
1125 && !strncmp(buf, hibernation_modes[i], len)) {
1130 if (mode != HIBERNATION_INVALID) {
1132 case HIBERNATION_SHUTDOWN:
1133 case HIBERNATION_REBOOT:
1134 #ifdef CONFIG_SUSPEND
1135 case HIBERNATION_SUSPEND:
1137 case HIBERNATION_TEST_RESUME:
1138 hibernation_mode = mode;
1140 case HIBERNATION_PLATFORM:
1141 if (hibernation_ops)
1142 hibernation_mode = mode;
1150 pm_pr_dbg("Hibernation mode set to '%s'\n",
1151 hibernation_modes[mode]);
1152 unlock_system_sleep(sleep_flags);
1153 return error ? error : n;
1158 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1161 return sprintf(buf, "%d:%d\n", MAJOR(swsusp_resume_device),
1162 MINOR(swsusp_resume_device));
1165 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1166 const char *buf, size_t n)
1168 unsigned int sleep_flags;
1174 if (!hibernation_available())
1177 if (len && buf[len-1] == '\n')
1179 name = kstrndup(buf, len, GFP_KERNEL);
1183 error = lookup_bdev(name, &dev);
1185 unsigned maj, min, offset;
1189 if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
1190 sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset,
1192 dev = MKDEV(maj, min);
1193 if (maj != MAJOR(dev) || min != MINOR(dev))
1196 dev = new_decode_dev(simple_strtoul(name, &p, 16));
1205 sleep_flags = lock_system_sleep();
1206 swsusp_resume_device = dev;
1207 unlock_system_sleep(sleep_flags);
1209 pm_pr_dbg("Configured hibernation resume from disk to %u\n",
1210 swsusp_resume_device);
1218 static ssize_t resume_offset_show(struct kobject *kobj,
1219 struct kobj_attribute *attr, char *buf)
1221 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1224 static ssize_t resume_offset_store(struct kobject *kobj,
1225 struct kobj_attribute *attr, const char *buf,
1228 unsigned long long offset;
1231 rc = kstrtoull(buf, 0, &offset);
1234 swsusp_resume_block = offset;
1239 power_attr(resume_offset);
1241 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1244 return sprintf(buf, "%lu\n", image_size);
1247 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1248 const char *buf, size_t n)
1252 if (sscanf(buf, "%lu", &size) == 1) {
1260 power_attr(image_size);
1262 static ssize_t reserved_size_show(struct kobject *kobj,
1263 struct kobj_attribute *attr, char *buf)
1265 return sprintf(buf, "%lu\n", reserved_size);
1268 static ssize_t reserved_size_store(struct kobject *kobj,
1269 struct kobj_attribute *attr,
1270 const char *buf, size_t n)
1274 if (sscanf(buf, "%lu", &size) == 1) {
1275 reserved_size = size;
1282 power_attr(reserved_size);
1284 static struct attribute *g[] = {
1286 &resume_offset_attr.attr,
1288 &image_size_attr.attr,
1289 &reserved_size_attr.attr,
1294 static const struct attribute_group attr_group = {
1299 static int __init pm_disk_init(void)
1301 return sysfs_create_group(power_kobj, &attr_group);
1304 core_initcall(pm_disk_init);
1307 static int __init resume_setup(char *str)
1312 strncpy(resume_file, str, 255);
1316 static int __init resume_offset_setup(char *str)
1318 unsigned long long offset;
1323 if (sscanf(str, "%llu", &offset) == 1)
1324 swsusp_resume_block = offset;
1329 static int __init hibernate_setup(char *str)
1331 if (!strncmp(str, "noresume", 8)) {
1333 } else if (!strncmp(str, "nocompress", 10)) {
1335 } else if (!strncmp(str, "no", 2)) {
1338 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
1339 && !strncmp(str, "protect_image", 13)) {
1340 enable_restore_image_protection();
1345 static int __init noresume_setup(char *str)
1351 static int __init resumewait_setup(char *str)
1357 static int __init resumedelay_setup(char *str)
1359 int rc = kstrtouint(str, 0, &resume_delay);
1362 pr_warn("resumedelay: bad option string '%s'\n", str);
1366 static int __init nohibernate_setup(char *str)
1373 __setup("noresume", noresume_setup);
1374 __setup("resume_offset=", resume_offset_setup);
1375 __setup("resume=", resume_setup);
1376 __setup("hibernate=", hibernate_setup);
1377 __setup("resumewait", resumewait_setup);
1378 __setup("resumedelay=", resumedelay_setup);
1379 __setup("nohibernate", nohibernate_setup);