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/export.h>
15 #include <linux/suspend.h>
16 #include <linux/reboot.h>
17 #include <linux/string.h>
18 #include <linux/device.h>
19 #include <linux/async.h>
20 #include <linux/delay.h>
22 #include <linux/mount.h>
24 #include <linux/nmi.h>
25 #include <linux/console.h>
26 #include <linux/cpu.h>
27 #include <linux/freezer.h>
28 #include <linux/gfp.h>
29 #include <linux/syscore_ops.h>
30 #include <linux/ctype.h>
31 #include <linux/ktime.h>
32 #include <linux/security.h>
33 #include <linux/secretmem.h>
34 #include <trace/events/power.h>
39 static int nocompress;
41 static int nohibernate;
42 static int resume_wait;
43 static unsigned int resume_delay;
44 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
45 dev_t swsusp_resume_device;
46 sector_t swsusp_resume_block;
47 __visible int in_suspend __nosavedata;
57 HIBERNATION_TEST_RESUME,
59 __HIBERNATION_AFTER_LAST
61 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
62 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
64 static int hibernation_mode = HIBERNATION_SHUTDOWN;
66 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(void)
692 pm_pr_dbg("Loading hibernation image.\n");
694 lock_device_hotplug();
695 error = create_basic_memory_bitmaps();
697 swsusp_close(FMODE_READ | FMODE_EXCL);
701 error = swsusp_read(&flags);
702 swsusp_close(FMODE_READ | FMODE_EXCL);
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 unsigned int sleep_flags;
723 if (!hibernation_available()) {
724 pm_pr_dbg("Hibernation not available.\n");
728 sleep_flags = lock_system_sleep();
729 /* The snapshot device should not be opened while we're running */
730 if (!hibernate_acquire()) {
735 pr_info("hibernation entry\n");
736 pm_prepare_console();
737 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
743 error = freeze_processes();
747 /* protected by system_transition_mutex */
748 snapshot_test = false;
750 lock_device_hotplug();
751 /* Allocate memory management structures */
752 error = create_basic_memory_bitmaps();
756 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
757 if (error || freezer_test_done)
761 unsigned int flags = 0;
763 if (hibernation_mode == HIBERNATION_PLATFORM)
764 flags |= SF_PLATFORM_MODE;
766 flags |= SF_NOCOMPRESS_MODE;
768 flags |= SF_CRC32_MODE;
770 pm_pr_dbg("Writing hibernation image.\n");
771 error = swsusp_write(flags);
774 if (hibernation_mode == HIBERNATION_TEST_RESUME)
775 snapshot_test = true;
780 pm_restore_gfp_mask();
782 pm_pr_dbg("Hibernation image restored successfully.\n");
786 free_basic_memory_bitmaps();
788 unlock_device_hotplug();
790 pm_pr_dbg("Checking hibernation image\n");
791 error = swsusp_check();
793 error = load_image_and_restore();
797 /* Don't bother checking whether freezer_test_done is true */
798 freezer_test_done = false;
800 pm_notifier_call_chain(PM_POST_HIBERNATION);
802 pm_restore_console();
805 unlock_system_sleep(sleep_flags);
806 pr_info("hibernation exit\n");
812 * hibernate_quiet_exec - Execute a function with all devices frozen.
813 * @func: Function to execute.
814 * @data: Data pointer to pass to @func.
816 * Return the @func return value or an error code if it cannot be executed.
818 int hibernate_quiet_exec(int (*func)(void *data), void *data)
820 unsigned int sleep_flags;
823 sleep_flags = lock_system_sleep();
825 if (!hibernate_acquire()) {
830 pm_prepare_console();
832 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
836 error = freeze_processes();
840 lock_device_hotplug();
842 pm_suspend_clear_flags();
844 error = platform_begin(true);
848 error = freeze_kernel_threads();
852 error = dpm_prepare(PMSG_FREEZE);
858 error = dpm_suspend(PMSG_FREEZE);
862 error = dpm_suspend_end(PMSG_FREEZE);
866 error = platform_pre_snapshot(true);
873 platform_finish(true);
875 dpm_resume_start(PMSG_THAW);
878 dpm_resume(PMSG_THAW);
883 dpm_complete(PMSG_THAW);
885 thaw_kernel_threads();
890 unlock_device_hotplug();
895 pm_notifier_call_chain(PM_POST_HIBERNATION);
898 pm_restore_console();
903 unlock_system_sleep(sleep_flags);
907 EXPORT_SYMBOL_GPL(hibernate_quiet_exec);
910 * software_resume - Resume from a saved hibernation image.
912 * This routine is called as a late initcall, when all devices have been
913 * discovered and initialized already.
915 * The image reading code is called to see if there is a hibernation image
916 * available for reading. If that is the case, devices are quiesced and the
917 * contents of memory is restored from the saved image.
919 * If this is successful, control reappears in the restored target kernel in
920 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
921 * attempts to recover gracefully and make the kernel return to the normal mode
924 static int software_resume(void)
929 * If the user said "noresume".. bail out early.
931 if (noresume || !hibernation_available())
935 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
936 * is configured into the kernel. Since the regular hibernate
937 * trigger path is via sysfs which takes a buffer mutex before
938 * calling hibernate functions (which take system_transition_mutex)
939 * this can cause lockdep to complain about a possible ABBA deadlock
940 * which cannot happen since we're in the boot code here and
941 * sysfs can't be invoked yet. Therefore, we use a subclass
942 * here to avoid lockdep complaining.
944 mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
946 snapshot_test = false;
948 if (swsusp_resume_device)
951 if (!strlen(resume_file)) {
956 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
959 pr_info("Waiting %dsec before reading resume device ...\n",
961 ssleep(resume_delay);
964 /* Check if the device is there */
965 swsusp_resume_device = name_to_dev_t(resume_file);
966 if (!swsusp_resume_device) {
968 * Some device discovery might still be in progress; we need
969 * to wait for this to finish.
971 wait_for_device_probe();
974 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
976 async_synchronize_full();
979 swsusp_resume_device = name_to_dev_t(resume_file);
980 if (!swsusp_resume_device) {
987 pm_pr_dbg("Hibernation image partition %d:%d present\n",
988 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
990 pm_pr_dbg("Looking for hibernation image.\n");
991 error = swsusp_check();
995 /* The snapshot device should not be opened while we're running */
996 if (!hibernate_acquire()) {
998 swsusp_close(FMODE_READ | FMODE_EXCL);
1002 pr_info("resume from hibernation\n");
1003 pm_prepare_console();
1004 error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE);
1008 pm_pr_dbg("Preparing processes for hibernation restore.\n");
1009 error = freeze_processes();
1013 error = freeze_kernel_threads();
1019 error = load_image_and_restore();
1022 pm_notifier_call_chain(PM_POST_RESTORE);
1024 pm_restore_console();
1025 pr_info("resume failed (%d)\n", error);
1026 hibernate_release();
1027 /* For success case, the suspend path will release the lock */
1029 mutex_unlock(&system_transition_mutex);
1030 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
1033 swsusp_close(FMODE_READ | FMODE_EXCL);
1037 late_initcall_sync(software_resume);
1040 static const char * const hibernation_modes[] = {
1041 [HIBERNATION_PLATFORM] = "platform",
1042 [HIBERNATION_SHUTDOWN] = "shutdown",
1043 [HIBERNATION_REBOOT] = "reboot",
1044 #ifdef CONFIG_SUSPEND
1045 [HIBERNATION_SUSPEND] = "suspend",
1047 [HIBERNATION_TEST_RESUME] = "test_resume",
1051 * /sys/power/disk - Control hibernation mode.
1053 * Hibernation can be handled in several ways. There are a few different ways
1054 * to put the system into the sleep state: using the platform driver (e.g. ACPI
1055 * or other hibernation_ops), powering it off or rebooting it (for testing
1058 * The sysfs file /sys/power/disk provides an interface for selecting the
1059 * hibernation mode to use. Reading from this file causes the available modes
1060 * to be printed. There are 3 modes that can be supported:
1066 * If a platform hibernation driver is in use, 'platform' will be supported
1067 * and will be used by default. Otherwise, 'shutdown' will be used by default.
1068 * The selected option (i.e. the one corresponding to the current value of
1069 * hibernation_mode) is enclosed by a square bracket.
1071 * To select a given hibernation mode it is necessary to write the mode's
1072 * string representation (as returned by reading from /sys/power/disk) back
1073 * into /sys/power/disk.
1076 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
1082 if (!hibernation_available())
1083 return sprintf(buf, "[disabled]\n");
1085 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1086 if (!hibernation_modes[i])
1089 case HIBERNATION_SHUTDOWN:
1090 case HIBERNATION_REBOOT:
1091 #ifdef CONFIG_SUSPEND
1092 case HIBERNATION_SUSPEND:
1094 case HIBERNATION_TEST_RESUME:
1096 case HIBERNATION_PLATFORM:
1097 if (hibernation_ops)
1099 /* not a valid mode, continue with loop */
1102 if (i == hibernation_mode)
1103 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
1105 buf += sprintf(buf, "%s ", hibernation_modes[i]);
1107 buf += sprintf(buf, "\n");
1111 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
1112 const char *buf, size_t n)
1114 int mode = HIBERNATION_INVALID;
1115 unsigned int sleep_flags;
1121 if (!hibernation_available())
1124 p = memchr(buf, '\n', n);
1125 len = p ? p - buf : n;
1127 sleep_flags = lock_system_sleep();
1128 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1129 if (len == strlen(hibernation_modes[i])
1130 && !strncmp(buf, hibernation_modes[i], len)) {
1135 if (mode != HIBERNATION_INVALID) {
1137 case HIBERNATION_SHUTDOWN:
1138 case HIBERNATION_REBOOT:
1139 #ifdef CONFIG_SUSPEND
1140 case HIBERNATION_SUSPEND:
1142 case HIBERNATION_TEST_RESUME:
1143 hibernation_mode = mode;
1145 case HIBERNATION_PLATFORM:
1146 if (hibernation_ops)
1147 hibernation_mode = mode;
1155 pm_pr_dbg("Hibernation mode set to '%s'\n",
1156 hibernation_modes[mode]);
1157 unlock_system_sleep(sleep_flags);
1158 return error ? error : n;
1163 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1166 return sprintf(buf, "%d:%d\n", MAJOR(swsusp_resume_device),
1167 MINOR(swsusp_resume_device));
1170 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1171 const char *buf, size_t n)
1173 unsigned int sleep_flags;
1178 if (len && buf[len-1] == '\n')
1180 name = kstrndup(buf, len, GFP_KERNEL);
1184 res = name_to_dev_t(name);
1189 sleep_flags = lock_system_sleep();
1190 swsusp_resume_device = res;
1191 unlock_system_sleep(sleep_flags);
1193 pm_pr_dbg("Configured hibernation resume from disk to %u\n",
1194 swsusp_resume_device);
1202 static ssize_t resume_offset_show(struct kobject *kobj,
1203 struct kobj_attribute *attr, char *buf)
1205 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1208 static ssize_t resume_offset_store(struct kobject *kobj,
1209 struct kobj_attribute *attr, const char *buf,
1212 unsigned long long offset;
1215 rc = kstrtoull(buf, 0, &offset);
1218 swsusp_resume_block = offset;
1223 power_attr(resume_offset);
1225 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1228 return sprintf(buf, "%lu\n", image_size);
1231 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1232 const char *buf, size_t n)
1236 if (sscanf(buf, "%lu", &size) == 1) {
1244 power_attr(image_size);
1246 static ssize_t reserved_size_show(struct kobject *kobj,
1247 struct kobj_attribute *attr, char *buf)
1249 return sprintf(buf, "%lu\n", reserved_size);
1252 static ssize_t reserved_size_store(struct kobject *kobj,
1253 struct kobj_attribute *attr,
1254 const char *buf, size_t n)
1258 if (sscanf(buf, "%lu", &size) == 1) {
1259 reserved_size = size;
1266 power_attr(reserved_size);
1268 static struct attribute *g[] = {
1270 &resume_offset_attr.attr,
1272 &image_size_attr.attr,
1273 &reserved_size_attr.attr,
1278 static const struct attribute_group attr_group = {
1283 static int __init pm_disk_init(void)
1285 return sysfs_create_group(power_kobj, &attr_group);
1288 core_initcall(pm_disk_init);
1291 static int __init resume_setup(char *str)
1296 strncpy(resume_file, str, 255);
1300 static int __init resume_offset_setup(char *str)
1302 unsigned long long offset;
1307 if (sscanf(str, "%llu", &offset) == 1)
1308 swsusp_resume_block = offset;
1313 static int __init hibernate_setup(char *str)
1315 if (!strncmp(str, "noresume", 8)) {
1317 } else if (!strncmp(str, "nocompress", 10)) {
1319 } else if (!strncmp(str, "no", 2)) {
1322 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
1323 && !strncmp(str, "protect_image", 13)) {
1324 enable_restore_image_protection();
1329 static int __init noresume_setup(char *str)
1335 static int __init resumewait_setup(char *str)
1341 static int __init resumedelay_setup(char *str)
1343 int rc = kstrtouint(str, 0, &resume_delay);
1346 pr_warn("resumedelay: bad option string '%s'\n", str);
1350 static int __init nohibernate_setup(char *str)
1357 __setup("noresume", noresume_setup);
1358 __setup("resume_offset=", resume_offset_setup);
1359 __setup("resume=", resume_setup);
1360 __setup("hibernate=", hibernate_setup);
1361 __setup("resumewait", resumewait_setup);
1362 __setup("resumedelay=", resumedelay_setup);
1363 __setup("nohibernate", nohibernate_setup);