swsusp.txt 14 KB

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