Swap suspend

Some warnings, first.

Warning

BIG FAT WARNING

If you touch anything on disk between suspend and resume...

...kiss your data goodbye.

If you do resume from initrd after your filesystems are mounted...

...bye bye root partition.

[this is actually same case as above]

If you have unsupported ( ) devices using DMA, you may have someproblems. If your disk driver does not support suspend... (IDE does),it may cause some problems, too. If you change kernel command linebetween suspend and resume, it may do something wrong. If you changeyour hardware while system is suspended... well, it was not good idea;but it will probably only crash.

( ) suspend/resume support is needed to make it safe.

If you have any filesystems on USB devices mounted before software suspend,they won’t be accessible after resume and you may lose data, as thoughyou have unplugged the USB devices with mounted filesystems on them;see the FAQ below for details. (This is not true for more traditionalpower states like “standby”, which normally don’t turn USB off.)

Swap partition:

You need to append resume=/dev/your_swap_partition to kernel commandline or specify it using /sys/power/resume.

Swap file:

If using a swapfile you can also specify a resume offset usingresume_offset=<number> on the kernel command line or specify itin /sys/power/resume_offset.

After preparing then you suspend by:

echo shutdown > /sys/power/disk; echo disk > /sys/power/state
  • If you feel ACPI works pretty well on your system, you might try:

    echo platform > /sys/power/disk; echo disk > /sys/power/state
  • If you would like to write hibernation image to swap and then suspendto RAM (provided your platform supports it), you can try:

    echo suspend > /sys/power/disk; echo disk > /sys/power/state
  • If you have SATA disks, you’ll need recent kernels with SATA suspendsupport. For suspend and resume to work, make sure your disk driversare built into kernel -- not modules. [There’s way to makesuspend/resume with modular disk drivers, see FAQ, but you probablyshould not do that.]

If you want to limit the suspend image size to N bytes, do:

echo N > /sys/power/image_size

before suspend (it is limited to around 2/5 of available RAM by default).

  • The resume process checks for the presence of the resume device,if found, it then checks the contents for the hibernation image signature.If both are found, it resumes the hibernation image.

  • The resume process may be triggered in two ways:

    1. During lateinit: If resume=/dev/your_swap_partition is specified onthe kernel command line, lateinit runs the resume process. If theresume device has not been probed yet, the resume process fails andbootup continues.

    2. Manually from an initrd or initramfs: May be run fromthe init script by using the /sys/power/resume file. It is vitalthat this be done prior to remounting any filesystems (even asread-only) otherwise data may be corrupted.

Article about goals and implementation of Software Suspend for Linux

Author: Gábor KutiLast revised: 2003-10-20 by Pavel Machek

Idea and goals to achieve

Nowadays it is common in several laptops that they have a suspend button. Itsaves the state of the machine to a filesystem or to a partition and switchesto standby mode. Later resuming the machine the saved state is loaded back toram and the machine can continue its work. It has two real benefits. First wesave ourselves the time machine goes down and later boots up, energy costsare real high when running from batteries. The other gain is that we don’t haveto interrupt our programs so processes that are calculating something for a longtime shouldn’t need to be written interruptible.

swsusp saves the state of the machine into active swaps and then reboots orpowerdowns. You must explicitly specify the swap partition to resume from withresume= kernel option. If signature is found it loads and restores savedstate. If the optionnoresume is specified as a boot parameter, it skipsthe resuming. If the optionhibernate=nocompress is specified as a bootparameter, it saves hibernation image without compression.

In the meantime while the system is suspended you should not add/remove anyof the hardware, write to the filesystems, etc.

Sleep states summary

There are three different interfaces you can use, /proc/acpi shouldwork like this:

In a really perfect world:

