blob: 079160e22bcc0d1bf24be8b07bd93e5b2265970c [file] [log] [blame]
Pavel Machekd7ae79c2005-09-06 15:16:21 -07001Some warnings, first.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
3 * BIG FAT WARNING *********************************************************
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * If you touch anything on disk between suspend and resume...
6 * ...kiss your data goodbye.
7 *
Pavel Machekd7ae79c2005-09-06 15:16:21 -07008 * If you do resume from initrd after your filesystems are mounted...
9 * ...bye bye root partition.
10 * [this is actually same case as above]
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Pavel Machekd7ae79c2005-09-06 15:16:21 -070012 * If you have unsupported (*) devices using DMA, you may have some
13 * problems. If your disk driver does not support suspend... (IDE does),
14 * it may cause some problems, too. If you change kernel command line
15 * between suspend and resume, it may do something wrong. If you change
16 * your hardware while system is suspended... well, it was not good idea;
17 * but it will probably only crash.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
19 * (*) suspend/resume support is needed to make it safe.
Pavel Machek543cc272006-03-23 03:00:02 -080020 *
David Brownellb9827e42006-05-16 17:33:14 -070021 * If you have any filesystems on USB devices mounted before software suspend,
Pavel Machek543cc272006-03-23 03:00:02 -080022 * they won't be accessible after resume and you may lose data, as though
David Brownellb9827e42006-05-16 17:33:14 -070023 * you have unplugged the USB devices with mounted filesystems on them;
24 * see the FAQ below for details. (This is not true for more traditional
25 * power states like "standby", which normally don't turn USB off.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27You need to append resume=/dev/your_swap_partition to kernel command
28line. Then you suspend by
29
30echo shutdown > /sys/power/disk; echo disk > /sys/power/state
31
32. If you feel ACPI works pretty well on your system, you might try
33
34echo platform > /sys/power/disk; echo disk > /sys/power/state
35
Bojan Smojver62c552c2012-06-16 00:09:58 +020036. If you would like to write hibernation image to swap and then suspend
37to RAM (provided your platform supports it), you can try
38
39echo suspend > /sys/power/disk; echo disk > /sys/power/state
40
Pavel Machek543cc272006-03-23 03:00:02 -080041. If you have SATA disks, you'll need recent kernels with SATA suspend
42support. For suspend and resume to work, make sure your disk drivers
43are built into kernel -- not modules. [There's way to make
44suspend/resume with modular disk drivers, see FAQ, but you probably
45should not do that.]
46
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080047If you want to limit the suspend image size to N bytes, do
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080048
49echo N > /sys/power/image_size
50
51before suspend (it is limited to 500 MB by default).
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Sebastian Capella1c936692013-08-26 14:54:49 -070053. The resume process checks for the presence of the resume device,
54if found, it then checks the contents for the hibernation image signature.
55If both are found, it resumes the hibernation image.
56
57. The resume process may be triggered in two ways:
58 1) During lateinit: If resume=/dev/your_swap_partition is specified on
59 the kernel command line, lateinit runs the resume process. If the
60 resume device has not been probed yet, the resume process fails and
61 bootup continues.
62 2) Manually from an initrd or initramfs: May be run from
63 the init script by using the /sys/power/resume file. It is vital
64 that this be done prior to remounting any filesystems (even as
65 read-only) otherwise data may be corrupted.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67Article about goals and implementation of Software Suspend for Linux
68~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Oskar Schirmer1557cc42012-08-21 09:54:09 +000069Author: Gábor Kuti
Linus Torvalds1da177e2005-04-16 15:20:36 -070070Last revised: 2003-10-20 by Pavel Machek
71
72Idea and goals to achieve
73
74Nowadays it is common in several laptops that they have a suspend button. It
75saves the state of the machine to a filesystem or to a partition and switches
76to standby mode. Later resuming the machine the saved state is loaded back to
77ram and the machine can continue its work. It has two real benefits. First we
78save ourselves the time machine goes down and later boots up, energy costs
79are real high when running from batteries. The other gain is that we don't have to
80interrupt our programs so processes that are calculating something for a long
81time shouldn't need to be written interruptible.
82
83swsusp saves the state of the machine into active swaps and then reboots or
84powerdowns. You must explicitly specify the swap partition to resume from with
85``resume='' kernel option. If signature is found it loads and restores saved
86state. If the option ``noresume'' is specified as a boot parameter, it skips
Bojan Smojverf996fc92010-09-09 23:06:23 +020087the resuming. If the option ``hibernate=nocompress'' is specified as a boot
88parameter, it saves hibernation image without compression.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90In the meantime while the system is suspended you should not add/remove any
91of the hardware, write to the filesystems, etc.
92
93Sleep states summary
94====================
95
96There are three different interfaces you can use, /proc/acpi should
97work like this:
98
99In a really perfect world:
100echo 1 > /proc/acpi/sleep # for standby
101echo 2 > /proc/acpi/sleep # for suspend to ram
102echo 3 > /proc/acpi/sleep # for suspend to ram, but with more power conservative
103echo 4 > /proc/acpi/sleep # for suspend to disk
104echo 5 > /proc/acpi/sleep # for shutdown unfriendly the system
105
106and perhaps
107echo 4b > /proc/acpi/sleep # for suspend to disk via s4bios
108
109Frequently Asked Questions
110==========================
111
112Q: well, suspending a server is IMHO a really stupid thing,
113but... (Diego Zuccato):
114
115A: You bought new UPS for your server. How do you install it without
116bringing machine down? Suspend to disk, rearrange power cables,
117resume.
118
119You have your server on UPS. Power died, and UPS is indicating 30
120seconds to failure. What do you do? Suspend to disk.
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123Q: Maybe I'm missing something, but why don't the regular I/O paths work?
124
125A: We do use the regular I/O paths. However we cannot restore the data
126to its original location as we load it. That would create an
127inconsistent kernel state which would certainly result in an oops.
128Instead, we load the image into unused memory and then atomically copy
129it back to it original location. This implies, of course, a maximum
130image size of half the amount of memory.
131
132There are two solutions to this:
133
134* require half of memory to be free during suspend. That way you can
135read "new" data onto free spots, then cli and copy
136
137* assume we had special "polling" ide driver that only uses memory
138between 0-640KB. That way, I'd have to make sure that 0-640KB is free
139during suspending, but otherwise it would work...
140
141suspend2 shares this fundamental limitation, but does not include user
142data and disk caches into "used memory" by saving them in
143advance. That means that the limitation goes away in practice.
144
145Q: Does linux support ACPI S4?
146
147A: Yes. That's what echo platform > /sys/power/disk does.
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149Q: What is 'suspend2'?
150
151A: suspend2 is 'Software Suspend 2', a forked implementation of
152suspend-to-disk which is available as separate patches for 2.4 and 2.6
153kernels from swsusp.sourceforge.net. It includes support for SMP, 4GB
154highmem and preemption. It also has a extensible architecture that
155allows for arbitrary transformations on the image (compression,
156encryption) and arbitrary backends for writing the image (eg to swap
157or an NFS share[Work In Progress]). Questions regarding suspend2
158should be sent to the mailing list available through the suspend2
159website, and not to the Linux Kernel Mailing List. We are working
160toward merging suspend2 into the mainline kernel.
161
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700162Q: What is the freezing of tasks and why are we using it?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700164A: The freezing of tasks is a mechanism by which user space processes and some
165kernel threads are controlled during hibernation or system-wide suspend (on some
166architectures). See freezing-of-tasks.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Johannes Berg11d77d02007-04-30 15:09:53 -0700168Q: What is the difference between "platform" and "shutdown"?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170A:
171
172shutdown: save state in linux, then tell bios to powerdown
173
174platform: save state in linux, then tell bios to powerdown and blink
175 "suspended led"
176
Johannes Berg11d77d02007-04-30 15:09:53 -0700177"platform" is actually right thing to do where supported, but
178"shutdown" is most reliable (except on ACPI systems).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180Q: I do not understand why you have such strong objections to idea of
181selective suspend.
182
Matt LaPlante2fe0ae72006-10-03 22:50:39 +0200183A: Do selective suspend during runtime power management, that's okay. But
184it's useless for suspend-to-disk. (And I do not see how you could use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185it for suspend-to-ram, I hope you do not want that).
186
187Lets see, so you suggest to
188
189* SUSPEND all but swap device and parents
190* Snapshot
191* Write image to disk
192* SUSPEND swap device and parents
193* Powerdown
194
195Oh no, that does not work, if swap device or its parents uses DMA,
196you've corrupted data. You'd have to do
197
198* SUSPEND all but swap device and parents
199* FREEZE swap device and parents
200* Snapshot
201* UNFREEZE swap device and parents
202* Write
203* SUSPEND swap device and parents
204
205Which means that you still need that FREEZE state, and you get more
206complicated code. (And I have not yet introduce details like system
207devices).
208
209Q: There don't seem to be any generally useful behavioral
210distinctions between SUSPEND and FREEZE.
211
212A: Doing SUSPEND when you are asked to do FREEZE is always correct,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300213but it may be unnecessarily slow. If you want your driver to stay simple,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214slowness may not matter to you. It can always be fixed later.
215
216For devices like disk it does matter, you do not want to spindown for
217FREEZE.
218
Matt LaPlante2fe0ae72006-10-03 22:50:39 +0200219Q: After resuming, system is paging heavily, leading to very bad interactivity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221A: Try running
222
223cat `cat /proc/[0-9]*/maps | grep / | sed 's:.* /:/:' | sort -u` > /dev/null
224
Adrian Bunka58a4142006-01-10 00:08:17 +0100225after resume. swapoff -a; swapon -a may also be useful.
Pavel Machekfc5fb2c2005-06-25 14:55:07 -0700226
227Q: What happens to devices during swsusp? They seem to be resumed
228during system suspend?
229
230A: That's correct. We need to resume them if we want to write image to
231disk. Whole sequence goes like
232
233 Suspend part
234 ~~~~~~~~~~~~
235 running system, user asks for suspend-to-disk
236
237 user processes are stopped
238
239 suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
240 with state snapshot
241
242 state snapshot: copy of whole used memory is taken with interrupts disabled
243
244 resume(): devices are woken up so that we can write image to swap
245
246 write image to swap
247
248 suspend(PMSG_SUSPEND): suspend devices so that we can power off
249
250 turn the power off
251
252 Resume part
253 ~~~~~~~~~~~
254 (is actually pretty similar)
255
256 running system, user asks for suspend-to-disk
257
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300258 user processes are stopped (in common case there are none, but with resume-from-initrd, no one knows)
Pavel Machekfc5fb2c2005-06-25 14:55:07 -0700259
260 read image from disk
261
262 suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
263 with image restoration
264
265 image restoration: rewrite memory with image
266
267 resume(): devices are woken up so that system can continue
268
269 thaw all user processes
270
271Q: What is this 'Encrypt suspend image' for?
272
273A: First of all: it is not a replacement for dm-crypt encrypted swap.
274It cannot protect your computer while it is suspended. Instead it does
275protect from leaking sensitive data after resume from suspend.
276
277Think of the following: you suspend while an application is running
278that keeps sensitive data in memory. The application itself prevents
279the data from being swapped out. Suspend, however, must write these
280data to swap to be able to resume later on. Without suspend encryption
281your sensitive data are then stored in plaintext on disk. This means
282that after resume your sensitive data are accessible to all
283applications having direct access to the swap device which was used
284for suspend. If you don't need swap after resume these data can remain
285on disk virtually forever. Thus it can happen that your system gets
286broken in weeks later and sensitive data which you thought were
287encrypted and protected are retrieved and stolen from the swap device.
288To prevent this situation you should use 'Encrypt suspend image'.
289
290During suspend a temporary key is created and this key is used to
291encrypt the data written to disk. When, during resume, the data was
292read back into memory the temporary key is destroyed which simply
293means that all data written to disk during suspend are then
294inaccessible so they can't be stolen later on. The only thing that
295you must then take care of is that you call 'mkswap' for the swap
296partition used for suspend as early as possible during regular
297boot. This asserts that any temporary key from an oopsed suspend or
298from a failed or aborted resume is erased from the swap device.
299
300As a rule of thumb use encrypted swap to protect your data while your
301system is shut down or suspended. Additionally use the encrypted
302suspend image to prevent sensitive data from being stolen after
303resume.
Pavel Machek7e958882005-09-03 15:56:56 -0700304
Rafael J. Wysockiecbd0da2006-12-06 20:34:13 -0800305Q: Can I suspend to a swap file?
Pavel Machek7e958882005-09-03 15:56:56 -0700306
Rafael J. Wysockiecbd0da2006-12-06 20:34:13 -0800307A: Generally, yes, you can. However, it requires you to use the "resume=" and
308"resume_offset=" kernel command line parameters, so the resume from a swap file
309cannot be initiated from an initrd or initramfs image. See
310swsusp-and-swap-files.txt for details.
Pavel Machekd7ae79c2005-09-06 15:16:21 -0700311
312Q: Is there a maximum system RAM size that is supported by swsusp?
313
314A: It should work okay with highmem.
315
316Q: Does swsusp (to disk) use only one swap partition or can it use
317multiple swap partitions (aggregate them into one logical space)?
318
319A: Only one swap partition, sorry.
320
321Q: If my application(s) causes lots of memory & swap space to be used
322(over half of the total system RAM), is it correct that it is likely
323to be useless to try to suspend to disk while that app is running?
324
325A: No, it should work okay, as long as your app does not mlock()
326it. Just prepare big enough swap partition.
327
Adrian Bunka58a4142006-01-10 00:08:17 +0100328Q: What information is useful for debugging suspend-to-disk problems?
Pavel Machekd7ae79c2005-09-06 15:16:21 -0700329
330A: Well, last messages on the screen are always useful. If something
331is broken, it is usually some kernel driver, therefore trying with as
332little as possible modules loaded helps a lot. I also prefer people to
333suspend from console, preferably without X running. Booting with
334init=/bin/bash, then swapon and starting suspend sequence manually
335usually does the trick. Then it is good idea to try with latest
336vanilla kernel.
337
Pavel Machek543cc272006-03-23 03:00:02 -0800338Q: How can distributions ship a swsusp-supporting kernel with modular
339disk drivers (especially SATA)?
340
341A: Well, it can be done, load the drivers, then do echo into
Sebastian Capella1c936692013-08-26 14:54:49 -0700342/sys/power/resume file from initrd. Be sure not to mount
Pavel Machek543cc272006-03-23 03:00:02 -0800343anything, not even read-only mount, or you are going to lose your
344data.
345
346Q: How do I make suspend more verbose?
347
348A: If you want to see any non-error kernel messages on the virtual
349terminal the kernel switches to during suspend, you have to set the
Pavel Macheke084dbd2006-06-23 02:04:50 -0700350kernel console loglevel to at least 4 (KERN_WARNING), for example by
351doing
Pavel Machek543cc272006-03-23 03:00:02 -0800352
Pavel Macheke084dbd2006-06-23 02:04:50 -0700353 # save the old loglevel
354 read LOGLEVEL DUMMY < /proc/sys/kernel/printk
355 # set the loglevel so we see the progress bar.
356 # if the level is higher than needed, we leave it alone.
357 if [ $LOGLEVEL -lt 5 ]; then
358 echo 5 > /proc/sys/kernel/printk
359 fi
360
361 IMG_SZ=0
362 read IMG_SZ < /sys/power/image_size
363 echo -n disk > /sys/power/state
364 RET=$?
365 #
366 # the logic here is:
367 # if image_size > 0 (without kernel support, IMG_SZ will be zero),
368 # then try again with image_size set to zero.
369 if [ $RET -ne 0 -a $IMG_SZ -ne 0 ]; then # try again with minimal image size
370 echo 0 > /sys/power/image_size
371 echo -n disk > /sys/power/state
372 RET=$?
373 fi
374
375 # restore previous loglevel
376 echo $LOGLEVEL > /proc/sys/kernel/printk
377 exit $RET
Pavel Machek543cc272006-03-23 03:00:02 -0800378
379Q: Is this true that if I have a mounted filesystem on a USB device and
380I suspend to disk, I can lose data unless the filesystem has been mounted
381with "sync"?
382
David Brownellb9827e42006-05-16 17:33:14 -0700383A: That's right ... if you disconnect that device, you may lose data.
384In fact, even with "-o sync" you can lose data if your programs have
385information in buffers they haven't written out to a disk you disconnect,
386or if you disconnect before the device finished saving data you wrote.
Pavel Machek543cc272006-03-23 03:00:02 -0800387
David Brownellb9827e42006-05-16 17:33:14 -0700388Software suspend normally powers down USB controllers, which is equivalent
389to disconnecting all USB devices attached to your system.
390
391Your system might well support low-power modes for its USB controllers
392while the system is asleep, maintaining the connection, using true sleep
393modes like "suspend-to-RAM" or "standby". (Don't write "disk" to the
394/sys/power/state file; write "standby" or "mem".) We've not seen any
395hardware that can use these modes through software suspend, although in
Johannes Berg11d77d02007-04-30 15:09:53 -0700396theory some systems might support "platform" modes that won't break the
397USB connections.
Pavel Machek543cc272006-03-23 03:00:02 -0800398
399Remember that it's always a bad idea to unplug a disk drive containing a
David Brownellb9827e42006-05-16 17:33:14 -0700400mounted filesystem. That's true even when your system is asleep! The
401safest thing is to unmount all filesystems on removable media (such USB,
402Firewire, CompactFlash, MMC, external SATA, or even IDE hotplug bays)
403before suspending; then remount them after resuming.
Pavel Machekd7ae79c2005-09-06 15:16:21 -0700404
Alan Stern0458d5b2007-05-04 11:52:20 -0400405There is a work-around for this problem. For more information, see
406Documentation/usb/persist.txt.
407
Pavel Machek23b168d2008-02-05 19:27:12 +0100408Q: Can I suspend-to-disk using a swap partition under LVM?
409
410A: No. You can suspend successfully, but you'll not be able to
411resume. uswsusp should be able to work with LVM. See suspend.sf.net.
412
Pavel Macheke084dbd2006-06-23 02:04:50 -0700413Q: I upgraded the kernel from 2.6.15 to 2.6.16. Both kernels were
414compiled with the similar configuration files. Anyway I found that
415suspend to disk (and resume) is much slower on 2.6.16 compared to
4162.6.15. Any idea for why that might happen or how can I speed it up?
417
418A: This is because the size of the suspend image is now greater than
419for 2.6.15 (by saving more data we can get more responsive system
420after resume).
421
422There's the /sys/power/image_size knob that controls the size of the
423image. If you set it to 0 (eg. by echo 0 > /sys/power/image_size as
424root), the 2.6.15 behavior should be restored. If it is still too
425slow, take a look at suspend.sf.net -- userland suspend is faster and
426supports LZF compression to speed it up further.