blob: b0d50840788ebf80fd478ac9db3e8ef841716e26 [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.
20
21You need to append resume=/dev/your_swap_partition to kernel command
22line. Then you suspend by
23
24echo shutdown > /sys/power/disk; echo disk > /sys/power/state
25
26. If you feel ACPI works pretty well on your system, you might try
27
28echo platform > /sys/power/disk; echo disk > /sys/power/state
29
30
Pavel Machekd7ae79c2005-09-06 15:16:21 -070031Encrypted suspend image:
32------------------------
33If you want to store your suspend image encrypted with a temporary
34key to prevent data gathering after resume you must compile
35crypto and the aes algorithm into the kernel - modules won't work
36as they cannot be loaded at resume time.
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39Article about goals and implementation of Software Suspend for Linux
40~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41Author: G‚ábor Kuti
42Last revised: 2003-10-20 by Pavel Machek
43
44Idea and goals to achieve
45
46Nowadays it is common in several laptops that they have a suspend button. It
47saves the state of the machine to a filesystem or to a partition and switches
48to standby mode. Later resuming the machine the saved state is loaded back to
49ram and the machine can continue its work. It has two real benefits. First we
50save ourselves the time machine goes down and later boots up, energy costs
51are real high when running from batteries. The other gain is that we don't have to
52interrupt our programs so processes that are calculating something for a long
53time shouldn't need to be written interruptible.
54
55swsusp saves the state of the machine into active swaps and then reboots or
56powerdowns. You must explicitly specify the swap partition to resume from with
57``resume='' kernel option. If signature is found it loads and restores saved
58state. If the option ``noresume'' is specified as a boot parameter, it skips
59the resuming.
60
61In the meantime while the system is suspended you should not add/remove any
62of the hardware, write to the filesystems, etc.
63
64Sleep states summary
65====================
66
67There are three different interfaces you can use, /proc/acpi should
68work like this:
69
70In a really perfect world:
71echo 1 > /proc/acpi/sleep # for standby
72echo 2 > /proc/acpi/sleep # for suspend to ram
73echo 3 > /proc/acpi/sleep # for suspend to ram, but with more power conservative
74echo 4 > /proc/acpi/sleep # for suspend to disk
75echo 5 > /proc/acpi/sleep # for shutdown unfriendly the system
76
77and perhaps
78echo 4b > /proc/acpi/sleep # for suspend to disk via s4bios
79
80Frequently Asked Questions
81==========================
82
83Q: well, suspending a server is IMHO a really stupid thing,
84but... (Diego Zuccato):
85
86A: You bought new UPS for your server. How do you install it without
87bringing machine down? Suspend to disk, rearrange power cables,
88resume.
89
90You have your server on UPS. Power died, and UPS is indicating 30
91seconds to failure. What do you do? Suspend to disk.
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94Q: Maybe I'm missing something, but why don't the regular I/O paths work?
95
96A: We do use the regular I/O paths. However we cannot restore the data
97to its original location as we load it. That would create an
98inconsistent kernel state which would certainly result in an oops.
99Instead, we load the image into unused memory and then atomically copy
100it back to it original location. This implies, of course, a maximum
101image size of half the amount of memory.
102
103There are two solutions to this:
104
105* require half of memory to be free during suspend. That way you can
106read "new" data onto free spots, then cli and copy
107
108* assume we had special "polling" ide driver that only uses memory
109between 0-640KB. That way, I'd have to make sure that 0-640KB is free
110during suspending, but otherwise it would work...
111
112suspend2 shares this fundamental limitation, but does not include user
113data and disk caches into "used memory" by saving them in
114advance. That means that the limitation goes away in practice.
115
116Q: Does linux support ACPI S4?
117
118A: Yes. That's what echo platform > /sys/power/disk does.
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120Q: What is 'suspend2'?
121
122A: suspend2 is 'Software Suspend 2', a forked implementation of
123suspend-to-disk which is available as separate patches for 2.4 and 2.6
124kernels from swsusp.sourceforge.net. It includes support for SMP, 4GB
125highmem and preemption. It also has a extensible architecture that
126allows for arbitrary transformations on the image (compression,
127encryption) and arbitrary backends for writing the image (eg to swap
128or an NFS share[Work In Progress]). Questions regarding suspend2
129should be sent to the mailing list available through the suspend2
130website, and not to the Linux Kernel Mailing List. We are working
131toward merging suspend2 into the mainline kernel.
132
133Q: A kernel thread must voluntarily freeze itself (call 'refrigerator').
134I found some kernel threads that don't do it, and they don't freeze
135so the system can't sleep. Is this a known behavior?
136
137A: All such kernel threads need to be fixed, one by one. Select the
138place where the thread is safe to be frozen (no kernel semaphores
139should be held at that point and it must be safe to sleep there), and
140add:
141
Linus Torvalds2031d0f2005-06-25 17:16:53 -0700142 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144If the thread is needed for writing the image to storage, you should
Pavel Machekfc5fb2c2005-06-25 14:55:07 -0700145instead set the PF_NOFREEZE process flag when creating the thread (and
146be very carefull).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148
149Q: What is the difference between between "platform", "shutdown" and
150"firmware" in /sys/power/disk?
151
152A:
153
154shutdown: save state in linux, then tell bios to powerdown
155
156platform: save state in linux, then tell bios to powerdown and blink
157 "suspended led"
158
159firmware: tell bios to save state itself [needs BIOS-specific suspend
160 partition, and has very little to do with swsusp]
161
162"platform" is actually right thing to do, but "shutdown" is most
163reliable.
164
165Q: I do not understand why you have such strong objections to idea of
166selective suspend.
167
168A: Do selective suspend during runtime power managment, that's okay. But
169its useless for suspend-to-disk. (And I do not see how you could use
170it for suspend-to-ram, I hope you do not want that).
171
172Lets see, so you suggest to
173
174* SUSPEND all but swap device and parents
175* Snapshot
176* Write image to disk
177* SUSPEND swap device and parents
178* Powerdown
179
180Oh no, that does not work, if swap device or its parents uses DMA,
181you've corrupted data. You'd have to do
182
183* SUSPEND all but swap device and parents
184* FREEZE swap device and parents
185* Snapshot
186* UNFREEZE swap device and parents
187* Write
188* SUSPEND swap device and parents
189
190Which means that you still need that FREEZE state, and you get more
191complicated code. (And I have not yet introduce details like system
192devices).
193
194Q: There don't seem to be any generally useful behavioral
195distinctions between SUSPEND and FREEZE.
196
197A: Doing SUSPEND when you are asked to do FREEZE is always correct,
198but it may be unneccessarily slow. If you want USB to stay simple,
199slowness may not matter to you. It can always be fixed later.
200
201For devices like disk it does matter, you do not want to spindown for
202FREEZE.
203
204Q: After resuming, system is paging heavilly, leading to very bad interactivity.
205
206A: Try running
207
208cat `cat /proc/[0-9]*/maps | grep / | sed 's:.* /:/:' | sort -u` > /dev/null
209
210after resume. swapoff -a; swapon -a may also be usefull.
Pavel Machekfc5fb2c2005-06-25 14:55:07 -0700211
212Q: What happens to devices during swsusp? They seem to be resumed
213during system suspend?
214
215A: That's correct. We need to resume them if we want to write image to
216disk. Whole sequence goes like
217
218 Suspend part
219 ~~~~~~~~~~~~
220 running system, user asks for suspend-to-disk
221
222 user processes are stopped
223
224 suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
225 with state snapshot
226
227 state snapshot: copy of whole used memory is taken with interrupts disabled
228
229 resume(): devices are woken up so that we can write image to swap
230
231 write image to swap
232
233 suspend(PMSG_SUSPEND): suspend devices so that we can power off
234
235 turn the power off
236
237 Resume part
238 ~~~~~~~~~~~
239 (is actually pretty similar)
240
241 running system, user asks for suspend-to-disk
242
243 user processes are stopped (in common case there are none, but with resume-from-initrd, noone knows)
244
245 read image from disk
246
247 suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
248 with image restoration
249
250 image restoration: rewrite memory with image
251
252 resume(): devices are woken up so that system can continue
253
254 thaw all user processes
255
256Q: What is this 'Encrypt suspend image' for?
257
258A: First of all: it is not a replacement for dm-crypt encrypted swap.
259It cannot protect your computer while it is suspended. Instead it does
260protect from leaking sensitive data after resume from suspend.
261
262Think of the following: you suspend while an application is running
263that keeps sensitive data in memory. The application itself prevents
264the data from being swapped out. Suspend, however, must write these
265data to swap to be able to resume later on. Without suspend encryption
266your sensitive data are then stored in plaintext on disk. This means
267that after resume your sensitive data are accessible to all
268applications having direct access to the swap device which was used
269for suspend. If you don't need swap after resume these data can remain
270on disk virtually forever. Thus it can happen that your system gets
271broken in weeks later and sensitive data which you thought were
272encrypted and protected are retrieved and stolen from the swap device.
273To prevent this situation you should use 'Encrypt suspend image'.
274
275During suspend a temporary key is created and this key is used to
276encrypt the data written to disk. When, during resume, the data was
277read back into memory the temporary key is destroyed which simply
278means that all data written to disk during suspend are then
279inaccessible so they can't be stolen later on. The only thing that
280you must then take care of is that you call 'mkswap' for the swap
281partition used for suspend as early as possible during regular
282boot. This asserts that any temporary key from an oopsed suspend or
283from a failed or aborted resume is erased from the swap device.
284
285As a rule of thumb use encrypted swap to protect your data while your
286system is shut down or suspended. Additionally use the encrypted
287suspend image to prevent sensitive data from being stolen after
288resume.
Pavel Machek7e958882005-09-03 15:56:56 -0700289
Pavel Machekd7ae79c2005-09-06 15:16:21 -0700290Q: Why can't we suspend to a swap file?
Pavel Machek7e958882005-09-03 15:56:56 -0700291
292A: Because accessing swap file needs the filesystem mounted, and
293filesystem might do something wrong (like replaying the journal)
Pavel Machekd7ae79c2005-09-06 15:16:21 -0700294during mount.
295
296There are few ways to get that fixed:
297
2981) Probably could be solved by modifying every filesystem to support
299some kind of "really read-only!" option. Patches welcome.
300
3012) suspend2 gets around that by storing absolute positions in on-disk
302image (and blocksize), with resume parameter pointing directly to
303suspend header.
304
305Q: Is there a maximum system RAM size that is supported by swsusp?
306
307A: It should work okay with highmem.
308
309Q: Does swsusp (to disk) use only one swap partition or can it use
310multiple swap partitions (aggregate them into one logical space)?
311
312A: Only one swap partition, sorry.
313
314Q: If my application(s) causes lots of memory & swap space to be used
315(over half of the total system RAM), is it correct that it is likely
316to be useless to try to suspend to disk while that app is running?
317
318A: No, it should work okay, as long as your app does not mlock()
319it. Just prepare big enough swap partition.
320
321Q: What information is usefull for debugging suspend-to-disk problems?
322
323A: Well, last messages on the screen are always useful. If something
324is broken, it is usually some kernel driver, therefore trying with as
325little as possible modules loaded helps a lot. I also prefer people to
326suspend from console, preferably without X running. Booting with
327init=/bin/bash, then swapon and starting suspend sequence manually
328usually does the trick. Then it is good idea to try with latest
329vanilla kernel.
330
331