blob: 7a6b7896645942d69815ee3b37594618367a9069 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001From kernel/suspend.c:
2
3 * BIG FAT WARNING *********************************************************
4 *
5 * If you have unsupported (*) devices using DMA...
6 * ...say goodbye to your data.
7 *
8 * If you touch anything on disk between suspend and resume...
9 * ...kiss your data goodbye.
10 *
11 * If your disk driver does not support suspend... (IDE does)
12 * ...you'd better find out how to get along
13 * without your data.
14 *
15 * If you change kernel command line between suspend and resume...
16 * ...prepare for nasty fsck or worse.
17 *
18 * If you change your hardware while system is suspended...
19 * ...well, it was not good idea.
20 *
21 * (*) suspend/resume support is needed to make it safe.
22
23You need to append resume=/dev/your_swap_partition to kernel command
24line. Then you suspend by
25
26echo shutdown > /sys/power/disk; echo disk > /sys/power/state
27
28. If you feel ACPI works pretty well on your system, you might try
29
30echo platform > /sys/power/disk; echo disk > /sys/power/state
31
32
33
34Article about goals and implementation of Software Suspend for Linux
35~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36Author: G‚ábor Kuti
37Last revised: 2003-10-20 by Pavel Machek
38
39Idea and goals to achieve
40
41Nowadays it is common in several laptops that they have a suspend button. It
42saves the state of the machine to a filesystem or to a partition and switches
43to standby mode. Later resuming the machine the saved state is loaded back to
44ram and the machine can continue its work. It has two real benefits. First we
45save ourselves the time machine goes down and later boots up, energy costs
46are real high when running from batteries. The other gain is that we don't have to
47interrupt our programs so processes that are calculating something for a long
48time shouldn't need to be written interruptible.
49
50swsusp saves the state of the machine into active swaps and then reboots or
51powerdowns. You must explicitly specify the swap partition to resume from with
52``resume='' kernel option. If signature is found it loads and restores saved
53state. If the option ``noresume'' is specified as a boot parameter, it skips
54the resuming.
55
56In the meantime while the system is suspended you should not add/remove any
57of the hardware, write to the filesystems, etc.
58
59Sleep states summary
60====================
61
62There are three different interfaces you can use, /proc/acpi should
63work like this:
64
65In a really perfect world:
66echo 1 > /proc/acpi/sleep # for standby
67echo 2 > /proc/acpi/sleep # for suspend to ram
68echo 3 > /proc/acpi/sleep # for suspend to ram, but with more power conservative
69echo 4 > /proc/acpi/sleep # for suspend to disk
70echo 5 > /proc/acpi/sleep # for shutdown unfriendly the system
71
72and perhaps
73echo 4b > /proc/acpi/sleep # for suspend to disk via s4bios
74
75Frequently Asked Questions
76==========================
77
78Q: well, suspending a server is IMHO a really stupid thing,
79but... (Diego Zuccato):
80
81A: You bought new UPS for your server. How do you install it without
82bringing machine down? Suspend to disk, rearrange power cables,
83resume.
84
85You have your server on UPS. Power died, and UPS is indicating 30
86seconds to failure. What do you do? Suspend to disk.
87
88Ethernet card in your server died. You want to replace it. Your
89server is not hotplug capable. What do you do? Suspend to disk,
90replace ethernet card, resume. If you are fast your users will not
91even see broken connections.
92
93
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
120Q: My machine doesn't work with ACPI. How can I use swsusp than ?
121
122A: Do a reboot() syscall with right parameters. Warning: glibc gets in
123its way, so check with strace:
124
125reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, 0xd000fce2)
126
127(Thanks to Peter Osterlund:)
128
129#include <unistd.h>
130#include <syscall.h>
131
132#define LINUX_REBOOT_MAGIC1 0xfee1dead
133#define LINUX_REBOOT_MAGIC2 672274793
134#define LINUX_REBOOT_CMD_SW_SUSPEND 0xD000FCE2
135
136int main()
137{
138 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
139 LINUX_REBOOT_CMD_SW_SUSPEND, 0);
140 return 0;
141}
142
143Also /sys/ interface should be still present.
144
145Q: What is 'suspend2'?
146
147A: suspend2 is 'Software Suspend 2', a forked implementation of
148suspend-to-disk which is available as separate patches for 2.4 and 2.6
149kernels from swsusp.sourceforge.net. It includes support for SMP, 4GB
150highmem and preemption. It also has a extensible architecture that
151allows for arbitrary transformations on the image (compression,
152encryption) and arbitrary backends for writing the image (eg to swap
153or an NFS share[Work In Progress]). Questions regarding suspend2
154should be sent to the mailing list available through the suspend2
155website, and not to the Linux Kernel Mailing List. We are working
156toward merging suspend2 into the mainline kernel.
157
158Q: A kernel thread must voluntarily freeze itself (call 'refrigerator').
159I found some kernel threads that don't do it, and they don't freeze
160so the system can't sleep. Is this a known behavior?
161
162A: All such kernel threads need to be fixed, one by one. Select the
163place where the thread is safe to be frozen (no kernel semaphores
164should be held at that point and it must be safe to sleep there), and
165add:
166
Linus Torvalds2031d0f2005-06-25 17:16:53 -0700167 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169If the thread is needed for writing the image to storage, you should
Pavel Machekfc5fb2c2005-06-25 14:55:07 -0700170instead set the PF_NOFREEZE process flag when creating the thread (and
171be very carefull).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173
174Q: What is the difference between between "platform", "shutdown" and
175"firmware" in /sys/power/disk?
176
177A:
178
179shutdown: save state in linux, then tell bios to powerdown
180
181platform: save state in linux, then tell bios to powerdown and blink
182 "suspended led"
183
184firmware: tell bios to save state itself [needs BIOS-specific suspend
185 partition, and has very little to do with swsusp]
186
187"platform" is actually right thing to do, but "shutdown" is most
188reliable.
189
190Q: I do not understand why you have such strong objections to idea of
191selective suspend.
192
193A: Do selective suspend during runtime power managment, that's okay. But
194its useless for suspend-to-disk. (And I do not see how you could use
195it for suspend-to-ram, I hope you do not want that).
196
197Lets see, so you suggest to
198
199* SUSPEND all but swap device and parents
200* Snapshot
201* Write image to disk
202* SUSPEND swap device and parents
203* Powerdown
204
205Oh no, that does not work, if swap device or its parents uses DMA,
206you've corrupted data. You'd have to do
207
208* SUSPEND all but swap device and parents
209* FREEZE swap device and parents
210* Snapshot
211* UNFREEZE swap device and parents
212* Write
213* SUSPEND swap device and parents
214
215Which means that you still need that FREEZE state, and you get more
216complicated code. (And I have not yet introduce details like system
217devices).
218
219Q: There don't seem to be any generally useful behavioral
220distinctions between SUSPEND and FREEZE.
221
222A: Doing SUSPEND when you are asked to do FREEZE is always correct,
223but it may be unneccessarily slow. If you want USB to stay simple,
224slowness may not matter to you. It can always be fixed later.
225
226For devices like disk it does matter, you do not want to spindown for
227FREEZE.
228
229Q: After resuming, system is paging heavilly, leading to very bad interactivity.
230
231A: Try running
232
233cat `cat /proc/[0-9]*/maps | grep / | sed 's:.* /:/:' | sort -u` > /dev/null
234
235after resume. swapoff -a; swapon -a may also be usefull.
Pavel Machekfc5fb2c2005-06-25 14:55:07 -0700236
237Q: What happens to devices during swsusp? They seem to be resumed
238during system suspend?
239
240A: That's correct. We need to resume them if we want to write image to
241disk. Whole sequence goes like
242
243 Suspend part
244 ~~~~~~~~~~~~
245 running system, user asks for suspend-to-disk
246
247 user processes are stopped
248
249 suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
250 with state snapshot
251
252 state snapshot: copy of whole used memory is taken with interrupts disabled
253
254 resume(): devices are woken up so that we can write image to swap
255
256 write image to swap
257
258 suspend(PMSG_SUSPEND): suspend devices so that we can power off
259
260 turn the power off
261
262 Resume part
263 ~~~~~~~~~~~
264 (is actually pretty similar)
265
266 running system, user asks for suspend-to-disk
267
268 user processes are stopped (in common case there are none, but with resume-from-initrd, noone knows)
269
270 read image from disk
271
272 suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
273 with image restoration
274
275 image restoration: rewrite memory with image
276
277 resume(): devices are woken up so that system can continue
278
279 thaw all user processes
280
281Q: What is this 'Encrypt suspend image' for?
282
283A: First of all: it is not a replacement for dm-crypt encrypted swap.
284It cannot protect your computer while it is suspended. Instead it does
285protect from leaking sensitive data after resume from suspend.
286
287Think of the following: you suspend while an application is running
288that keeps sensitive data in memory. The application itself prevents
289the data from being swapped out. Suspend, however, must write these
290data to swap to be able to resume later on. Without suspend encryption
291your sensitive data are then stored in plaintext on disk. This means
292that after resume your sensitive data are accessible to all
293applications having direct access to the swap device which was used
294for suspend. If you don't need swap after resume these data can remain
295on disk virtually forever. Thus it can happen that your system gets
296broken in weeks later and sensitive data which you thought were
297encrypted and protected are retrieved and stolen from the swap device.
298To prevent this situation you should use 'Encrypt suspend image'.
299
300During suspend a temporary key is created and this key is used to
301encrypt the data written to disk. When, during resume, the data was
302read back into memory the temporary key is destroyed which simply
303means that all data written to disk during suspend are then
304inaccessible so they can't be stolen later on. The only thing that
305you must then take care of is that you call 'mkswap' for the swap
306partition used for suspend as early as possible during regular
307boot. This asserts that any temporary key from an oopsed suspend or
308from a failed or aborted resume is erased from the swap device.
309
310As a rule of thumb use encrypted swap to protect your data while your
311system is shut down or suspended. Additionally use the encrypted
312suspend image to prevent sensitive data from being stolen after
313resume.