2 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
7 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
8 * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
10 * This file is released under the GPLv2.
13 #define pr_fmt(fmt) "PM: " fmt
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/genhd.h>
33 #include <linux/ktime.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
;
68 static const struct platform_hibernation_ops
*hibernation_ops
;
70 bool hibernation_available(void)
72 return (nohibernate
== 0);
76 * hibernation_set_ops - Set the global hibernate operations.
77 * @ops: Hibernation operations to use in subsequent hibernation transitions.
79 void hibernation_set_ops(const struct platform_hibernation_ops
*ops
)
81 if (ops
&& !(ops
->begin
&& ops
->end
&& ops
->pre_snapshot
82 && ops
->prepare
&& ops
->finish
&& ops
->enter
&& ops
->pre_restore
83 && ops
->restore_cleanup
&& ops
->leave
)) {
88 hibernation_ops
= ops
;
90 hibernation_mode
= HIBERNATION_PLATFORM
;
91 else if (hibernation_mode
== HIBERNATION_PLATFORM
)
92 hibernation_mode
= HIBERNATION_SHUTDOWN
;
94 unlock_system_sleep();
96 EXPORT_SYMBOL_GPL(hibernation_set_ops
);
98 static bool entering_platform_hibernation
;
100 bool system_entering_hibernation(void)
102 return entering_platform_hibernation
;
104 EXPORT_SYMBOL(system_entering_hibernation
);
106 #ifdef CONFIG_PM_DEBUG
107 static void hibernation_debug_sleep(void)
109 pr_info("hibernation debug: Waiting for 5 seconds.\n");
113 static int hibernation_test(int level
)
115 if (pm_test_level
== level
) {
116 hibernation_debug_sleep();
121 #else /* !CONFIG_PM_DEBUG */
122 static int hibernation_test(int level
) { return 0; }
123 #endif /* !CONFIG_PM_DEBUG */
126 * platform_begin - Call platform to start hibernation.
127 * @platform_mode: Whether or not to use the platform driver.
129 static int platform_begin(int platform_mode
)
131 return (platform_mode
&& hibernation_ops
) ?
132 hibernation_ops
->begin() : 0;
136 * platform_end - Call platform to finish transition to the working state.
137 * @platform_mode: Whether or not to use the platform driver.
139 static void platform_end(int platform_mode
)
141 if (platform_mode
&& hibernation_ops
)
142 hibernation_ops
->end();
146 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
147 * @platform_mode: Whether or not to use the platform driver.
149 * Use the platform driver to prepare the system for creating a hibernate image,
150 * if so configured, and return an error code if that fails.
153 static int platform_pre_snapshot(int platform_mode
)
155 return (platform_mode
&& hibernation_ops
) ?
156 hibernation_ops
->pre_snapshot() : 0;
160 * platform_leave - Call platform to prepare a transition to the working state.
161 * @platform_mode: Whether or not to use the platform driver.
163 * Use the platform driver prepare to prepare the machine for switching to the
164 * normal mode of operation.
166 * This routine is called on one CPU with interrupts disabled.
168 static void platform_leave(int platform_mode
)
170 if (platform_mode
&& hibernation_ops
)
171 hibernation_ops
->leave();
175 * platform_finish - Call platform to switch the system to the working state.
176 * @platform_mode: Whether or not to use the platform driver.
178 * Use the platform driver to switch the machine to the normal mode of
181 * This routine must be called after platform_prepare().
183 static void platform_finish(int platform_mode
)
185 if (platform_mode
&& hibernation_ops
)
186 hibernation_ops
->finish();
190 * platform_pre_restore - Prepare for hibernate image restoration.
191 * @platform_mode: Whether or not to use the platform driver.
193 * Use the platform driver to prepare the system for resume from a hibernation
196 * If the restore fails after this function has been called,
197 * platform_restore_cleanup() must be called.
199 static int platform_pre_restore(int platform_mode
)
201 return (platform_mode
&& hibernation_ops
) ?
202 hibernation_ops
->pre_restore() : 0;
206 * platform_restore_cleanup - Switch to the working state after failing restore.
207 * @platform_mode: Whether or not to use the platform driver.
209 * Use the platform driver to switch the system to the normal mode of operation
210 * after a failing restore.
212 * If platform_pre_restore() has been called before the failing restore, this
213 * function must be called too, regardless of the result of
214 * platform_pre_restore().
216 static void platform_restore_cleanup(int platform_mode
)
218 if (platform_mode
&& hibernation_ops
)
219 hibernation_ops
->restore_cleanup();
223 * platform_recover - Recover from a failure to suspend devices.
224 * @platform_mode: Whether or not to use the platform driver.
226 static void platform_recover(int platform_mode
)
228 if (platform_mode
&& hibernation_ops
&& hibernation_ops
->recover
)
229 hibernation_ops
->recover();
233 * swsusp_show_speed - Print time elapsed between two events during hibernation.
234 * @start: Starting event.
235 * @stop: Final event.
236 * @nr_pages: Number of memory pages processed between @start and @stop.
237 * @msg: Additional diagnostic message to print.
239 void swsusp_show_speed(ktime_t start
, ktime_t stop
,
240 unsigned nr_pages
, char *msg
)
243 u64 elapsed_centisecs64
;
244 unsigned int centisecs
;
248 diff
= ktime_sub(stop
, start
);
249 elapsed_centisecs64
= ktime_divns(diff
, 10*NSEC_PER_MSEC
);
250 centisecs
= elapsed_centisecs64
;
252 centisecs
= 1; /* avoid div-by-zero */
253 k
= nr_pages
* (PAGE_SIZE
/ 1024);
254 kps
= (k
* 100) / centisecs
;
255 pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
256 msg
, k
, centisecs
/ 100, centisecs
% 100, kps
/ 1000,
261 * create_image - Create a hibernation image.
262 * @platform_mode: Whether or not to use the platform driver.
264 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
265 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
267 * Control reappears in this routine after the subsequent restore.
269 static int create_image(int platform_mode
)
273 error
= dpm_suspend_end(PMSG_FREEZE
);
275 pr_err("Some devices failed to power down, aborting hibernation\n");
279 error
= platform_pre_snapshot(platform_mode
);
280 if (error
|| hibernation_test(TEST_PLATFORM
))
281 goto Platform_finish
;
283 error
= suspend_disable_secondary_cpus();
284 if (error
|| hibernation_test(TEST_CPUS
))
289 system_state
= SYSTEM_SUSPEND
;
291 error
= syscore_suspend();
293 pr_err("Some system devices failed to power down, aborting hibernation\n");
297 if (hibernation_test(TEST_CORE
) || pm_wakeup_pending())
301 save_processor_state();
302 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE
, true);
303 error
= swsusp_arch_suspend();
304 /* Restore control flow magically appears here */
305 restore_processor_state();
306 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE
, false);
308 pr_err("Error %d creating hibernation image\n", error
);
311 events_check_enabled
= false;
315 platform_leave(platform_mode
);
321 system_state
= SYSTEM_RUNNING
;
325 suspend_enable_secondary_cpus();
328 platform_finish(platform_mode
);
330 dpm_resume_start(in_suspend
?
331 (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
);
337 * hibernation_snapshot - Quiesce devices and create a hibernation image.
338 * @platform_mode: If set, use platform driver to prepare for the transition.
340 * This routine must be called with system_transition_mutex held.
342 int hibernation_snapshot(int platform_mode
)
347 pm_suspend_clear_flags();
348 error
= platform_begin(platform_mode
);
352 /* Preallocate image memory before shutting down devices. */
353 error
= hibernate_preallocate_memory();
357 error
= freeze_kernel_threads();
361 if (hibernation_test(TEST_FREEZER
)) {
364 * Indicate to the caller that we are returning due to a
365 * successful freezer test.
367 freezer_test_done
= true;
371 error
= dpm_prepare(PMSG_FREEZE
);
373 dpm_complete(PMSG_RECOVER
);
378 pm_restrict_gfp_mask();
380 error
= dpm_suspend(PMSG_FREEZE
);
382 if (error
|| hibernation_test(TEST_DEVICES
))
383 platform_recover(platform_mode
);
385 error
= create_image(platform_mode
);
388 * In the case that we call create_image() above, the control
389 * returns here (1) after the image has been created or the
390 * image creation has failed and (2) after a successful restore.
393 /* We may need to release the preallocated image pages here. */
394 if (error
|| !in_suspend
)
397 msg
= in_suspend
? (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
;
400 if (error
|| !in_suspend
)
401 pm_restore_gfp_mask();
407 platform_end(platform_mode
);
411 thaw_kernel_threads();
417 int __weak
hibernate_resume_nonboot_cpu_disable(void)
419 return suspend_disable_secondary_cpus();
423 * resume_target_kernel - Restore system state from a hibernation image.
424 * @platform_mode: Whether or not to use the platform driver.
426 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
427 * contents of highmem that have not been restored yet from the image and run
428 * the low-level code that will restore the remaining contents of memory and
429 * switch to the just restored target kernel.
431 static int resume_target_kernel(bool platform_mode
)
435 error
= dpm_suspend_end(PMSG_QUIESCE
);
437 pr_err("Some devices failed to power down, aborting resume\n");
441 error
= platform_pre_restore(platform_mode
);
445 error
= hibernate_resume_nonboot_cpu_disable();
450 system_state
= SYSTEM_SUSPEND
;
452 error
= syscore_suspend();
456 save_processor_state();
457 error
= restore_highmem();
459 error
= swsusp_arch_resume();
461 * The code below is only ever reached in case of a failure.
462 * Otherwise, execution continues at the place where
463 * swsusp_arch_suspend() was called.
467 * This call to restore_highmem() reverts the changes made by
473 * The only reason why swsusp_arch_resume() can fail is memory being
474 * very tight, so we have to free it as soon as we can to avoid
475 * subsequent failures.
478 restore_processor_state();
479 touch_softlockup_watchdog();
484 system_state
= SYSTEM_RUNNING
;
488 suspend_enable_secondary_cpus();
491 platform_restore_cleanup(platform_mode
);
493 dpm_resume_start(PMSG_RECOVER
);
499 * hibernation_restore - Quiesce devices and restore from a hibernation image.
500 * @platform_mode: If set, use platform driver to prepare for the transition.
502 * This routine must be called with system_transition_mutex held. If it is
503 * successful, control reappears in the restored target kernel in
504 * hibernation_snapshot().
506 int hibernation_restore(int platform_mode
)
510 pm_prepare_console();
512 pm_restrict_gfp_mask();
513 error
= dpm_suspend_start(PMSG_QUIESCE
);
515 error
= resume_target_kernel(platform_mode
);
517 * The above should either succeed and jump to the new kernel,
518 * or return with an error. Otherwise things are just
519 * undefined, so let's be paranoid.
523 dpm_resume_end(PMSG_RECOVER
);
524 pm_restore_gfp_mask();
526 pm_restore_console();
531 * hibernation_platform_enter - Power off the system using the platform driver.
533 int hibernation_platform_enter(void)
537 if (!hibernation_ops
)
541 * We have cancelled the power transition by running
542 * hibernation_ops->finish() before saving the image, so we should let
543 * the firmware know that we're going to enter the sleep state after all
545 error
= hibernation_ops
->begin();
549 entering_platform_hibernation
= true;
551 error
= dpm_suspend_start(PMSG_HIBERNATE
);
553 if (hibernation_ops
->recover
)
554 hibernation_ops
->recover();
558 error
= dpm_suspend_end(PMSG_HIBERNATE
);
562 error
= hibernation_ops
->prepare();
564 goto Platform_finish
;
566 error
= suspend_disable_secondary_cpus();
571 system_state
= SYSTEM_SUSPEND
;
573 if (pm_wakeup_pending()) {
578 hibernation_ops
->enter();
579 /* We should never get here */
584 system_state
= SYSTEM_RUNNING
;
588 suspend_enable_secondary_cpus();
591 hibernation_ops
->finish();
593 dpm_resume_start(PMSG_RESTORE
);
596 entering_platform_hibernation
= false;
597 dpm_resume_end(PMSG_RESTORE
);
601 hibernation_ops
->end();
607 * power_down - Shut the machine down for hibernation.
609 * Use the platform driver, if configured, to put the system into the sleep
610 * state corresponding to hibernation, or try to power it off or reboot,
611 * depending on the value of hibernation_mode.
613 static void power_down(void)
615 #ifdef CONFIG_SUSPEND
618 if (hibernation_mode
== HIBERNATION_SUSPEND
) {
619 error
= suspend_devices_and_enter(PM_SUSPEND_MEM
);
621 hibernation_mode
= hibernation_ops
?
622 HIBERNATION_PLATFORM
:
623 HIBERNATION_SHUTDOWN
;
625 /* Restore swap signature. */
626 error
= swsusp_unmark();
628 pr_err("Swap will be unusable! Try swapon -a.\n");
635 switch (hibernation_mode
) {
636 case HIBERNATION_REBOOT
:
637 kernel_restart(NULL
);
639 case HIBERNATION_PLATFORM
:
640 hibernation_platform_enter();
642 case HIBERNATION_SHUTDOWN
:
649 * Valid image is on the disk, if we continue we risk serious data
650 * corruption after resume.
652 pr_crit("Power down manually\n");
657 static int load_image_and_restore(void)
662 pm_pr_dbg("Loading hibernation image.\n");
664 lock_device_hotplug();
665 error
= create_basic_memory_bitmaps();
669 error
= swsusp_read(&flags
);
670 swsusp_close(FMODE_READ
);
672 hibernation_restore(flags
& SF_PLATFORM_MODE
);
674 pr_err("Failed to load hibernation image, recovering.\n");
676 free_basic_memory_bitmaps();
678 unlock_device_hotplug();
684 * hibernate - Carry out system hibernation, including saving the image.
688 int error
, nr_calls
= 0;
689 bool snapshot_test
= false;
691 if (!hibernation_available()) {
692 pm_pr_dbg("Hibernation not available.\n");
697 /* The snapshot device should not be opened while we're running */
698 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
703 pr_info("hibernation entry\n");
704 pm_prepare_console();
705 error
= __pm_notifier_call_chain(PM_HIBERNATION_PREPARE
, -1, &nr_calls
);
713 error
= freeze_processes();
717 lock_device_hotplug();
718 /* Allocate memory management structures */
719 error
= create_basic_memory_bitmaps();
723 error
= hibernation_snapshot(hibernation_mode
== HIBERNATION_PLATFORM
);
724 if (error
|| freezer_test_done
)
728 unsigned int flags
= 0;
730 if (hibernation_mode
== HIBERNATION_PLATFORM
)
731 flags
|= SF_PLATFORM_MODE
;
733 flags
|= SF_NOCOMPRESS_MODE
;
735 flags
|= SF_CRC32_MODE
;
737 pm_pr_dbg("Writing image.\n");
738 error
= swsusp_write(flags
);
741 if (hibernation_mode
== HIBERNATION_TEST_RESUME
)
742 snapshot_test
= true;
747 pm_restore_gfp_mask();
749 pm_pr_dbg("Image restored successfully.\n");
753 free_basic_memory_bitmaps();
755 unlock_device_hotplug();
757 pm_pr_dbg("Checking hibernation image\n");
758 error
= swsusp_check();
760 error
= load_image_and_restore();
764 /* Don't bother checking whether freezer_test_done is true */
765 freezer_test_done
= false;
767 __pm_notifier_call_chain(PM_POST_HIBERNATION
, nr_calls
, NULL
);
768 pm_restore_console();
769 atomic_inc(&snapshot_device_available
);
771 unlock_system_sleep();
772 pr_info("hibernation exit\n");
779 * software_resume - Resume from a saved hibernation image.
781 * This routine is called as a late initcall, when all devices have been
782 * discovered and initialized already.
784 * The image reading code is called to see if there is a hibernation image
785 * available for reading. If that is the case, devices are quiesced and the
786 * contents of memory is restored from the saved image.
788 * If this is successful, control reappears in the restored target kernel in
789 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
790 * attempts to recover gracefully and make the kernel return to the normal mode
793 static int software_resume(void)
795 int error
, nr_calls
= 0;
798 * If the user said "noresume".. bail out early.
800 if (noresume
|| !hibernation_available())
804 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
805 * is configured into the kernel. Since the regular hibernate
806 * trigger path is via sysfs which takes a buffer mutex before
807 * calling hibernate functions (which take system_transition_mutex)
808 * this can cause lockdep to complain about a possible ABBA deadlock
809 * which cannot happen since we're in the boot code here and
810 * sysfs can't be invoked yet. Therefore, we use a subclass
811 * here to avoid lockdep complaining.
813 mutex_lock_nested(&system_transition_mutex
, SINGLE_DEPTH_NESTING
);
815 if (swsusp_resume_device
)
818 if (!strlen(resume_file
)) {
823 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file
);
826 pr_info("Waiting %dsec before reading resume device ...\n",
828 ssleep(resume_delay
);
831 /* Check if the device is there */
832 swsusp_resume_device
= name_to_dev_t(resume_file
);
835 * name_to_dev_t is ineffective to verify parition if resume_file is in
836 * integer format. (e.g. major:minor)
838 if (isdigit(resume_file
[0]) && resume_wait
) {
840 while (!get_gendisk(swsusp_resume_device
, &partno
))
844 if (!swsusp_resume_device
) {
846 * Some device discovery might still be in progress; we need
847 * to wait for this to finish.
849 wait_for_device_probe();
852 while ((swsusp_resume_device
= name_to_dev_t(resume_file
)) == 0)
854 async_synchronize_full();
857 swsusp_resume_device
= name_to_dev_t(resume_file
);
858 if (!swsusp_resume_device
) {
865 pm_pr_dbg("Hibernation image partition %d:%d present\n",
866 MAJOR(swsusp_resume_device
), MINOR(swsusp_resume_device
));
868 pm_pr_dbg("Looking for hibernation image.\n");
869 error
= swsusp_check();
873 /* The snapshot device should not be opened while we're running */
874 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
876 swsusp_close(FMODE_READ
);
880 pr_info("resume from hibernation\n");
881 pm_prepare_console();
882 error
= __pm_notifier_call_chain(PM_RESTORE_PREPARE
, -1, &nr_calls
);
888 pm_pr_dbg("Preparing processes for restore.\n");
889 error
= freeze_processes();
892 error
= load_image_and_restore();
895 __pm_notifier_call_chain(PM_POST_RESTORE
, nr_calls
, NULL
);
896 pm_restore_console();
897 pr_info("resume from hibernation failed (%d)\n", error
);
898 atomic_inc(&snapshot_device_available
);
899 /* For success case, the suspend path will release the lock */
901 mutex_unlock(&system_transition_mutex
);
902 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
905 swsusp_close(FMODE_READ
);
909 late_initcall_sync(software_resume
);
912 static const char * const hibernation_modes
[] = {
913 [HIBERNATION_PLATFORM
] = "platform",
914 [HIBERNATION_SHUTDOWN
] = "shutdown",
915 [HIBERNATION_REBOOT
] = "reboot",
916 #ifdef CONFIG_SUSPEND
917 [HIBERNATION_SUSPEND
] = "suspend",
919 [HIBERNATION_TEST_RESUME
] = "test_resume",
923 * /sys/power/disk - Control hibernation mode.
925 * Hibernation can be handled in several ways. There are a few different ways
926 * to put the system into the sleep state: using the platform driver (e.g. ACPI
927 * or other hibernation_ops), powering it off or rebooting it (for testing
930 * The sysfs file /sys/power/disk provides an interface for selecting the
931 * hibernation mode to use. Reading from this file causes the available modes
932 * to be printed. There are 3 modes that can be supported:
938 * If a platform hibernation driver is in use, 'platform' will be supported
939 * and will be used by default. Otherwise, 'shutdown' will be used by default.
940 * The selected option (i.e. the one corresponding to the current value of
941 * hibernation_mode) is enclosed by a square bracket.
943 * To select a given hibernation mode it is necessary to write the mode's
944 * string representation (as returned by reading from /sys/power/disk) back
945 * into /sys/power/disk.
948 static ssize_t
disk_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
954 if (!hibernation_available())
955 return sprintf(buf
, "[disabled]\n");
957 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
958 if (!hibernation_modes
[i
])
961 case HIBERNATION_SHUTDOWN
:
962 case HIBERNATION_REBOOT
:
963 #ifdef CONFIG_SUSPEND
964 case HIBERNATION_SUSPEND
:
966 case HIBERNATION_TEST_RESUME
:
968 case HIBERNATION_PLATFORM
:
971 /* not a valid mode, continue with loop */
974 if (i
== hibernation_mode
)
975 buf
+= sprintf(buf
, "[%s] ", hibernation_modes
[i
]);
977 buf
+= sprintf(buf
, "%s ", hibernation_modes
[i
]);
979 buf
+= sprintf(buf
, "\n");
983 static ssize_t
disk_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
984 const char *buf
, size_t n
)
990 int mode
= HIBERNATION_INVALID
;
992 if (!hibernation_available())
995 p
= memchr(buf
, '\n', n
);
996 len
= p
? p
- buf
: n
;
999 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
1000 if (len
== strlen(hibernation_modes
[i
])
1001 && !strncmp(buf
, hibernation_modes
[i
], len
)) {
1006 if (mode
!= HIBERNATION_INVALID
) {
1008 case HIBERNATION_SHUTDOWN
:
1009 case HIBERNATION_REBOOT
:
1010 #ifdef CONFIG_SUSPEND
1011 case HIBERNATION_SUSPEND
:
1013 case HIBERNATION_TEST_RESUME
:
1014 hibernation_mode
= mode
;
1016 case HIBERNATION_PLATFORM
:
1017 if (hibernation_ops
)
1018 hibernation_mode
= mode
;
1026 pm_pr_dbg("Hibernation mode set to '%s'\n",
1027 hibernation_modes
[mode
]);
1028 unlock_system_sleep();
1029 return error
? error
: n
;
1034 static ssize_t
resume_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1037 return sprintf(buf
,"%d:%d\n", MAJOR(swsusp_resume_device
),
1038 MINOR(swsusp_resume_device
));
1041 static ssize_t
resume_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1042 const char *buf
, size_t n
)
1048 if (len
&& buf
[len
-1] == '\n')
1050 name
= kstrndup(buf
, len
, GFP_KERNEL
);
1054 res
= name_to_dev_t(name
);
1059 lock_system_sleep();
1060 swsusp_resume_device
= res
;
1061 unlock_system_sleep();
1062 pm_pr_dbg("Configured resume from disk to %u\n", swsusp_resume_device
);
1070 static ssize_t
resume_offset_show(struct kobject
*kobj
,
1071 struct kobj_attribute
*attr
, char *buf
)
1073 return sprintf(buf
, "%llu\n", (unsigned long long)swsusp_resume_block
);
1076 static ssize_t
resume_offset_store(struct kobject
*kobj
,
1077 struct kobj_attribute
*attr
, const char *buf
,
1080 unsigned long long offset
;
1083 rc
= kstrtoull(buf
, 0, &offset
);
1086 swsusp_resume_block
= offset
;
1091 power_attr(resume_offset
);
1093 static ssize_t
image_size_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1096 return sprintf(buf
, "%lu\n", image_size
);
1099 static ssize_t
image_size_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1100 const char *buf
, size_t n
)
1104 if (sscanf(buf
, "%lu", &size
) == 1) {
1112 power_attr(image_size
);
1114 static ssize_t
reserved_size_show(struct kobject
*kobj
,
1115 struct kobj_attribute
*attr
, char *buf
)
1117 return sprintf(buf
, "%lu\n", reserved_size
);
1120 static ssize_t
reserved_size_store(struct kobject
*kobj
,
1121 struct kobj_attribute
*attr
,
1122 const char *buf
, size_t n
)
1126 if (sscanf(buf
, "%lu", &size
) == 1) {
1127 reserved_size
= size
;
1134 power_attr(reserved_size
);
1136 static struct attribute
* g
[] = {
1138 &resume_offset_attr
.attr
,
1140 &image_size_attr
.attr
,
1141 &reserved_size_attr
.attr
,
1146 static const struct attribute_group attr_group
= {
1151 static int __init
pm_disk_init(void)
1153 return sysfs_create_group(power_kobj
, &attr_group
);
1156 core_initcall(pm_disk_init
);
1159 static int __init
resume_setup(char *str
)
1164 strncpy( resume_file
, str
, 255 );
1168 static int __init
resume_offset_setup(char *str
)
1170 unsigned long long offset
;
1175 if (sscanf(str
, "%llu", &offset
) == 1)
1176 swsusp_resume_block
= offset
;
1181 static int __init
hibernate_setup(char *str
)
1183 if (!strncmp(str
, "noresume", 8)) {
1185 } else if (!strncmp(str
, "nocompress", 10)) {
1187 } else if (!strncmp(str
, "no", 2)) {
1190 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX
)
1191 && !strncmp(str
, "protect_image", 13)) {
1192 enable_restore_image_protection();
1197 static int __init
noresume_setup(char *str
)
1203 static int __init
resumewait_setup(char *str
)
1209 static int __init
resumedelay_setup(char *str
)
1211 int rc
= kstrtouint(str
, 0, &resume_delay
);
1218 static int __init
nohibernate_setup(char *str
)
1225 __setup("noresume", noresume_setup
);
1226 __setup("resume_offset=", resume_offset_setup
);
1227 __setup("resume=", resume_setup
);
1228 __setup("hibernate=", hibernate_setup
);
1229 __setup("resumewait", resumewait_setup
);
1230 __setup("resumedelay=", resumedelay_setup
);
1231 __setup("nohibernate", nohibernate_setup
);