swsusp.txt 12 KB

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