Back to home page

OSCL-LXR

 
 

    


0001 ============
0002 Swap suspend
0003 ============
0004 
0005 Some warnings, first.
0006 
0007 .. warning::
0008 
0009    **BIG FAT WARNING**
0010 
0011    If you touch anything on disk between suspend and resume...
0012                                 ...kiss your data goodbye.
0013 
0014    If you do resume from initrd after your filesystems are mounted...
0015                                 ...bye bye root partition.
0016 
0017                         [this is actually same case as above]
0018 
0019    If you have unsupported ( ) devices using DMA, you may have some
0020    problems. If your disk driver does not support suspend... (IDE does),
0021    it may cause some problems, too. If you change kernel command line
0022    between suspend and resume, it may do something wrong. If you change
0023    your hardware while system is suspended... well, it was not good idea;
0024    but it will probably only crash.
0025 
0026    ( ) suspend/resume support is needed to make it safe.
0027 
0028    If you have any filesystems on USB devices mounted before software suspend,
0029    they won't be accessible after resume and you may lose data, as though
0030    you have unplugged the USB devices with mounted filesystems on them;
0031    see the FAQ below for details.  (This is not true for more traditional
0032    power states like "standby", which normally don't turn USB off.)
0033 
0034 Swap partition:
0035   You need to append resume=/dev/your_swap_partition to kernel command
0036   line or specify it using /sys/power/resume.
0037 
0038 Swap file:
0039   If using a swapfile you can also specify a resume offset using
0040   resume_offset=<number> on the kernel command line or specify it
0041   in /sys/power/resume_offset.
0042 
0043 After preparing then you suspend by::
0044 
0045         echo shutdown > /sys/power/disk; echo disk > /sys/power/state
0046 
0047 - If you feel ACPI works pretty well on your system, you might try::
0048 
0049         echo platform > /sys/power/disk; echo disk > /sys/power/state
0050 
0051 - If you would like to write hibernation image to swap and then suspend
0052   to RAM (provided your platform supports it), you can try::
0053 
0054         echo suspend > /sys/power/disk; echo disk > /sys/power/state
0055 
0056 - If you have SATA disks, you'll need recent kernels with SATA suspend
0057   support. For suspend and resume to work, make sure your disk drivers
0058   are built into kernel -- not modules. [There's way to make
0059   suspend/resume with modular disk drivers, see FAQ, but you probably
0060   should not do that.]
0061 
0062 If you want to limit the suspend image size to N bytes, do::
0063 
0064         echo N > /sys/power/image_size
0065 
0066 before suspend (it is limited to around 2/5 of available RAM by default).
0067 
0068 - The resume process checks for the presence of the resume device,
0069   if found, it then checks the contents for the hibernation image signature.
0070   If both are found, it resumes the hibernation image.
0071 
0072 - The resume process may be triggered in two ways:
0073 
0074   1) During lateinit:  If resume=/dev/your_swap_partition is specified on
0075      the kernel command line, lateinit runs the resume process.  If the
0076      resume device has not been probed yet, the resume process fails and
0077      bootup continues.
0078   2) Manually from an initrd or initramfs:  May be run from
0079      the init script by using the /sys/power/resume file.  It is vital
0080      that this be done prior to remounting any filesystems (even as
0081      read-only) otherwise data may be corrupted.
0082 
0083 Article about goals and implementation of Software Suspend for Linux
0084 ====================================================================
0085 
0086 Author: Gábor Kuti
0087 Last revised: 2003-10-20 by Pavel Machek
0088 
0089 Idea and goals to achieve
0090 -------------------------
0091 
0092 Nowadays it is common in several laptops that they have a suspend button. It
0093 saves the state of the machine to a filesystem or to a partition and switches
0094 to standby mode. Later resuming the machine the saved state is loaded back to
0095 ram and the machine can continue its work. It has two real benefits. First we
0096 save ourselves the time machine goes down and later boots up, energy costs
0097 are real high when running from batteries. The other gain is that we don't have
0098 to interrupt our programs so processes that are calculating something for a long
0099 time shouldn't need to be written interruptible.
0100 
0101 swsusp saves the state of the machine into active swaps and then reboots or
0102 powerdowns.  You must explicitly specify the swap partition to resume from with
0103 `resume=` kernel option. If signature is found it loads and restores saved
0104 state. If the option `noresume` is specified as a boot parameter, it skips
0105 the resuming.  If the option `hibernate=nocompress` is specified as a boot
0106 parameter, it saves hibernation image without compression.
0107 
0108 In the meantime while the system is suspended you should not add/remove any
0109 of the hardware, write to the filesystems, etc.
0110 
0111 Sleep states summary
0112 ====================
0113 
0114 There are three different interfaces you can use, /proc/acpi should
0115 work like this:
0116 
0117 In a really perfect world::
0118 
0119   echo 1 > /proc/acpi/sleep       # for standby
0120   echo 2 > /proc/acpi/sleep       # for suspend to ram
0121   echo 3 > /proc/acpi/sleep       # for suspend to ram, but with more power
0122                                   # conservative
0123   echo 4 > /proc/acpi/sleep       # for suspend to disk
0124   echo 5 > /proc/acpi/sleep       # for shutdown unfriendly the system
0125 
0126 and perhaps::
0127 
0128   echo 4b > /proc/acpi/sleep      # for suspend to disk via s4bios
0129 
0130 Frequently Asked Questions
0131 ==========================
0132 
0133 Q:
0134   well, suspending a server is IMHO a really stupid thing,
0135   but... (Diego Zuccato):
0136 
0137 A:
0138   You bought new UPS for your server. How do you install it without
0139   bringing machine down? Suspend to disk, rearrange power cables,
0140   resume.
0141 
0142   You have your server on UPS. Power died, and UPS is indicating 30
0143   seconds to failure. What do you do? Suspend to disk.
0144 
0145 
0146 Q:
0147   Maybe I'm missing something, but why don't the regular I/O paths work?
0148 
0149 A:
0150   We do use the regular I/O paths. However we cannot restore the data
0151   to its original location as we load it. That would create an
0152   inconsistent kernel state which would certainly result in an oops.
0153   Instead, we load the image into unused memory and then atomically copy
0154   it back to it original location. This implies, of course, a maximum
0155   image size of half the amount of memory.
0156 
0157   There are two solutions to this:
0158 
0159   * require half of memory to be free during suspend. That way you can
0160     read "new" data onto free spots, then cli and copy
0161 
0162   * assume we had special "polling" ide driver that only uses memory
0163     between 0-640KB. That way, I'd have to make sure that 0-640KB is free
0164     during suspending, but otherwise it would work...
0165 
0166   suspend2 shares this fundamental limitation, but does not include user
0167   data and disk caches into "used memory" by saving them in
0168   advance. That means that the limitation goes away in practice.
0169 
0170 Q:
0171   Does linux support ACPI S4?
0172 
0173 A:
0174   Yes. That's what echo platform > /sys/power/disk does.
0175 
0176 Q:
0177   What is 'suspend2'?
0178 
0179 A:
0180   suspend2 is 'Software Suspend 2', a forked implementation of
0181   suspend-to-disk which is available as separate patches for 2.4 and 2.6
0182   kernels from swsusp.sourceforge.net. It includes support for SMP, 4GB
0183   highmem and preemption. It also has a extensible architecture that
0184   allows for arbitrary transformations on the image (compression,
0185   encryption) and arbitrary backends for writing the image (eg to swap
0186   or an NFS share[Work In Progress]). Questions regarding suspend2
0187   should be sent to the mailing list available through the suspend2
0188   website, and not to the Linux Kernel Mailing List. We are working
0189   toward merging suspend2 into the mainline kernel.
0190 
0191 Q:
0192   What is the freezing of tasks and why are we using it?
0193 
0194 A:
0195   The freezing of tasks is a mechanism by which user space processes and some
0196   kernel threads are controlled during hibernation or system-wide suspend (on
0197   some architectures).  See freezing-of-tasks.txt for details.
0198 
0199 Q:
0200   What is the difference between "platform" and "shutdown"?
0201 
0202 A:
0203   shutdown:
0204         save state in linux, then tell bios to powerdown
0205 
0206   platform:
0207         save state in linux, then tell bios to powerdown and blink
0208         "suspended led"
0209 
0210   "platform" is actually right thing to do where supported, but
0211   "shutdown" is most reliable (except on ACPI systems).
0212 
0213 Q:
0214   I do not understand why you have such strong objections to idea of
0215   selective suspend.
0216 
0217 A:
0218   Do selective suspend during runtime power management, that's okay. But
0219   it's useless for suspend-to-disk. (And I do not see how you could use
0220   it for suspend-to-ram, I hope you do not want that).
0221 
0222   Lets see, so you suggest to
0223 
0224   * SUSPEND all but swap device and parents
0225   * Snapshot
0226   * Write image to disk
0227   * SUSPEND swap device and parents
0228   * Powerdown
0229 
0230   Oh no, that does not work, if swap device or its parents uses DMA,
0231   you've corrupted data. You'd have to do
0232 
0233   * SUSPEND all but swap device and parents
0234   * FREEZE swap device and parents
0235   * Snapshot
0236   * UNFREEZE swap device and parents
0237   * Write
0238   * SUSPEND swap device and parents
0239 
0240   Which means that you still need that FREEZE state, and you get more
0241   complicated code. (And I have not yet introduce details like system
0242   devices).
0243 
0244 Q:
0245   There don't seem to be any generally useful behavioral
0246   distinctions between SUSPEND and FREEZE.
0247 
0248 A:
0249   Doing SUSPEND when you are asked to do FREEZE is always correct,
0250   but it may be unnecessarily slow. If you want your driver to stay simple,
0251   slowness may not matter to you. It can always be fixed later.
0252 
0253   For devices like disk it does matter, you do not want to spindown for
0254   FREEZE.
0255 
0256 Q:
0257   After resuming, system is paging heavily, leading to very bad interactivity.
0258 
0259 A:
0260   Try running::
0261 
0262     cat /proc/[0-9]*/maps | grep / | sed 's:.* /:/:' | sort -u | while read file
0263     do
0264       test -f "$file" && cat "$file" > /dev/null
0265     done
0266 
0267   after resume. swapoff -a; swapon -a may also be useful.
0268 
0269 Q:
0270   What happens to devices during swsusp? They seem to be resumed
0271   during system suspend?
0272 
0273 A:
0274   That's correct. We need to resume them if we want to write image to
0275   disk. Whole sequence goes like
0276 
0277       **Suspend part**
0278 
0279       running system, user asks for suspend-to-disk
0280 
0281       user processes are stopped
0282 
0283       suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
0284       with state snapshot
0285 
0286       state snapshot: copy of whole used memory is taken with interrupts
0287       disabled
0288 
0289       resume(): devices are woken up so that we can write image to swap
0290 
0291       write image to swap
0292 
0293       suspend(PMSG_SUSPEND): suspend devices so that we can power off
0294 
0295       turn the power off
0296 
0297       **Resume part**
0298 
0299       (is actually pretty similar)
0300 
0301       running system, user asks for suspend-to-disk
0302 
0303       user processes are stopped (in common case there are none,
0304       but with resume-from-initrd, no one knows)
0305 
0306       read image from disk
0307 
0308       suspend(PMSG_FREEZE): devices are frozen so that they don't interfere
0309       with image restoration
0310 
0311       image restoration: rewrite memory with image
0312 
0313       resume(): devices are woken up so that system can continue
0314 
0315       thaw all user processes
0316 
0317 Q:
0318   What is this 'Encrypt suspend image' for?
0319 
0320 A:
0321   First of all: it is not a replacement for dm-crypt encrypted swap.
0322   It cannot protect your computer while it is suspended. Instead it does
0323   protect from leaking sensitive data after resume from suspend.
0324 
0325   Think of the following: you suspend while an application is running
0326   that keeps sensitive data in memory. The application itself prevents
0327   the data from being swapped out. Suspend, however, must write these
0328   data to swap to be able to resume later on. Without suspend encryption
0329   your sensitive data are then stored in plaintext on disk.  This means
0330   that after resume your sensitive data are accessible to all
0331   applications having direct access to the swap device which was used
0332   for suspend. If you don't need swap after resume these data can remain
0333   on disk virtually forever. Thus it can happen that your system gets
0334   broken in weeks later and sensitive data which you thought were
0335   encrypted and protected are retrieved and stolen from the swap device.
0336   To prevent this situation you should use 'Encrypt suspend image'.
0337 
0338   During suspend a temporary key is created and this key is used to
0339   encrypt the data written to disk. When, during resume, the data was
0340   read back into memory the temporary key is destroyed which simply
0341   means that all data written to disk during suspend are then
0342   inaccessible so they can't be stolen later on.  The only thing that
0343   you must then take care of is that you call 'mkswap' for the swap
0344   partition used for suspend as early as possible during regular
0345   boot. This asserts that any temporary key from an oopsed suspend or
0346   from a failed or aborted resume is erased from the swap device.
0347 
0348   As a rule of thumb use encrypted swap to protect your data while your
0349   system is shut down or suspended. Additionally use the encrypted
0350   suspend image to prevent sensitive data from being stolen after
0351   resume.
0352 
0353 Q:
0354   Can I suspend to a swap file?
0355 
0356 A:
0357   Generally, yes, you can.  However, it requires you to use the "resume=" and
0358   "resume_offset=" kernel command line parameters, so the resume from a swap
0359   file cannot be initiated from an initrd or initramfs image.  See
0360   swsusp-and-swap-files.txt for details.
0361 
0362 Q:
0363   Is there a maximum system RAM size that is supported by swsusp?
0364 
0365 A:
0366   It should work okay with highmem.
0367 
0368 Q:
0369   Does swsusp (to disk) use only one swap partition or can it use
0370   multiple swap partitions (aggregate them into one logical space)?
0371 
0372 A:
0373   Only one swap partition, sorry.
0374 
0375 Q:
0376   If my application(s) causes lots of memory & swap space to be used
0377   (over half of the total system RAM), is it correct that it is likely
0378   to be useless to try to suspend to disk while that app is running?
0379 
0380 A:
0381   No, it should work okay, as long as your app does not mlock()
0382   it. Just prepare big enough swap partition.
0383 
0384 Q:
0385   What information is useful for debugging suspend-to-disk problems?
0386 
0387 A:
0388   Well, last messages on the screen are always useful. If something
0389   is broken, it is usually some kernel driver, therefore trying with as
0390   little as possible modules loaded helps a lot. I also prefer people to
0391   suspend from console, preferably without X running. Booting with
0392   init=/bin/bash, then swapon and starting suspend sequence manually
0393   usually does the trick. Then it is good idea to try with latest
0394   vanilla kernel.
0395 
0396 Q:
0397   How can distributions ship a swsusp-supporting kernel with modular
0398   disk drivers (especially SATA)?
0399 
0400 A:
0401   Well, it can be done, load the drivers, then do echo into
0402   /sys/power/resume file from initrd. Be sure not to mount
0403   anything, not even read-only mount, or you are going to lose your
0404   data.
0405 
0406 Q:
0407   How do I make suspend more verbose?
0408 
0409 A:
0410   If you want to see any non-error kernel messages on the virtual
0411   terminal the kernel switches to during suspend, you have to set the
0412   kernel console loglevel to at least 4 (KERN_WARNING), for example by
0413   doing::
0414 
0415         # save the old loglevel
0416         read LOGLEVEL DUMMY < /proc/sys/kernel/printk
0417         # set the loglevel so we see the progress bar.
0418         # if the level is higher than needed, we leave it alone.
0419         if [ $LOGLEVEL -lt 5 ]; then
0420                 echo 5 > /proc/sys/kernel/printk
0421                 fi
0422 
0423         IMG_SZ=0
0424         read IMG_SZ < /sys/power/image_size
0425         echo -n disk > /sys/power/state
0426         RET=$?
0427         #
0428         # the logic here is:
0429         # if image_size > 0 (without kernel support, IMG_SZ will be zero),
0430         # then try again with image_size set to zero.
0431         if [ $RET -ne 0 -a $IMG_SZ -ne 0 ]; then # try again with minimal image size
0432                 echo 0 > /sys/power/image_size
0433                 echo -n disk > /sys/power/state
0434                 RET=$?
0435         fi
0436 
0437         # restore previous loglevel
0438         echo $LOGLEVEL > /proc/sys/kernel/printk
0439         exit $RET
0440 
0441 Q:
0442   Is this true that if I have a mounted filesystem on a USB device and
0443   I suspend to disk, I can lose data unless the filesystem has been mounted
0444   with "sync"?
0445 
0446 A:
0447   That's right ... if you disconnect that device, you may lose data.
0448   In fact, even with "-o sync" you can lose data if your programs have
0449   information in buffers they haven't written out to a disk you disconnect,
0450   or if you disconnect before the device finished saving data you wrote.
0451 
0452   Software suspend normally powers down USB controllers, which is equivalent
0453   to disconnecting all USB devices attached to your system.
0454 
0455   Your system might well support low-power modes for its USB controllers
0456   while the system is asleep, maintaining the connection, using true sleep
0457   modes like "suspend-to-RAM" or "standby".  (Don't write "disk" to the
0458   /sys/power/state file; write "standby" or "mem".)  We've not seen any
0459   hardware that can use these modes through software suspend, although in
0460   theory some systems might support "platform" modes that won't break the
0461   USB connections.
0462 
0463   Remember that it's always a bad idea to unplug a disk drive containing a
0464   mounted filesystem.  That's true even when your system is asleep!  The
0465   safest thing is to unmount all filesystems on removable media (such USB,
0466   Firewire, CompactFlash, MMC, external SATA, or even IDE hotplug bays)
0467   before suspending; then remount them after resuming.
0468 
0469   There is a work-around for this problem.  For more information, see
0470   Documentation/driver-api/usb/persist.rst.
0471 
0472 Q:
0473   Can I suspend-to-disk using a swap partition under LVM?
0474 
0475 A:
0476   Yes and No.  You can suspend successfully, but the kernel will not be able
0477   to resume on its own.  You need an initramfs that can recognize the resume
0478   situation, activate the logical volume containing the swap volume (but not
0479   touch any filesystems!), and eventually call::
0480 
0481     echo -n "$major:$minor" > /sys/power/resume
0482 
0483   where $major and $minor are the respective major and minor device numbers of
0484   the swap volume.
0485 
0486   uswsusp works with LVM, too.  See http://suspend.sourceforge.net/
0487 
0488 Q:
0489   I upgraded the kernel from 2.6.15 to 2.6.16. Both kernels were
0490   compiled with the similar configuration files. Anyway I found that
0491   suspend to disk (and resume) is much slower on 2.6.16 compared to
0492   2.6.15. Any idea for why that might happen or how can I speed it up?
0493 
0494 A:
0495   This is because the size of the suspend image is now greater than
0496   for 2.6.15 (by saving more data we can get more responsive system
0497   after resume).
0498 
0499   There's the /sys/power/image_size knob that controls the size of the
0500   image.  If you set it to 0 (eg. by echo 0 > /sys/power/image_size as
0501   root), the 2.6.15 behavior should be restored.  If it is still too
0502   slow, take a look at suspend.sf.net -- userland suspend is faster and
0503   supports LZF compression to speed it up further.