echo 1 > /proc/acpi/sleep       # for standbyecho 2 > /proc/acpi/sleep       # for suspend to ramecho 3 > /proc/acpi/sleep       # for suspend to ram, but with more power                                # conservativeecho 4 > /proc/acpi/sleep       # for suspend to diskecho 5 > /proc/acpi/sleep       # for shutdown unfriendly the system

and perhaps:

echo 4b > /proc/acpi/sleep      # for suspend to disk via s4bios

Frequently Asked Questions

Q:

well, suspending a server is IMHO a really stupid thing,but... (Diego Zuccato):

A:

You bought new UPS for your server. How do you install it withoutbringing machine down? Suspend to disk, rearrange power cables,resume.

You have your server on UPS. Power died, and UPS is indicating 30seconds to failure. What do you do? Suspend to disk.

Q:

Maybe I’m missing something, but why don’t the regular I/O paths work?

A:

We do use the regular I/O paths. However we cannot restore the datato its original location as we load it. That would create aninconsistent kernel state which would certainly result in an oops.Instead, we load the image into unused memory and then atomically copyit back to it original location. This implies, of course, a maximumimage size of half the amount of memory.

There are two solutions to this:

  • require half of memory to be free during suspend. That way you canread “new” data onto free spots, then cli and copy

  • assume we had special “polling” ide driver that only uses memorybetween 0-640KB. That way, I’d have to make sure that 0-640KB is freeduring suspending, but otherwise it would work...

suspend2 shares this fundamental limitation, but does not include userdata and disk caches into “used memory” by saving them inadvance. That means that the limitation goes away in practice.

Q:

Does linux support ACPI S4?

A:

Yes. That’s what echo platform > /sys/power/disk does.

Q:

What is ‘suspend2’?

A:

suspend2 is ‘Software Suspend 2’, a forked implementation ofsuspend-to-disk which is available as separate patches for 2.4 and 2.6kernels from swsusp.sourceforge.net. It includes support for SMP, 4GBhighmem and preemption. It also has a extensible architecture thatallows for arbitrary transformations on the image (compression,encryption) and arbitrary backends for writing the image (eg to swapor an NFS share[Work In Progress]). Questions regarding suspend2should be sent to the mailing list available through the suspend2website, and not to the Linux Kernel Mailing List. We are workingtoward merging suspend2 into the mainline kernel.

Q:

What is the freezing of tasks and why are we using it?

A:

The freezing of tasks is a mechanism by which user space processes and somekernel threads are controlled during hibernation or system-wide suspend (onsome architectures). SeeFreezing of tasks for details.

Q:

What is the difference between “platform” and “shutdown”?

A:
shutdown:

save state in linux, then tell bios to powerdown

platform:

save state in linux, then tell bios to powerdown and blink“suspended led”

“platform” is actually right thing to do where supported, but“shutdown” is most reliable (except on ACPI systems).

Q:

I do not understand why you have such strong objections to idea ofselective suspend.

A:

Do selective suspend during runtime power management, that’s okay. Butit’s useless for suspend-to-disk. (And I do not see how you could useit for suspend-to-ram, I hope you do not want that).

Lets see, so you suggest to

  • SUSPEND all but swap device and parents

  • Snapshot

  • Write image to disk

  • SUSPEND swap device and parents

  • Powerdown

Oh no, that does not work, if swap device or its parents uses DMA,you’ve corrupted data. You’d have to do

  • SUSPEND all but swap device and parents

  • FREEZE swap device and parents

  • Snapshot

  • UNFREEZE swap device and parents

  • Write

  • SUSPEND swap device and parents

Which means that you still need that FREEZE state, and you get morecomplicated code. (And I have not yet introduce details like systemdevices).

Q:

There don’t seem to be any generally useful behavioraldistinctions between SUSPEND and FREEZE.

A:

Doing SUSPEND when you are asked to do FREEZE is always correct,but it may be unnecessarily slow. If you want your driver to stay simple,slowness may not matter to you. It can always be fixed later.

For devices like disk it does matter, you do not want to spindown forFREEZE.

