swsusp.txt 16 KB

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