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