swsusp.rst 18 KB

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