Q:

After resuming, system is paging heavily, leading to very bad interactivity.

A:

Try running:

cat /proc/[0-9]*/maps | grep / | sed 's:.* /:/:' | sort -u | while read filedo  test -f "$file" && cat "$file" > /dev/nulldone

after resume. swapoff -a; swapon -a may also be useful.

Q:

What happens to devices during swsusp? They seem to be resumedduring system suspend?

A:

That’s correct. We need to resume them if we want to write image todisk. Whole sequence goes like

Suspend part

running system, user asks for suspend-to-disk

user processes are stopped

suspend(PMSG_FREEZE): devices are frozen so that they don’t interferewith state snapshot

state snapshot: copy of whole used memory is taken with interruptsdisabled

resume(): devices are woken up so that we can write image to swap

write image to swap

suspend(PMSG_SUSPEND): suspend devices so that we can power off

turn the power off

Resume part

(is actually pretty similar)

running system, user asks for suspend-to-disk

user processes are stopped (in common case there are none,but with resume-from-initrd, no one knows)

read image from disk

suspend(PMSG_FREEZE): devices are frozen so that they don’t interferewith image restoration

image restoration: rewrite memory with image

resume(): devices are woken up so that system can continue

thaw all user processes

Q:

What is this ‘Encrypt suspend image’ for?

A:

First of all: it is not a replacement for dm-crypt encrypted swap.It cannot protect your computer while it is suspended. Instead it doesprotect from leaking sensitive data after resume from suspend.

Think of the following: you suspend while an application is runningthat keeps sensitive data in memory. The application itself preventsthe data from being swapped out. Suspend, however, must write thesedata to swap to be able to resume later on. Without suspend encryptionyour sensitive data are then stored in plaintext on disk. This meansthat after resume your sensitive data are accessible to allapplications having direct access to the swap device which was usedfor suspend. If you don’t need swap after resume these data can remainon disk virtually forever. Thus it can happen that your system getsbroken in weeks later and sensitive data which you thought wereencrypted and protected are retrieved and stolen from the swap device.To prevent this situation you should use ‘Encrypt suspend image’.

During suspend a temporary key is created and this key is used toencrypt the data written to disk. When, during resume, the data wasread back into memory the temporary key is destroyed which simplymeans that all data written to disk during suspend are theninaccessible so they can’t be stolen later on. The only thing thatyou must then take care of is that you call ‘mkswap’ for the swappartition used for suspend as early as possible during regularboot. This asserts that any temporary key from an oopsed suspend orfrom a failed or aborted resume is erased from the swap device.

As a rule of thumb use encrypted swap to protect your data while yoursystem is shut down or suspended. Additionally use the encryptedsuspend image to prevent sensitive data from being stolen afterresume.

Q:

Can I suspend to a swap file?

A:

Generally, yes, you can. However, it requires you to use the “resume=” and“resume_offset=” kernel command line parameters, so the resume from a swapfile cannot be initiated from an initrd or initramfs image. SeeUsing swap files with software suspend (swsusp) for details.

Q:

Is there a maximum system RAM size that is supported by swsusp?

A:

It should work okay with highmem.

Q:

Does swsusp (to disk) use only one swap partition or can it usemultiple swap partitions (aggregate them into one logical space)?

A:

Only one swap partition, sorry.

Q:

If my application(s) causes lots of memory & swap space to be used(over half of the total system RAM), is it correct that it is likelyto be useless to try to suspend to disk while that app is running?

A:

No, it should work okay, as long as your app does notmlock()it. Just prepare big enough swap partition.

Q:

What information is useful for debugging suspend-to-disk problems?

A:

Well, last messages on the screen are always useful. If somethingis broken, it is usually some kernel driver, therefore trying with aslittle as possible modules loaded helps a lot. I also prefer people tosuspend from console, preferably without X running. Booting withinit=/bin/bash, then swapon and starting suspend sequence manuallyusually does the trick. Then it is good idea to try with latestvanilla kernel.

