swsusp.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. From kernel/suspend.c:
  2. * BIG FAT WARNING *********************************************************
  3. *
  4. * If you have unsupported (*) devices using DMA...
  5. * ...say goodbye to your data.
  6. *
  7. * If you touch anything on disk between suspend and resume...
  8. * ...kiss your data goodbye.
  9. *
  10. * If your disk driver does not support suspend... (IDE does)
  11. * ...you'd better find out how to get along
  12. * without your data.
  13. *
  14. * If you change kernel command line between suspend and resume...
  15. * ...prepare for nasty fsck or worse.
  16. *
  17. * If you change your hardware while system is suspended...
  18. * ...well, it was not good idea.
  19. *
  20. * (*) suspend/resume support is needed to make it safe.
  21. You need to append resume=/dev/your_swap_partition to kernel command
  22. line. Then you suspend by
  23. echo shutdown > /sys/power/disk; echo disk > /sys/power/state
  24. . If you feel ACPI works pretty well on your system, you might try
  25. echo platform > /sys/power/disk; echo disk > /sys/power/state
  26. Article about goals and implementation of Software Suspend for Linux
  27. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. Author: G‚ábor Kuti
  29. Last revised: 2003-10-20 by Pavel Machek
  30. Idea and goals to achieve
  31. Nowadays it is common in several laptops that they have a suspend button. It
  32. saves the state of the machine to a filesystem or to a partition and switches
  33. to standby mode. Later resuming the machine the saved state is loaded back to
  34. ram and the machine can continue its work. It has two real benefits. First we
  35. save ourselves the time machine goes down and later boots up, energy costs
  36. are real high when running from batteries. The other gain is that we don't have to
  37. interrupt our programs so processes that are calculating something for a long
  38. time shouldn't need to be written interruptible.
  39. swsusp saves the state of the machine into active swaps and then reboots or
  40. powerdowns. You must explicitly specify the swap partition to resume from with
  41. ``resume='' kernel option. If signature is found it loads and restores saved
  42. state. If the option ``noresume'' is specified as a boot parameter, it skips
  43. the resuming.
  44. In the meantime while the system is suspended you should not add/remove any
  45. of the hardware, write to the filesystems, etc.
  46. Sleep states summary
  47. ====================
  48. There are three different interfaces you can use, /proc/acpi should
  49. work like this:
  50. In a really perfect world:
  51. echo 1 > /proc/acpi/sleep # for standby
  52. echo 2 > /proc/acpi/sleep # for suspend to ram
  53. echo 3 > /proc/acpi/sleep # for suspend to ram, but with more power conservative
  54. echo 4 > /proc/acpi/sleep # for suspend to disk
  55. echo 5 > /proc/acpi/sleep # for shutdown unfriendly the system
  56. and perhaps
  57. echo 4b > /proc/acpi/sleep # for suspend to disk via s4bios
  58. Frequently Asked Questions
  59. ==========================
  60. Q: well, suspending a server is IMHO a really stupid thing,
  61. but... (Diego Zuccato):
  62. A: You bought new UPS for your server. How do you install it without
  63. bringing machine down? Suspend to disk, rearrange power cables,
  64. resume.
  65. You have your server on UPS. Power died, and UPS is indicating 30
  66. seconds to failure. What do you do? Suspend to disk.
  67. Ethernet card in your server died. You want to replace it. Your
  68. server is not hotplug capable. What do you do? Suspend to disk,
  69. replace ethernet card, resume. If you are fast your users will not
  70. even see broken connections.
  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: My machine doesn't work with ACPI. How can I use swsusp than ?
  90. A: Do a reboot() syscall with right parameters. Warning: glibc gets in
  91. its way, so check with strace:
  92. reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, 0xd000fce2)
  93. (Thanks to Peter Osterlund:)
  94. #include <unistd.h>
  95. #include <syscall.h>
  96. #define LINUX_REBOOT_MAGIC1 0xfee1dead
  97. #define LINUX_REBOOT_MAGIC2 672274793
  98. #define LINUX_REBOOT_CMD_SW_SUSPEND 0xD000FCE2
  99. int main()
  100. {
  101. syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
  102. LINUX_REBOOT_CMD_SW_SUSPEND, 0);
  103. return 0;
  104. }
  105. Also /sys/ interface should be still present.
  106. Q: What is 'suspend2'?
  107. A: suspend2 is 'Software Suspend 2', a forked implementation of
  108. suspend-to-disk which is available as separate patches for 2.4 and 2.6
  109. kernels from swsusp.sourceforge.net. It includes support for SMP, 4GB
  110. highmem and preemption. It also has a extensible architecture that
  111. allows for arbitrary transformations on the image (compression,
  112. encryption) and arbitrary backends for writing the image (eg to swap
  113. or an NFS share[Work In Progress]). Questions regarding suspend2
  114. should be sent to the mailing list available through the suspend2
  115. website, and not to the Linux Kernel Mailing List. We are working
  116. toward merging suspend2 into the mainline kernel.
  117. Q: A kernel thread must voluntarily freeze itself (call 'refrigerator').
  118. I found some kernel threads that don't do it, and they don't freeze
  119. so the system can't sleep. Is this a known behavior?
  120. A: All such kernel threads need to be fixed, one by one. Select the
  121. place where the thread is safe to be frozen (no kernel semaphores
  122. should be held at that point and it must be safe to sleep there), and
  123. add:
  124. try_to_freeze();
  125. If the thread is needed for writing the image to storage, you should
  126. instead set the PF_NOFREEZE process flag when creating the thread (and
  127. be very carefull).
  128. Q: What is the difference between between "platform", "shutdown" and
  129. "firmware" in /sys/power/disk?
  130. A:
  131. shutdown: save state in linux, then tell bios to powerdown
  132. platform: save state in linux, then tell bios to powerdown and blink
  133. "suspended led"
  134. firmware: tell bios to save state itself [needs BIOS-specific suspend
  135. partition, and has very little to do with swsusp]
  136. "platform" is actually right thing to do, but "shutdown" is most
  137. reliable.
  138. Q: I do not understand why you have such strong objections to idea of
  139. selective suspend.
  140. A: Do selective suspend during runtime power managment, that's okay. But
  141. its useless for suspend-to-disk. (And I do not see how you could use
  142. it for suspend-to-ram, I hope you do not want that).
  143. Lets see, so you suggest to
  144. * SUSPEND all but swap device and parents
  145. * Snapshot
  146. * Write image to disk
  147. * SUSPEND swap device and parents
  148. * Powerdown
  149. Oh no, that does not work, if swap device or its parents uses DMA,
  150. you've corrupted data. You'd have to do
  151. * SUSPEND all but swap device and parents
  152. * FREEZE swap device and parents
  153. * Snapshot
  154. * UNFREEZE swap device and parents
  155. * Write
  156. * SUSPEND swap device and parents
  157. Which means that you still need that FREEZE state, and you get more
  158. complicated code. (And I have not yet introduce details like system
  159. devices).
  160. Q: There don't seem to be any generally useful behavioral
  161. distinctions between SUSPEND and FREEZE.
  162. A: Doing SUSPEND when you are asked to do FREEZE is always correct,
  163. but it may be unneccessarily slow. If you want USB to stay simple,
  164. slowness may not matter to you. It can always be fixed later.
  165. For devices like disk it does matter, you do not want to spindown for
  166. FREEZE.
  167. Q: After resuming, system is paging heavilly, leading to very bad interactivity.
  168. A: Try running
  169. cat `cat /proc/[0-9]*/maps | grep / | sed 's:.* /:/:' | sort -u` > /dev/null
  170. after resume. swapoff -a; swapon -a may also be usefull.
  171. Q: What happens to devices during swsusp? They seem to be resumed
  172. during system suspend?
  173. A: That's correct. We need to resume them if we want to write image to
  174. disk. Whole sequence goes like
  175. Suspend part
  176. ~~~~~~~~~~~~
  177. running system, user asks for suspend-to-disk
  178. user processes are stopped
  179. suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
  180. with state snapshot
  181. state snapshot: copy of whole used memory is taken with interrupts disabled
  182. resume(): devices are woken up so that we can write image to swap
  183. write image to swap
  184. suspend(PMSG_SUSPEND): suspend devices so that we can power off
  185. turn the power off
  186. Resume part
  187. ~~~~~~~~~~~
  188. (is actually pretty similar)
  189. running system, user asks for suspend-to-disk
  190. user processes are stopped (in common case there are none, but with resume-from-initrd, noone knows)
  191. read image from disk
  192. suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
  193. with image restoration
  194. image restoration: rewrite memory with image
  195. resume(): devices are woken up so that system can continue
  196. thaw all user processes
  197. Q: What is this 'Encrypt suspend image' for?
  198. A: First of all: it is not a replacement for dm-crypt encrypted swap.
  199. It cannot protect your computer while it is suspended. Instead it does
  200. protect from leaking sensitive data after resume from suspend.
  201. Think of the following: you suspend while an application is running
  202. that keeps sensitive data in memory. The application itself prevents
  203. the data from being swapped out. Suspend, however, must write these
  204. data to swap to be able to resume later on. Without suspend encryption
  205. your sensitive data are then stored in plaintext on disk. This means
  206. that after resume your sensitive data are accessible to all
  207. applications having direct access to the swap device which was used
  208. for suspend. If you don't need swap after resume these data can remain
  209. on disk virtually forever. Thus it can happen that your system gets
  210. broken in weeks later and sensitive data which you thought were
  211. encrypted and protected are retrieved and stolen from the swap device.
  212. To prevent this situation you should use 'Encrypt suspend image'.
  213. During suspend a temporary key is created and this key is used to
  214. encrypt the data written to disk. When, during resume, the data was
  215. read back into memory the temporary key is destroyed which simply
  216. means that all data written to disk during suspend are then
  217. inaccessible so they can't be stolen later on. The only thing that
  218. you must then take care of is that you call 'mkswap' for the swap
  219. partition used for suspend as early as possible during regular
  220. boot. This asserts that any temporary key from an oopsed suspend or
  221. from a failed or aborted resume is erased from the swap device.
  222. As a rule of thumb use encrypted swap to protect your data while your
  223. system is shut down or suspended. Additionally use the encrypted
  224. suspend image to prevent sensitive data from being stolen after
  225. resume.