About Kernel Documentation Linux Kernel Contact Linux Resources Linux Blog

Documentation / power / swsusp.txt


Based on kernel version 4.16.1. Page generated on 2018-04-09 11:53 EST.

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


About Kernel Documentation Linux Kernel Contact Linux Resources Linux Blog