Q:

How can distributions ship a swsusp-supporting kernel with modulardisk drivers (especially SATA)?

A:

Well, it can be done, load the drivers, then do echo into/sys/power/resume file from initrd. Be sure not to mountanything, not even read-only mount, or you are going to lose yourdata.

Q:

How do I make suspend more verbose?

A:

If you want to see any non-error kernel messages on the virtualterminal the kernel switches to during suspend, you have to set thekernel console loglevel to at least 4 (KERN_WARNING), for example bydoing:

# save the old loglevelread LOGLEVEL DUMMY < /proc/sys/kernel/printk# set the loglevel so we see the progress bar.# if the level is higher than needed, we leave it alone.if [ $LOGLEVEL -lt 5 ]; then        echo 5 > /proc/sys/kernel/printk        fiIMG_SZ=0read IMG_SZ < /sys/power/image_sizeecho -n disk > /sys/power/stateRET=$?## the logic here is:# if image_size > 0 (without kernel support, IMG_SZ will be zero),# then try again with image_size set to zero.if [ $RET -ne 0 -a $IMG_SZ -ne 0 ]; then # try again with minimal image size        echo 0 > /sys/power/image_size        echo -n disk > /sys/power/state        RET=$?fi# restore previous loglevelecho $LOGLEVEL > /proc/sys/kernel/printkexit $RET
Q:

Is this true that if I have a mounted filesystem on a USB device andI suspend to disk, I can lose data unless the filesystem has been mountedwith “sync”?

A:

That’s right ... if you disconnect that device, you may lose data.In fact, even with “-o sync” you can lose data if your programs haveinformation in buffers they haven’t written out to a disk you disconnect,or if you disconnect before the device finished saving data you wrote.

Software suspend normally powers down USB controllers, which is equivalentto disconnecting all USB devices attached to your system.

Your system might well support low-power modes for its USB controllerswhile the system is asleep, maintaining the connection, using true sleepmodes like “suspend-to-RAM” or “standby”. (Don’t write “disk” to the/sys/power/state file; write “standby” or “mem”.) We’ve not seen anyhardware that can use these modes through software suspend, although intheory some systems might support “platform” modes that won’t break theUSB connections.

Remember that it’s always a bad idea to unplug a disk drive containing amounted filesystem. That’s true even when your system is asleep! Thesafest thing is to unmount all filesystems on removable media (such USB,Firewire, CompactFlash, MMC, external SATA, or even IDE hotplug bays)before suspending; then remount them after resuming.

There is a work-around for this problem. For more information, seeUSB device persistence during system suspend.

Q:

Can I suspend-to-disk using a swap partition under LVM?

A:

Yes and No. You can suspend successfully, but the kernel will not be ableto resume on its own. You need an initramfs that can recognize the resumesituation, activate the logical volume containing the swap volume (but nottouch any filesystems!), and eventually call:

echo -n "$major:$minor" > /sys/power/resume

where $major and $minor are the respective major and minor device numbers ofthe swap volume.

uswsusp works with LVM, too. Seehttp://suspend.sourceforge.net/

Q:

I upgraded the kernel from 2.6.15 to 2.6.16. Both kernels werecompiled with the similar configuration files. Anyway I found thatsuspend to disk (and resume) is much slower on 2.6.16 compared to2.6.15. Any idea for why that might happen or how can I speed it up?

A:

This is because the size of the suspend image is now greater thanfor 2.6.15 (by saving more data we can get more responsive systemafter resume).

There’s the /sys/power/image_size knob that controls the size of theimage. If you set it to 0 (eg. by echo 0 > /sys/power/image_size asroot), the 2.6.15 behavior should be restored. If it is still tooslow, take a look at suspend.sf.net -- userland suspend is faster andsupports LZF compression to speed it up further.