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