2 * linux/kernel/power/swsusp.c
4 * This file provides code to write suspend image to swap and read it back.
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
7 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
9 * This file is released under the GPLv2.
11 * I'd like to thank the following people for their work:
13 * Pavel Machek <pavel@ucw.cz>:
14 * Modifications, defectiveness pointing, being with me at the very beginning,
15 * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
17 * Steve Doddi <dirk@loth.demon.co.uk>:
18 * Support the possibility of hardware state restoring.
20 * Raph <grey.havens@earthling.net>:
21 * Support for preserving states of network devices and virtual console
22 * (including X and svgatextmode)
24 * Kurt Garloff <garloff@suse.de>:
25 * Straightened the critical function in order to prevent compilers from
26 * playing tricks with local variables.
28 * Andreas Mohr <a.mohr@mailto.de>
30 * Alex Badea <vampire@go.ro>:
33 * Rafael J. Wysocki <rjw@sisk.pl>
34 * Reworked the freeing of memory and the handling of swap
36 * More state savers are welcome. Especially for the scsi layer...
38 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
42 #include <linux/suspend.h>
43 #include <linux/spinlock.h>
44 #include <linux/kernel.h>
45 #include <linux/major.h>
46 #include <linux/swap.h>
48 #include <linux/swapops.h>
49 #include <linux/bootmem.h>
50 #include <linux/syscalls.h>
51 #include <linux/highmem.h>
56 * Preferred image size in bytes (tunable via /sys/power/image_size).
57 * When it is set to N, swsusp will do its best to ensure the image
58 * size will not exceed N bytes, but if that is impossible, it will
59 * try to create the smallest image possible.
61 unsigned long image_size = 500 * 1024 * 1024;
63 int in_suspend __nosavedata = 0;
66 unsigned int count_highmem_pages(void);
67 int save_highmem(void);
68 int restore_highmem(void);
70 static inline int save_highmem(void) { return 0; }
71 static inline int restore_highmem(void) { return 0; }
72 static inline unsigned int count_highmem_pages(void) { return 0; }
76 * The following functions are used for tracing the allocated
77 * swap pages, so that they can be freed in case of an error.
79 * The functions operate on a linked bitmap structure defined
83 void free_bitmap(struct bitmap_page *bitmap)
85 struct bitmap_page *bp;
89 free_page((unsigned long)bitmap);
94 struct bitmap_page *alloc_bitmap(unsigned int nr_bits)
96 struct bitmap_page *bitmap, *bp;
102 bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
104 for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) {
105 bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
115 static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit)
119 n = BITMAP_PAGE_BITS;
120 while (bitmap && n <= bit) {
121 n += BITMAP_PAGE_BITS;
122 bitmap = bitmap->next;
126 n -= BITMAP_PAGE_BITS;
129 while (bit >= BITS_PER_CHUNK) {
130 bit -= BITS_PER_CHUNK;
133 bitmap->chunks[n] |= (1UL << bit);
137 unsigned long alloc_swap_page(int swap, struct bitmap_page *bitmap)
139 unsigned long offset;
141 offset = swp_offset(get_swap_page_of_type(swap));
143 if (bitmap_set(bitmap, offset)) {
144 swap_free(swp_entry(swap, offset));
151 void free_all_swap_pages(int swap, struct bitmap_page *bitmap)
158 for (n = 0; n < BITMAP_PAGE_CHUNKS; n++)
159 for (test = 1UL; test; test <<= 1) {
160 if (bitmap->chunks[n] & test)
161 swap_free(swp_entry(swap, bit));
164 bitmap = bitmap->next;
169 * swsusp_shrink_memory - Try to free as much memory as needed
171 * ... but do not OOM-kill anyone
173 * Notice: all userland should be stopped before it is called, or
174 * livelock is possible.
177 #define SHRINK_BITE 10000
178 static inline unsigned long __shrink_memory(long tmp)
180 if (tmp > SHRINK_BITE)
182 return shrink_all_memory(tmp);
185 int swsusp_shrink_memory(void)
189 unsigned long pages = 0;
193 printk("Shrinking memory... ");
195 size = 2 * count_highmem_pages();
196 size += size / 50 + count_data_pages();
197 size += (size + PBES_PER_PAGE - 1) / PBES_PER_PAGE +
201 if (!is_highmem(zone) && populated_zone(zone)) {
202 tmp -= zone->free_pages;
203 tmp += zone->lowmem_reserve[ZONE_NORMAL];
206 tmp = __shrink_memory(tmp);
210 } else if (size > image_size / PAGE_SIZE) {
211 tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
214 printk("\b%c", p[i++%4]);
216 printk("\bdone (%lu pages freed)\n", pages);
221 int swsusp_suspend(void)
225 if ((error = arch_prepare_suspend()))
228 /* At this point, device_suspend() has been called, but *not*
229 * device_power_down(). We *must* device_power_down() now.
230 * Otherwise, drivers for some devices (e.g. interrupt controllers)
231 * become desynchronized with the actual state of the hardware
232 * at resume time, and evil weirdness ensues.
234 if ((error = device_power_down(PMSG_FREEZE))) {
235 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
239 if ((error = save_highmem())) {
240 printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
241 goto Restore_highmem;
244 save_processor_state();
245 if ((error = swsusp_arch_suspend()))
246 printk(KERN_ERR "Error %d suspending\n", error);
247 /* Restore control flow magically appears here */
248 restore_processor_state();
257 int swsusp_resume(void)
261 if (device_power_down(PMSG_FREEZE))
262 printk(KERN_ERR "Some devices failed to power down, very bad\n");
263 /* We'll ignore saved state, but this gets preempt count (etc) right */
264 save_processor_state();
265 error = swsusp_arch_resume();
266 /* Code below is only ever reached in case of failure. Otherwise
267 * execution continues at place where swsusp_arch_suspend was called
270 /* The only reason why swsusp_arch_resume() can fail is memory being
271 * very tight, so we have to free it as soon as we can to avoid
272 * subsequent failures
275 restore_processor_state();
277 touch_softlockup_watchdog();