Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / Documentation / power / swsusp.txt
CommitLineData
d7ae79c7 1Some warnings, first.
1da177e4
LT
2
3 * BIG FAT WARNING *********************************************************
4 *
1da177e4
LT
5 * If you touch anything on disk between suspend and resume...
6 * ...kiss your data goodbye.
7 *
d7ae79c7
PM
8 * If you do resume from initrd after your filesystems are mounted...
9 * ...bye bye root partition.
10 * [this is actually same case as above]
1da177e4 11 *
d7ae79c7
PM
12 * 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.
1da177e4
LT
18 *
19 * (*) suspend/resume support is needed to make it safe.
543cc27d 20 *
b9827e4b 21 * If you have any filesystems on USB devices mounted before software suspend,
543cc27d 22 * they won't be accessible after resume and you may lose data, as though
b9827e4b
DB
23 * 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.)
1da177e4
LT
26
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
62c552cc
BS
36. 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
543cc27d
PM
41. 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
853609b6 47If you want to limit the suspend image size to N bytes, do
ca0aec0f
RW
48
49echo N > /sys/power/image_size
50
51before suspend (it is limited to 500 MB by default).
1da177e4
LT
52
53
54Article about goals and implementation of Software Suspend for Linux
55~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1557cc42 56Author: Gábor Kuti
1da177e4
LT
57Last revised: 2003-10-20 by Pavel Machek
58
59Idea and goals to achieve
60
61Nowadays it is common in several laptops that they have a suspend button. It
62saves the state of the machine to a filesystem or to a partition and switches
63to standby mode. Later resuming the machine the saved state is loaded back to
64ram and the machine can continue its work. It has two real benefits. First we
65save ourselves the time machine goes down and later boots up, energy costs
66are real high when running from batteries. The other gain is that we don't have to
67interrupt our programs so processes that are calculating something for a long
68time shouldn't need to be written interruptible.
69
70swsusp saves the state of the machine into active swaps and then reboots or
71powerdowns. You must explicitly specify the swap partition to resume from with
72``resume='' kernel option. If signature is found it loads and restores saved
73state. If the option ``noresume'' is specified as a boot parameter, it skips
f996fc96
BS
74the resuming. If the option ``hibernate=nocompress'' is specified as a boot
75parameter, it saves hibernation image without compression.
1da177e4
LT
76
77In the meantime while the system is suspended you should not add/remove any
78of the hardware, write to the filesystems, etc.
79
80Sleep states summary
81====================
82
83There are three different interfaces you can use, /proc/acpi should
84work like this:
85
86In a really perfect world:
87echo 1 > /proc/acpi/sleep # for standby
88echo 2 > /proc/acpi/sleep # for suspend to ram
89echo 3 > /proc/acpi/sleep # for suspend to ram, but with more power conservative
90echo 4 > /proc/acpi/sleep # for suspend to disk
91echo 5 > /proc/acpi/sleep # for shutdown unfriendly the system
92
93and perhaps
94echo 4b > /proc/acpi/sleep # for suspend to disk via s4bios
95
96Frequently Asked Questions
97==========================
98
99Q: well, suspending a server is IMHO a really stupid thing,
100but... (Diego Zuccato):
101
102A: You bought new UPS for your server. How do you install it without
103bringing machine down? Suspend to disk, rearrange power cables,
104resume.
105
106You have your server on UPS. Power died, and UPS is indicating 30
107seconds to failure. What do you do? Suspend to disk.
108
1da177e4
LT
109
110Q: Maybe I'm missing something, but why don't the regular I/O paths work?
111
112A: We do use the regular I/O paths. However we cannot restore the data
113to its original location as we load it. That would create an
114inconsistent kernel state which would certainly result in an oops.
115Instead, we load the image into unused memory and then atomically copy
116it back to it original location. This implies, of course, a maximum
117image size of half the amount of memory.
118
119There are two solutions to this:
120
121* require half of memory to be free during suspend. That way you can
122read "new" data onto free spots, then cli and copy
123
124* assume we had special "polling" ide driver that only uses memory
125between 0-640KB. That way, I'd have to make sure that 0-640KB is free
126during suspending, but otherwise it would work...
127
128suspend2 shares this fundamental limitation, but does not include user
129data and disk caches into "used memory" by saving them in
130advance. That means that the limitation goes away in practice.
131
132Q: Does linux support ACPI S4?
133
134A: Yes. That's what echo platform > /sys/power/disk does.
135
1da177e4
LT
136Q: What is 'suspend2'?
137
138A: suspend2 is 'Software Suspend 2', a forked implementation of
139suspend-to-disk which is available as separate patches for 2.4 and 2.6
140kernels from swsusp.sourceforge.net. It includes support for SMP, 4GB
141highmem and preemption. It also has a extensible architecture that
142allows for arbitrary transformations on the image (compression,
143encryption) and arbitrary backends for writing the image (eg to swap
144or an NFS share[Work In Progress]). Questions regarding suspend2
145should be sent to the mailing list available through the suspend2
146website, and not to the Linux Kernel Mailing List. We are working
147toward merging suspend2 into the mainline kernel.
148
83144186 149Q: What is the freezing of tasks and why are we using it?
1da177e4 150
83144186
RW
151A: The freezing of tasks is a mechanism by which user space processes and some
152kernel threads are controlled during hibernation or system-wide suspend (on some
153architectures). See freezing-of-tasks.txt for details.
1da177e4 154
11d77d0c 155Q: What is the difference between "platform" and "shutdown"?
1da177e4
LT
156
157A:
158
159shutdown: save state in linux, then tell bios to powerdown
160
161platform: save state in linux, then tell bios to powerdown and blink
162 "suspended led"
163
11d77d0c
JB
164"platform" is actually right thing to do where supported, but
165"shutdown" is most reliable (except on ACPI systems).
1da177e4
LT
166
167Q: I do not understand why you have such strong objections to idea of
168selective suspend.
169
2fe0ae78
ML
170A: Do selective suspend during runtime power management, that's okay. But
171it's useless for suspend-to-disk. (And I do not see how you could use
1da177e4
LT
172it for suspend-to-ram, I hope you do not want that).
173
174Lets see, so you suggest to
175
176* SUSPEND all but swap device and parents
177* Snapshot
178* Write image to disk
179* SUSPEND swap device and parents
180* Powerdown
181
182Oh no, that does not work, if swap device or its parents uses DMA,
183you've corrupted data. You'd have to do
184
185* SUSPEND all but swap device and parents
186* FREEZE swap device and parents
187* Snapshot
188* UNFREEZE swap device and parents
189* Write
190* SUSPEND swap device and parents
191
192Which means that you still need that FREEZE state, and you get more
193complicated code. (And I have not yet introduce details like system
194devices).
195
196Q: There don't seem to be any generally useful behavioral
197distinctions between SUSPEND and FREEZE.
198
199A: Doing SUSPEND when you are asked to do FREEZE is always correct,
25985edc 200but it may be unnecessarily slow. If you want your driver to stay simple,
1da177e4
LT
201slowness may not matter to you. It can always be fixed later.
202
203For devices like disk it does matter, you do not want to spindown for
204FREEZE.
205
2fe0ae78 206Q: After resuming, system is paging heavily, leading to very bad interactivity.
1da177e4
LT
207
208A: Try running
209
210cat `cat /proc/[0-9]*/maps | grep / | sed 's:.* /:/:' | sort -u` > /dev/null
211
a58a414f 212after resume. swapoff -a; swapon -a may also be useful.
fc5fb2c6
PM
213
214Q: What happens to devices during swsusp? They seem to be resumed
215during system suspend?
216
217A: That's correct. We need to resume them if we want to write image to
218disk. Whole sequence goes like
219
220 Suspend part
221 ~~~~~~~~~~~~
222 running system, user asks for suspend-to-disk
223
224 user processes are stopped
225
226 suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
227 with state snapshot
228
229 state snapshot: copy of whole used memory is taken with interrupts disabled
230
231 resume(): devices are woken up so that we can write image to swap
232
233 write image to swap
234
235 suspend(PMSG_SUSPEND): suspend devices so that we can power off
236
237 turn the power off
238
239 Resume part
240 ~~~~~~~~~~~
241 (is actually pretty similar)
242
243 running system, user asks for suspend-to-disk
244
25985edc 245 user processes are stopped (in common case there are none, but with resume-from-initrd, no one knows)
fc5fb2c6
PM
246
247 read image from disk
248
249 suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
250 with image restoration
251
252 image restoration: rewrite memory with image
253
254 resume(): devices are woken up so that system can continue
255
256 thaw all user processes
257
258Q: What is this 'Encrypt suspend image' for?
259
260A: First of all: it is not a replacement for dm-crypt encrypted swap.
261It cannot protect your computer while it is suspended. Instead it does
262protect from leaking sensitive data after resume from suspend.
263
264Think of the following: you suspend while an application is running
265that keeps sensitive data in memory. The application itself prevents
266the data from being swapped out. Suspend, however, must write these
267data to swap to be able to resume later on. Without suspend encryption
268your sensitive data are then stored in plaintext on disk. This means
269that after resume your sensitive data are accessible to all
270applications having direct access to the swap device which was used
271for suspend. If you don't need swap after resume these data can remain
272on disk virtually forever. Thus it can happen that your system gets
273broken in weeks later and sensitive data which you thought were
274encrypted and protected are retrieved and stolen from the swap device.
275To prevent this situation you should use 'Encrypt suspend image'.
276
277During suspend a temporary key is created and this key is used to
278encrypt the data written to disk. When, during resume, the data was
279read back into memory the temporary key is destroyed which simply
280means that all data written to disk during suspend are then
281inaccessible so they can't be stolen later on. The only thing that
282you must then take care of is that you call 'mkswap' for the swap
283partition used for suspend as early as possible during regular
284boot. This asserts that any temporary key from an oopsed suspend or
285from a failed or aborted resume is erased from the swap device.
286
287As a rule of thumb use encrypted swap to protect your data while your
288system is shut down or suspended. Additionally use the encrypted
289suspend image to prevent sensitive data from being stolen after
290resume.
7e958883 291
ecbd0da1 292Q: Can I suspend to a swap file?
7e958883 293
ecbd0da1
RW
294A: Generally, yes, you can. However, it requires you to use the "resume=" and
295"resume_offset=" kernel command line parameters, so the resume from a swap file
296cannot be initiated from an initrd or initramfs image. See
297swsusp-and-swap-files.txt for details.
d7ae79c7
PM
298
299Q: Is there a maximum system RAM size that is supported by swsusp?
300
301A: It should work okay with highmem.
302
303Q: Does swsusp (to disk) use only one swap partition or can it use
304multiple swap partitions (aggregate them into one logical space)?
305
306A: Only one swap partition, sorry.
307
308Q: If my application(s) causes lots of memory & swap space to be used
309(over half of the total system RAM), is it correct that it is likely
310to be useless to try to suspend to disk while that app is running?
311
312A: No, it should work okay, as long as your app does not mlock()
313it. Just prepare big enough swap partition.
314
a58a414f 315Q: What information is useful for debugging suspend-to-disk problems?
d7ae79c7
PM
316
317A: Well, last messages on the screen are always useful. If something
318is broken, it is usually some kernel driver, therefore trying with as
319little as possible modules loaded helps a lot. I also prefer people to
320suspend from console, preferably without X running. Booting with
321init=/bin/bash, then swapon and starting suspend sequence manually
322usually does the trick. Then it is good idea to try with latest
323vanilla kernel.
324
543cc27d
PM
325Q: How can distributions ship a swsusp-supporting kernel with modular
326disk drivers (especially SATA)?
327
328A: Well, it can be done, load the drivers, then do echo into
329/sys/power/disk/resume file from initrd. Be sure not to mount
330anything, not even read-only mount, or you are going to lose your
331data.
332
333Q: How do I make suspend more verbose?
334
335A: If you want to see any non-error kernel messages on the virtual
336terminal the kernel switches to during suspend, you have to set the
e084dbd3
PM
337kernel console loglevel to at least 4 (KERN_WARNING), for example by
338doing
339
340 # save the old loglevel
341 read LOGLEVEL DUMMY < /proc/sys/kernel/printk
342 # set the loglevel so we see the progress bar.
343 # if the level is higher than needed, we leave it alone.
344 if [ $LOGLEVEL -lt 5 ]; then
345 echo 5 > /proc/sys/kernel/printk
346 fi
347
348 IMG_SZ=0
349 read IMG_SZ < /sys/power/image_size
350 echo -n disk > /sys/power/state
351 RET=$?
352 #
353 # the logic here is:
354 # if image_size > 0 (without kernel support, IMG_SZ will be zero),
355 # then try again with image_size set to zero.
356 if [ $RET -ne 0 -a $IMG_SZ -ne 0 ]; then # try again with minimal image size
357 echo 0 > /sys/power/image_size
358 echo -n disk > /sys/power/state
359 RET=$?
360 fi
361
362 # restore previous loglevel
363 echo $LOGLEVEL > /proc/sys/kernel/printk
364 exit $RET
543cc27d
PM
365
366Q: Is this true that if I have a mounted filesystem on a USB device and
367I suspend to disk, I can lose data unless the filesystem has been mounted
368with "sync"?
369
b9827e4b
DB
370A: That's right ... if you disconnect that device, you may lose data.
371In fact, even with "-o sync" you can lose data if your programs have
372information in buffers they haven't written out to a disk you disconnect,
373or if you disconnect before the device finished saving data you wrote.
543cc27d 374
b9827e4b
DB
375Software suspend normally powers down USB controllers, which is equivalent
376to disconnecting all USB devices attached to your system.
377
378Your system might well support low-power modes for its USB controllers
379while the system is asleep, maintaining the connection, using true sleep
380modes like "suspend-to-RAM" or "standby". (Don't write "disk" to the
381/sys/power/state file; write "standby" or "mem".) We've not seen any
382hardware that can use these modes through software suspend, although in
11d77d0c
JB
383theory some systems might support "platform" modes that won't break the
384USB connections.
543cc27d
PM
385
386Remember that it's always a bad idea to unplug a disk drive containing a
b9827e4b
DB
387mounted filesystem. That's true even when your system is asleep! The
388safest thing is to unmount all filesystems on removable media (such USB,
389Firewire, CompactFlash, MMC, external SATA, or even IDE hotplug bays)
390before suspending; then remount them after resuming.
d7ae79c7 391
0458d5b4
AS
392There is a work-around for this problem. For more information, see
393Documentation/usb/persist.txt.
394
23b168d4
PM
395Q: Can I suspend-to-disk using a swap partition under LVM?
396
397A: No. You can suspend successfully, but you'll not be able to
398resume. uswsusp should be able to work with LVM. See suspend.sf.net.
399
e084dbd3
PM
400Q: I upgraded the kernel from 2.6.15 to 2.6.16. Both kernels were
401compiled with the similar configuration files. Anyway I found that
402suspend to disk (and resume) is much slower on 2.6.16 compared to
4032.6.15. Any idea for why that might happen or how can I speed it up?
404
405A: This is because the size of the suspend image is now greater than
406for 2.6.15 (by saving more data we can get more responsive system
407after resume).
408
409There's the /sys/power/image_size knob that controls the size of the
410image. If you set it to 0 (eg. by echo 0 > /sys/power/image_size as
411root), the 2.6.15 behavior should be restored. If it is still too
412slow, take a look at suspend.sf.net -- userland suspend is faster and
413supports LZF compression to speed it up further.